0% found this document useful (0 votes)
62 views35 pages

D Ata S Tructu Res: Chapter - 4: Queues

The document discusses different types of queues including their definitions, common operations like push and pop, and various implementations using arrays, linked lists, and stacks. It also covers more advanced queue types such as circular queues, deques, and priority queues.

Uploaded by

HAFE DESTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views35 pages

D Ata S Tructu Res: Chapter - 4: Queues

The document discusses different types of queues including their definitions, common operations like push and pop, and various implementations using arrays, linked lists, and stacks. It also covers more advanced queue types such as circular queues, deques, and priority queues.

Uploaded by

HAFE DESTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

D ata S tructures

Chapter – 4: Queues

Asmelash Girmay
Department of Information Technology
4.1 Definition of Queue
• A queue is logically a first in first out (FIFO or first come first serve) linear
data structure.
• 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 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
center.
• It is a homogeneous collection of elements in which new elements are
added at one end called rear, and the existing elements are deleted from
other end called front.

2018-10-30 IT3201 Data Structures 2


4.2 Operations on Queue
• The basic operations that can be performed on queue are:
1. Push: Insert (or add) an element to the queue.
2. Pop: Delete (or remove) an element from a queue.
• Push operation will insert (or add) an element to queue, at the rear
end, by incrementing the array index or Incrementing rear.
• Pop operation will delete (or remove) from the front end by
decrementing the array index (or Incrementing front) and will assign
the deleted value to a variable.
• Total number of elements present in the queue is |front-rear-1| or
rear-front+1, when implemented using arrays.
2018-10-30 IT3201 Data Structures 3
4.2 Operations on Queue …
1
4
1. Queue is empty
4. Push (41)

5
2
2. Push (10) 5. Push (70)

6
3
3. Push (3)
6. X = pop () (i.e. X=10)

2018-10-30 IT3201 Data Structures 4


4.2 Operations on Queue …

7 9

7. Push (11) 9. X= pop() ( i.e. X=41)

8. X = Pop () ( i.e. X=3)

2018-10-30 IT3201 Data Structures 5


4.3 Implementation of Queue
• Queue can be implemented in three ways:
1. Using arrays (static)
2. Using pointers (linked lists) (dynamic)
3. Using two stacks (static and dynamic depending on the stack
implementation)

Implementation of Queue – LAB #5


2018-10-30 IT3201 Data Structures 6
4.3.1 Array Implementation of Queue
• Underflow and overflow conditions can occur when a queue is implemented
using arrays.
Que: Do you think that overflow and underflow occurs in dynamic?
• If we try to pop (or delete or remove) an element from queue when it is empty,
underflow occurs.
• It is not possible to delete (or take out) any element when there is no element in the queue.
• Suppose maximum size of the queue (when it is implemented using arrays) is 50.
If we try to push (or insert or add) an element to queue, overflow occurs.
• When queue is full it is naturally not possible to insert any more elements

2018-10-30 IT3201 Data Structures 7


4.3.1 Static Imp’n - Algorithms
• Let Q be the array of some specified size say SIZE

- 1)

2018-10-30 IT3201 Data Structures 8


4.3.2 Linked List Imp’n of Queue
• The implementation issues of the queue (First In First Out - FIFO) using linked list
as illustrated in the following figures.

2018-10-30 IT3201 Data Structures 9


4.3.2 Linked List Imp’n of Queue…

push (20)

2018-10-30 IT3201 Data Structures 10


4.3.2 Linked List Imp’n of Queue…
• Algorithm for PUSH Operation
• REAR is a pointer in queue where the new elements are added.
• FRONT is a pointer pointing to the queue where the elements are popped.
• DATA is a data element to be pushed.

2018-10-30 IT3201 Data Structures 11


4.3.2 Linked List Imp’n of Queue…
• Algorithm for POP Operation
• REAR is a pointer in queue where the new elements are added.
• FRONT is a pointer, which is pointing to the queue where the elements are popped.
• DATA is a data element popped from the queue.

2018-10-30 IT3201 Data Structures 12


4.3.3 Stacks Implementation of Queue
• A queue can be implemented using two stacks.
• Suppose STACK1 and STACK2 are the two stacks.
• When an element is pushed on to the queue, push the same on STACK1.
• When an element is popped from the queue, pop all elements of STACK1 and
push the same on STACK2.
• Then pop the topmost element of STACK2; which is the first (front) element to be
popped from the queue.
• Then pop all elements of STACK2 and push the same on STACK1 for next
operation (i.e., push or pop).

2018-10-30 IT3201 Data Structures 13


4.3.4 Drawback of Queue
• Suppose a queue Q has maximum size 5, say 5 elements pushed and 2 elements
popped.
• Now if we attempt to add more elements, even though 2 queue cells are free, the
elements cannot be pushed.
• Because in a queue, elements are always inserted at the rear end and hence rear
points to last location of the queue array Q[4].
• That is queue is full (overflow condition) though it has is empty location.
• This limitation can be overcome by using,
for example, circular queue.

2018-10-30 IT3201 Data Structures 14


4.4 Other Queues
• There are three major variations in a simple queue. They are:
1. Circular queue
2. Double ended queue (de-queue)
3. Priority queue
• Priority queue is generally implemented using linked list.

Implementation of Other Queues – LAB #6


2018-10-30 IT3201 Data Structures 15
4.4.1 Circular Queue
• In circular queues the elements Q[0], Q[1], Q[2] .... Q[n – 1] is
represented in a 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 last location at the
queue is full.
• Suppose Q is a queue array of size 6.
• Push and pop operation can be performed on circular (see figures
below).

2018-10-30 IT3201 Data Structures 16


4.4.1 Circular Queue …

2018-10-30 IT3201 Data Structures 17


4.4.1 Circular Queue…
• At any time, the position of the element to be inserted will be
calculated by the relation Rear = (Rear + 1) % SIZE
• After deleting an element from 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 to be inserted, rear,
compare it with front.
• If (rear = front), the queue is full and cannot be inserted anymore.

2018-10-30 IT3201 Data Structures 18


4.4.1 Algorithm for Circular Queue
• Let Q be the array Inserting an element to circular Queue
of some specified 1. Initialize FRONT = –1; REAR = –1
size say SIZE. 2. TEMP = (REAR + 1) % SIZE
• FRONT and REAR 3. If (FRONT is equal to TEMP)
are two pointers (a) Display “Queue is full”
where the (b) Exit
elements are 4. REAR = TEMP
deleted and 5. Input the value to be inserted and assign to variable “DATA”
inserted at two 6. If (FRONT is equal to –1)
ends of the circular (a) FRONT = 0
queue. (b) REAR = 0
• DATA is the 7. Q[REAR] = DATA
element to be 8. Repeat steps 2 to 6 to insert more elements
inserted 9. Exit

2018-10-30 IT3201 Data Structures 19


4.4.1 Algorithm for Circular Queue…

2018-10-30 IT3201 Data Structures 20


4.4.2 Deque
• A deque is a homogeneous list in which elements can be added or inserted
(called push operation) and deleted or removed from both the ends (which is
called pop operation).
• i.e. we can add a new element at the rear or front end and also, we can remove an element
from both front and rear end.
• Hence it is called Double Ended Queue.

2018-10-30 IT3201 Data Structures 21


4.4.2 Deque…
• There are two types of deque depending upon the restriction to
perform insertion or deletion operations at the two ends. They are:
1. Input restricted deque
2. Output restricted deque
1. An input restricted deque: allows insertion at only 1 end, rear end,
but allows deletion at both ends, rear and front end of the lists.
2. An output-restricted deque: allows deletion at only one end, front
end, but allows insertion at both ends, rear and front ends of the
lists.

2018-10-30 IT3201 Data Structures 22


4.4.2.1 Operations on Deque
• The possible operations performed on deque are:
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.

2018-10-30 IT3201 Data Structures 23


4.4.2.2 Deque Algorithms
• Let Q be the array of MAX
elements. front (or left) and
rear (or right) are two array
index (pointers), where the
addition and deletion of
elements occurred.
• Let DATA be the element to
be inserted.
• Before inserting any
element to the queue left
and right pointer will point
to the – 1.

2018-10-30 IT3201 Data Structures 24


4.4.2.2 Deque Algorithms…

2018-10-30 IT3201 Data Structures 25


4.4.2.2 Deque Algorithms…

• Let Q be the array of


MAX elements.
• front (or left) and rear
(or right) are two array
index (pointers), where
the addition and
deletion of elements
occurred.
• DATA will contain the
element just deleted.

2018-10-30 IT3201 Data Structures 26


4.4.2.2 Deque Algorithms…

2018-10-30 IT3201 Data Structures 27


4.4.3 Priority Queue
• Priority Queue is a queue where each element is assigned a priority.
• In priority queue, the elements are deleted and processed by following
rules.
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with the same priority are processed according to the order in which
they were inserted to the queue.
• For example, Consider a manager who is in a process of checking and
approving files in a first come first serve basis.
• In between, if any urgent file (with a high priority) comes, he will process
the urgent file next and continue with the other low urgent files.

2018-10-30 IT3201 Data Structures 28


4.4.3 Priority Queue…

2018-10-30 IT3201 Data Structures 29


4.4.3 Priority Queue…
• Above Fig. gives a pictorial representation of priority queue using
arrays after adding 5 elements (10,14,12,60,13) with their
corresponding priorities (9,10,17,30,46).
• Here the priorities of the data are in ascending order.
• Always we may not be pushing the data in an ascending order.
• From the mixed priority list, it is difficult to find the highest priority
element if the priority queue is implemented using arrays.

2018-10-30 IT3201 Data Structures 30


4.4.3 Priority Queue…
• Using linked list, a node in the priority queue will contain
• DATA – stores the actual information
• PRIORITY – stores the priority of DATA
• NEXT field – stores the address of the next node.
• When an element is inserted into the priority queue, it will check the
priority of the element with the element(s) present in the linked list
to find the suitable position to insert.

2018-10-30 IT3201 Data Structures 31


4.4.3 Priority Queue…
• The node will be inserted in such a way that the data in the priority
field(s) is in ascending order.
• We do not use rear pointer when it is implemented using linked list,
because the new nodes are not always inserted at the rear end.

2018-10-30 IT3201 Data Structures 32


4.4.3 Priority Queue…
• Following figures will illustrate the push and pop operation of priority queue using linked list.

2018-10-30 IT3201 Data Structures 33


3.5 Applications of Queue
• Round robin techniques for processor scheduling is implemented
using queue.
• Printer server routines (in drivers) are designed using queues.
• All types of customer service software (like Railway/Air ticket
reservation) are designed using queue to give proper service to the
customers.

2018-10-30 IT3201 Data Structures 34


The End ☺
2018-10-30 IT3201 Data Structures 35

You might also like