UNIT - 1 Queues
UNIT - 1 Queues
DEQUEUE ENQUEUE
FRONT REAR
Schematic Representation of Queue
Representation of Queue
A Queue can be represented in two ways
1. Using arrays
2. Using Linked List
N-2
N-1
N
. . - - - . .
In array representation of Queue, two pointers are used to indicate two ends of Queue.
The above representation states as follows.
1. Queue Empty condition
Front = -1 and Rear = -1
Front = NULL and Rear = NULL
2. Queue Full condition
Rear = N - 1 where N is the size of the array we are taken
3. Queue conta ins only on e element
Front = = Rear
4. Number of items in Queue is
Rear – Front + 1
Queue overflow: Trying to perform ENQUEUE (insertion) operation in full Queue is known
as Queue overflow.
Queue overflow condition is Rear == N - 1
Queue Underflow: Trying to perform DEQUEUE (deletion) operation on empty Queue is
known as Queue Underflow.
Algorithm Dequeue( )
Input: Queue with some elements.
Output: Element is deleted from queue at front end if queue is not empty.
1. if(front = = -1 and rear = = -1 )
a) print(Q is empty, not possible for dequeue operation)
2. else
i) if(front = = rear) /* Q has only one element */
a) item=Q[front]
b) front=-1
c) rear=-1
ii) else
a)item=Q[front]
b)front=front+1
iii) end if
iv) print(deleted item is item)
3. end if
End Dequeue
Algorithm Queue_Status( )
Input: Queue with some elements.
Output: Status of the queue. i.e. Q is empty or not, Q is full or not, Element at front end and
rear end.
1. if(front = = -1 and rear = = -1)
a) print(Q is empty)
else if (rear = = N- 1 )
b) print(Q is full)
2. else
i) if(front = = rear)
a) print(Q has only one item)
ii) else
a) print(element at front end is Q[front])
b) print(element at rear end is Q[rear])
iii) end if
3. end if
End Queue_Status
In Linked List Representation of Queue, Front always points to First node in the
Linked List and Rear always points to Last node in the Linked List.
header N1 N2 N3 new
X X
2
Front Rear NULL
3
Rear
Before ENQUEUE
header N1 N2 N3 new
X X
Rear
After ENQUEUE
1. Previous last node link part is replaced with address of new node.
2. Link part of new node is replaced with NULL, because new nodes becomes the last
node.
3. Rear is points to last node in the list. i.e. newly inserted node in the list.
Algorithm Dequeue_LL( )
Input: Queue with some elements
Output: Element is deleted at front end if queue is not empty.
1.if(front= =NULL and rear = =NULL)
a) print(queue is empty, not possible to perform dequeue operation)
2. else
i) if(front = = rear) /* Q has only one element */
a) header.link = NULL
b) item=front.data
c) front=NULL
d) rear=NULL
b) else /* Q has more than one element */
a) header.link = front.link /* 1 */
b) item=front.data
c) free(front)
d) front=header.link /* 2 */
c) end if
d) print(deleted element is item)
3. end if
End_Dequeue_LL
1. Circular Queues
Physically a circular array is same as ordinary array a[i-N], but logically itimplements
that a[0] comes after a[N] or a[N] comes before a[0].
The following figure shows the physical and logical representation for circular
array
Here both Front and Rear pointers are move in clockwise direction. This is controlled
by the MOD operation.
For e.g. if the current pointer is at i, then shift next location will
be (i + 1 % LENTH) , 1<= i <= Length
Circular Queue empty condition is
Front = -1 and Rear = -1
Algorithm CQ_Dequeue( )
Input: Circular Queue with some elements.
Output: Element is deleted from circular queue at front end if circular queue is not empty.
1. if(front = = -1 and rear = = -1)
a) print(CQ is empty, not possible for cqueue operation)
2. else
i) if(front = = rear) /* Q has only one element */
a) item=CQ[front]
b) front= -1
c) rear= -1
ii) else
a)item=CQ[front]
b)front=(front + 1 % N)
iii) end if
iv) print(deleted item is item)
3. end if
End CQ_Dequeue
**************************