Data Structures andAlgorithms
◼ Queue
▪ An ordered list in which data can be added from an end
called as rear and removed from other end called as front
▪ FIFO
◼ Operations
▪ Insert/Enqueue: addition of data from rear
▪ Delete/Dequeue: remove data from front
◼ Linear, Circular and Priority Queue
2
Key Learning Points
3.
Data Structures andAlgorithms
◼ Rear points to the topmost and Front points to bottom most element of queue.
[True/False]
◼ Queue is Last-in-Last-Out list. [True/False]
◼ If we insert the elements A, B, C, D, in that order, in a Queue then ……. is the
first element we delete from the queue
◼ In a circular queue, after every insert operation rear is updated as
a. rear++ b. rear = (rear -1) % MAXQSIZE c. rear= front d. rear = (rear +1) % MAXQSIZE
◼ The condition in which we have rear=front in a Queue is called as
a. House full b. Don’t Care c. Full d. Empty
◼ An element with …………….. is processed before elements with …………. in a
priority queue.
3
Teach to Learn
4.
◼ Static andDynamic List Structures
◼ Array Implementation of List
◼ Queues as list
4
List
Contents
5.
◼ After completionof this chapter you will be able to:
▪ Define list as a Data Structure
▪ Define static and dynamic list
▪ Compare static and dynamic list
▪ Implement the list in form of an array
▪ Implement Queue as a list
▪ Attempt 6 marks’ question in final exams
5
List
Objectives
6.
◼ A linearlist is a data structure each element of which
has a unique successor:
◼ Array is the simplest linear list
◼ There are two types of list:
▪ General list
▪ Restricted list
6
List
Definition
element 1 element 2 element 3
7.
◼ General list:
▪no restrictions on where data can be inserted/deleted, and on
which operations can be used on the list
▪ Random list:there is no ordering on data
▪ Ordered list:data are arranged according to a key
◼ Restricted list:
▪ data can be inserted/deleted and operations are performed only at
the ends of the list
▪ FIFO (First-In-First-Out): queue
▪ LIFO (Last-In-First-Out): stack
7
List
Definition {Contd..}
8.
◼ Insertion
▪ Theprocess of adding data to the list
◼ Deletion
▪ Is the process of searching and removing data from the
list
◼ Retrieval
▪ also requires list searching, but does not change the
contents of the list.
◼ Traversal
▪ is retrieval of all elements in sequence
8
List
Operations
9.
◼ Insertion
▪ Randomlist: insertion can be made at the beginning, the middle or
the end of the list.
▪ Ordered list: the data must be inserted so that the ordering of the
list is maintained.
▪ Array requires physical shifting
9
List
Operations {Contd..}
10 20 30
25
10 20 25 30
10.
◼ Deletion
▪ Deletionfrom a general list requires searching the list in
order to locate the data being deleted.
▪ Array requires physical shifting after the data is deleted.
10
List
Operations {Contd..}
25
10 20 30
10 20 25 30
11.
◼ void menu()
◼void create();
◼ void insert(int, int);
◼ void del(int);
◼ void find(int);
◼ void display();
◼ int isfull();
◼ int isempty();
11
List
Implementation using Array
12.
◼ int menu()
intch;
clrscr();
printf("nt LIST Implementation using Arrays");
printf("nt1. Creatent2. Insertnt3. Deletent4.
Countnt5. Findnt6. Displaynt7. ExitnnEnter Your
Choice:");
scanf("%d",&ch);
printf("nn");
return ch;
12
List
Implementation using Array {Contd..}
13.
◼ void create()
intelement;
int flag=1;
while(flag==1)
{
printf("nEnter an element:");
scanf("%d", &element);
l.list[l.length]=element;
l.length++;
printf("nPress '1' to insert another element!n");
scanf("%d",&flag);
}
13
List
Implementation using Array {Contd..}
14.
◼ void insert(int,int)
int i;
if(pos==0)
{
printf("nnCannot insert at zeroth position.");
getch();
return;
}
if(pos-1>l.length)
{
printf("nnOnly %d elements exit. Cannot insert at %d
position",l.length,pos);
printf("nPress any key to continue..");
getch();
}
14
List
Implementation using Array {Contd..}
◼ void del(int)
inti;
if(pos==0)
{
printf("nn No Element at zerothposition");
getch();
return;
}
if(pos>l.length)
{
printf("nnOnly%d elements. Cannot delete element at %d position.", l.length,
pos);
printf("Press any key to continue..");
return;
}
for(i=pos;i<l.length;i++)
{
l.list[i]=l.list[i+1];
}
l.length--;
16
List
Implementation using Array {Contd..}
17.
◼ Void count()
printf("nEnternumber ofelements in the list is %d",l.length);
printf("nPress any key to continue..");
17
List
Implementation using Array {Contd..}
18.
◼ void find(int)
inti;
int flag=1;
for(i=0;i<l.length;i++)
{
if(l.list[i]==element)
{
printf("%d exists at %d position", element, i+1);
flag=0;
printf("nPress any key to continue..");
getch();
break;
}
}
if(flag==1)
{
printf("%d not found.nPress any key to continue..", element);
getch();
}
18
List
Implementation using Array {Contd..}
19.
◼ void display()
inti;
for(i=0;i<l.length;i++)
{
printf("nElement: %dtPosition: %d",l.list[i], i+1);
}
printf("nPress any key to continue...");
getch();
19
List
Implementation using Array {Contd..}