Professional Documents
Culture Documents
Week6 PDF
Week6 PDF
NFCIET Multan 1
Data Structures
&
Algorithms
Subject Teacher
Engr. Zeeshan Raza
EE Dept. NFCIET Multan
Queue
A Queue is a linear list of elements.
In a queue, deletions can take place
only at one end, and insertions can
take place only at the other end.
The term Front is used for the
deletions end and Rear is used for the
insertions end.
Queue (FIFO)
Queue is also called (first in first out
(FIFO) lists.
The first element entering in the
queue will be the first one out of the
queue.
The elements in which elements enter
a queue is the order in which they
leave.
Queue (Example)
For example, the people waiting in
line at a bank form a queue, where
the first person in line is the first one
to be waited on; and so on. So queue
is called FIFO.
In computers, timesharing system is an
example of queue in which programs
of same priority form a queue while
waiting to be executed.
Queue (Insertion,
Deletion)
Queue (Place for new
element)
Queue (Assume as
circular)
Queue (Using circular
technique)
N=6
(while deletion)
Front = Front+1
OR (if Front = N)
Front = 1
(while insertion)
Rear = Rear+1
OR (if Rear = N)
Rear = 1
Queue Insert (Queue, N, Front, Rear,
Item)
This procedure inserts an element ITEM into a queue.
1. [ Queue already filled .. ? ]
If Front = 1 and Rear = N, or if Front = Rear + 1,
then: write; OVERFLOW and Return.
2. [ find new value of Rear ]
If Front = Null, [ Queue initially empty. ]
then: Set Front = 1 and Rear = 1,
Else if Rear = N, then: Set Rear = 1
Else: Set Rear = Rear+ 1.
(end of If structure)
3. Set Queue [Rear] = Item. [ this inserts new element. ]
4. Return
Queue Delete (Queue, N, Front, Rear,
Item)
This procedure inserts an element from a queue and assigns it to a
variable ITEM.
1. [ Queue already empty .. ? ]
If Front = Null, then: write; OVERFLOW and Return.
2. Set Item = Queue [Front] . [ this assigns element to Item. ]
3. [ find new value of Front ]
If Front = Rear, [ Queue has only one element to start. ]
then: Set Front = Null and Rear = Null,
Else if Front = N, then: Set Front = 1
Else: Set Front = Front+ 1.
(end of If structure)
4. Return
Advantage of Queue