Data Structures: The Queues
Data Structures: The Queues
Data Structures
Lecture 5 :
The Queues
The Queues
A queue is logically a first in first out (FIFO or first come first serve)
The concept of queue can be understood by our real life problems. For
example a customer come and join in a queue to take the train ticket at
the end (rear) and the ticket is issued from the front to end of queue. That
is, the customer who arrived first will receive the ticket first. It means the
customers are serviced in the order in which they arrive at the service
centre.
3
The Queues
added at one end called REAR, and the existing elements are deleted
incrementing the array index. Pop operation will delete from the front end
by decrementing the array index and will assign the deleted value to a
variable.
4
The Queues
Following figure will illustrate the basic operations on queue.
5
The Queues
6
The Queues
7
The Queues
The Queues
The Queues
The Queues
(c) Exit
2. Else
2. Else
3. Exit 3. Exit
12
The Queues
1- ARRAYS
2- Pointers
The Queues
1. Circular Queue
3. Priority Queue
discussed in Ch 5 sec 13. The other two queue variations are discussed in the
following sections.
14
The Queues
circular fashion with Q[1] following Q[n]. A circular queue is one in which the
insertion of a new element is done at the very first location of the queue if
The Queues
16
The Queues
17
The Queues
After inserting an element at last location Q[5], the next element will be
inserted at the very first location (i.e., Q[0]) that is circular queue is one
in which the first element comes just after the last element.
18
The Queues
19
The Queues
circular queue the position of the front end is calculated by the relation
Front= (Front + 1) % SIZE After locating the position of the new element
The Queues
The Queues
1. If (front is equal to – 1)
(a) Display “Queue is empty” (b) Exit
2. Else
(a) data = Q[front]
3. If (front is equal to rear)
(a) front = –1 (b) rear = –1
4. Else
(a) front = (front +1) % SIZE
5. Repeat the steps 1 to 4 if we want to delete more elements
6. Exit
22
The Queues
void circular_queue::insert()
{
int data;
//Checking for overflow condition
if ((front == 0 && rear == MAX-1) || (front == rear + 1))
{
cout<<“\nQueue Overflow \n”;
return;
}
if (front == –1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
if (rear == MAX-1) /*rear is at last position of queue */
rear = 0;
else
rear = rear + 1;
cout<<“\nInput the element for insertion in queue:”;
cin>> data;
cqueue_arr[rear] = data;
}/*End of insert()*/
23
The Queues
24
The Queues
void circular_queue::del()
{
//Checking for queue underflow
if (front == –1)
{
cout<<“\nQueue Underflow\n”;
return;
}
cout<<“\n Element deleted from queue is:”<<cqueue_arr[front]<<“\n”;
if (front == rear)
{
front = –1;
rear = –1;
}
else
if(front == MAX-1)
front = 0;
else
front = front + 1;
}/*End of del()*/
25
The Queues
The Queues
4.4. DE-QUEUES
inserted (called push operation) and deleted or removed from both the
the rear or front end and also we can remove an element from both
The Queues
28
The Queues
They are:
The Queues
An input restricted, which allows insertion at only 1 end, rear end, but
allows deletion at both ends, rear and front end of the lists.
An output-restricted, which allows deletion at only one end, front end, but
allows insertion at both ends, rear and front ends of the lists.
The possible operation performed on deque is
1. Add an element at the rear end
2. Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Only 1st, 3rd and 4th operations are performed by input-restricted deque
and 1st, 2nd and 3rd operations are performed by output-restricted deque.
30
The Queues
4.4.1. ALGORITHMS FOR INSERTING AN ELEMENT
The Queues
INSERT AN ELEMENT AT THE LEFT SIDE OF THE DE-QUEUE
1. Input the DATA to be inserted
2. If ((left == 0 && right == MAX–1) || (left == right+1))
(a) Display “Queue Overflow” (b) Exit
3. If (left == – 1)
(a) left = 0 (b) right = 0
4. Else
(a) if (left == 0)
(i) left = MAX – 1
(b) else
(i) left = left – 1
5. Q[left] = DATA
6. Exit
32
The Queues
4.4.2. ALGORITHMS FOR DELETING AN ELEMENT
DELETE AN ELEMENT FROM THE RIGHT SIDE OF THE DE-QUEUE
1. If (left == – 1)
(a) Display “Queue Underflow” (b) Exit
2. DATA = Q [right]
3. If (left == right)
(a) left = – 1 (b) right = – 1
4. Else
(a) if(right == 0)
(i) right = MAX-1
(b) else
(i) right = right-1
5. Exit
33
The Queues
The Queues
within Lab
35
The Queues
reservation).
36
The Queues
Compulsory:
4, 5, 7, 9, 10
37
Any Questions?