0% found this document useful (0 votes)
12 views13 pages

Understanding Queue Data Structures

This document provides an overview of queues as a linear data structure, detailing operations such as ENQUEUE and DEQUEUE, and their representations using arrays and linked lists. It explains the conditions for queue overflow and underflow, along with algorithms for managing queue operations. Additionally, it introduces variations of queues including circular queues and deques, highlighting their unique characteristics and operational methods.

Uploaded by

kodurusailalitya
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)
12 views13 pages

Understanding Queue Data Structures

This document provides an overview of queues as a linear data structure, detailing operations such as ENQUEUE and DEQUEUE, and their representations using arrays and linked lists. It explains the conditions for queue overflow and underflow, along with algorithms for managing queue operations. Additionally, it introduces variations of queues including circular queues and deques, highlighting their unique characteristics and operational methods.

Uploaded by

kodurusailalitya
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

Unit - IV

Queues
Learning Material
Queue is a linear Data structure.
Definition: Queue is a collection of homogeneous data elements, where insertion and deletion
operations are performed at two extreme ends.
 The insertion operation in Queue is termed as ENQUEUE.
 The deletion operation in Queue is termed as DEQUEUE.
 An element present in queue is termed as ITEM.
 The number of elements that a queue can accommodate is termed as LENGTH of the Queue.
 In the Queue the ENQUEUE (insertion) operation is performed at REAR end and DEQUEUE
(deletion) operation is performed at FRONT end.
 Queue follows FIFO principle. i.e. First In First Out principle. i.e. a item First inserted into
Queue, that item only First deleted from Queue, so queue follows FIFO principle.

. . - - - . . .
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

1. Representation of Queue using arrays


A one dimensional array Q[1-N] can be used to represent a queue.
N-2

N-1

1 2 3
N

. . - - - . .

FRONT REAR

Data Structures Unit-IV Learning Material 1


Array representation of Queue
 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
2. Queue Full condition
Rear = = N-1 where N is the size of the array we are taken
3. Queue contains only one 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.
Queue Underflow condition is Front = = -1

Operation on Queue
1. ENQUEUE : To insert element in to Queue
2. DEQUEUE : To delete element from Queue
3. Status : To know present status of the Queue
Algorithm Enqueue(item)
Input: item is new item insert in to queue at rear end.
Output: Insertion of new item queue at rear end if queue is not full.
1. if(rear = = N-1)
a) print "queue is full, not possible for enqueue operation"
2. else
a) if(front = = -1 && rear = = -1) /* Queue is Empty */
i) rear = rear+1
ii) Q[rear] = item
iii) front = 0
b) else
i) rear = rear+1
ii) Q[rear] = item
c) end if
3. end if

Data Structures Unit-IV Learning Material 2


End Enqueue
While performing ENQUEUE operation two situations are occur.
1. if queue is empty, then newly inserting element becomes first element and last element in
the queue. So Front and Rear points to first element in the list.
2. If Queue is not empty, then newly inserting element is inserted at Rear end.

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 && rear = = -1)
a) print "Q is empty, not possible for dequeue operation"
2. else
a) if(front = = rear) /* Q has only one element */
i) item = Q[front]
ii) front = -1
iii) rear = -1
b) else
i) item = Q[front]
ii) front = front+1
c) end if
d) print "deleted item is" item
3. end if
End Dequeue

While performing DEQUEUE operation two situations are occur.


1. If queue has only one element, then after deletion of that element Queue becomes empty.
So Front and Rear becomes -1.
2. If Queue has more than one element, then first element is deleted at Front end.

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 && rear = = -1)
a) print "Queue is empty"
2. else if (rear = = size-1 )
a) print "Queue is full"
3. else

Data Structures Unit-IV Learning Material 3


a) if(front = = rear)
i) print "Queue has only one item"
b) else
i) print "element at front end is" Q[front]
ii) print "element at rear end is" Q[rear]
c) end if
4. end if
End Queue_Status

2. Representation of Queue using Linked List


 Array representation of Queue has static memory allocation only.
 To overcome the static memory allocation problem, Queue can be represented using Linked
List.
header N1 N2 N3 N4

X X

Front Rear
Linked List Representation of Queue

 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.

The Linked List representation of Queue stated as follows.


1. Empty Queue condition is
Front = = NULL and Rear = = NULL or headerlink = = NULL
2. Queue full condition is not available in Linked List representation of Queue, because in Linked List
representation memory is allocated dynamically.
3. Queue has only one element
Front = = Rear

Operation on Linked List Representation of Queue


1. ENQUEUE : To insert element in to Queue
2. DEQUEUE : To delete element from Queue
3. Status : To know present status of the Queue

Data Structures Unit-IV Learning Material 4


Algorithm Enqueue _LL(item)
Input: item is new item to be insert.
Output: new item i.e new node is inserted at rear end.
1. new = getnewnode()
2. if(new = = NULL)
a) print "required node is not available in memory"
3. else
a) if(front = = NULL && rear = = NULL) /* Q is EMPTY */
i) headerlink = new
ii) newlink = NULL
iii) front = new
iv) rear = new
v) newdata = item
b) else /* Q is not EMPTY */
i) rearlink = new /* 1 */
ii) newlink = NULL /* 2 */
iii) rear = new /* 3 */
iv) newdata = item
c) end if
4. end if
End_Enqueue_LL
While performing ENQUEUE operation two situations are occur.
1. if queue is empty, then newly inserting element becomes first node and last node in the queue.
So Front and Rear points to first node in the list.
2. If Queue is not empty, then newly inserting node is inserted at last.
header N1 N2 N3 new

X X
1
2
Front Rear NULL
3
Before ENQUEUE Rear
header N1 N2 N3 new

X X

Front Rear
After ENQUEUE

Data Structures Unit-IV Learning Material 5


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 && rear = = NULL)
a) print "queue is empty, not possible to perform dequeue operation"
2. else
a) if(front = = rear) /* Q has only one element */
i) headerlink = NULL
ii) item = frontdata
iii) front = NULL
iv) rear = NULL
b) else /* Q has more than one element */
i) headerlink = frontlink /* 1 */
ii) item = frontdata
iii) free(front)
iv) front = headerlink /* 2 */
c) end if
d) print "deleted element is item"
3. end if
End_Dequeue_LL

While performing DEQUEUE operation two situations are occur.


1. If queue has only one element, then after deletion of that element Queue becomes empty.
So Front and Rear points to NULL.
2. If Queue has more than one element, then first node is deleted at Front end.

Data Structures Unit-IV Learning Material 6


1

header N1 N2 N3 N4

X X
2
Front Rear
Front

Before DEQUEUE

header N2 N3 N4

X X

Front
Rear
After DEQUEUE

1. Link part of the header node is replaced with address of second node. i.e. address of second
node is available in link part of first node.
2. Front is set to first node in the list.

Algorithm Queue_Status_LL
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 = = NULL && rear = = NULL)
a) print "Q is empty"
2. else if(front = = rear)
a) print "Q has only one item"
3. else
a) print "element at front end is" frontdata
b) print "element at rear end is" reardata
4. end if
End Queue_Status_LL

Various Queue Structures


1. Circular Queues
2. DEQue
3. Priority Queue
4.

Data Structures Unit-IV Learning Material 7


1. Circular Queues
 Physically a circular array is same as ordinary array, say a[1-N], but logically it implements
that a[1] comes after a[N] or a[N] comes after a[1].
 The following figure shows the physical and logical representation for circular array.

Front Rear

n-1
n
1
2
j
1 j i n i

Circular Array (Physical) Front

Rear
Circular Queue (Logical)

Logical and physical view of a Circular Queue


 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 mod LENTH) +1, 1<= i <= Length
Circular Queue empty condition is
Front = = 0 && Rear = = 0
Circular Queue is full
Front = = (Rear % Length) + 1

Data Structures Unit-IV Learning Material 8


Algorithm CQ_Enqueue(item)
Input: item is new element insert in to Circular queue at rear end.
Output: Insertion of new item in Circular queue at rear end if queue is not full.
1. next = (REAR % N) + 1
2. if( FRONT == next)
a) print(“Circular queue is full, enqueue operation is not possible”)
3. else
i) if(FRONT==0 and REAR==0) /* CQ is Empty */
a) REAR=(REAR % N) +1
b) CQ[REAR]=item
c) FRONT=1
ii) else
a) REAR=(REAR% N) + 1
b) CQ[REAR]=item
iii) end if
4. endif
End CQ_Enqueue

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= =0 and REAR= =0)
a) print(“Circular Queue is empty, dequeue operation is not possible”)
2. else
i) if(FRONT = =REAR) /* Q has only one element */
a) item=CQ[FRONT]
b) FRONT=0
c) REAR=0
ii) else
a) item=CQ[FRONT]
b) FRONT=(FRONT % N)+1
iii) End if
iv) print(“deleted item is ‘item’ “)
3. end if
End CQ_Dequeue

Data Structures Unit-IV Learning Material 9


2. Deque:
 It is one of the Queue variant. It is also called as ‘deck’.
 In this both insertion and deletion operations are performed at both the ends of structure.
 The term deque is originated from double ended queue.

 Deque structure is general representation of both queue and stack. That means the deque can be
used as a queue as well as a stack.
 We can implement deque by using double linked list or by circular array (as in circular queue).
 Operations on deque:
1. Push_DQ(ITEM): To insert ITEM at the FRONT end of a deque.
2. Pop_DQ(): To remove the FRONT ITEM of a deque.
3. Inject(ITEM): To insert ITEM at the REAR end of a deque.
4. Eject(): To remove the REAR ITEM of a deque.
 Operations of deque based on circular array of size LENGTH. Let the array be
DQ[1……LENGTH].

Algorithm for Push_DQ(ITEM):

Input: ITEM to be inserted at the FRONT

Output: deque with newly inserted element ITEM if it is empty.

Data structures: circular array representation of deque.

If(FRONT ==1) then //If FRONT is at extreme left

Ahead=LENGTH;

Else //If deque is empty

If((REAR==0) or (FRONT==0)) then

Ahead=1;

REAR=1;

Else

If(FRONT==LENGTH) //If FRONT is at extreme right

Ahead=1;

Else

Data Structures Unit-IV Learning Material 10


Ahead=FRONT-1 //If FRONT is at an intermediate position

If(Ahead==REAR) then

Print “Deque is full”;

Exit;

Else

FRONT=Ahead;

DQ[FRONT]=ITEM; //PUSH the ITEM

Stop;

Algorithm for Pop_DQ():

Input: A DQ with elements. Two pointers FRONT and REAR are known.

Output: The deleted element is ITEM if the DQ is not empty.

Data structures: circular array representation of deque.

/* this algorithm is same as the algorithm DECQUEUE */

If(FRONT ==0) then

Print “deque is empty”

Exit;

Else

ITEM=DQ[FRONT]

If(FRONT==REAR) then //If DQ consists single element

FRONT=0;

REAR=0;

Else

FRONT=(FRONT mod LENGTH)+1;

End if;

End if;

Stop;

Data Structures Unit-IV Learning Material 11


Algorithm for Inject(ITEM):

Input: ITEM to be inserted at the FRONT

Output: deque with newly inserted element ITEM if it is empty.

Data structures: circular array representation of deque.

/* this algorithm is same as the algorithm ENCQUEUE */

If (FRONT==0) then //when DQ is empty

FRONT=1;

REAR=1;

DQ[REAR]=ITEM;

Else // DQ is not empty

Next=(REAR mod LENGTH)+1;

If(Next != FRONT) then //if DQ is not full

REAR=Next;

DQ[REAR]=ITEM;

Else

Print “DQ is FULL”

End if;

End if;

Stop;

Algorithm for Eject():

Input: A DQ with elements. Two pointers FRONT and REAR are known.

Output: The deleted element is ITEM if the DQ is not empty.

Data structures: circular array representation of deque.

If(FRONT ==0) then

Print “deque is empty”

Exit;

Else

If(FRONT==REAR) then //If DQ consists single element

ITEM=DQ[REAR]

FRONT=0;

Data Structures Unit-IV Learning Material 12


REAR=0; //DQ becomes empty

Else

If (REAR == 1) then //REAR is at extreme left

ITEM=DQ[REAR];

REAR=LENGTH;

Else

If (REAR == LENGTH) then //REAR is at extreme right

ITEM=DQ[REAR];

REAR=1;

Types of Deque

 There are two variations of Deque known as


1. Input restricted Deque
2. Output restricted Deque
1. Input restricted Deque
 Here Deque allows insertion at one end (say REAR end) only, but allows deletion at
both ends.

Deletion

Deletion Insertion

FRONT REAR
Input restricted Deque
2. Output restricted Deque
 Here Deque allows deletion at one end (say FRONT end) only, but allows insertion at
both ends.

Insertion

Deletion Insertion

FRONT REAR
Output restricted DEQue

Data Structures Unit-IV Learning Material 13

Common questions

Powered by AI

In array-based queue implementations, queue overflow occurs when an ENQUEUE operation is attempted and the rear pointer has reached the maximum array index (N-1), indicating no more space is available for new items . Underflow occurs when a DEQUEUE operation is attempted on an empty queue, which is detected by the front and rear pointers both being -1 . Linked list-based implementations naturally mitigate overflow conditions because memory is dynamically allocated, meaning the queue can grow as needed until system memory is exhausted . Underflow is handled similarly, but like arrays, it involves checking if both front and rear pointers are NULL, indicating emptiness . Thus, linked lists dynamically adjust size, avoiding overflow issues inherent to fixed-size arrays .

Queue representation using arrays involves static memory allocation, meaning the size of the queue is fixed and cannot grow beyond a set limit . This can lead to overflow errors if the queue reaches its maximum capacity . Operations such as ENQUEUE and DEQUEUE have constant time complexity (O(1)) but checking for overflow or underflow conditions requires additional checks . In contrast, the linked list representation uses dynamic memory allocation, allowing the queue to grow as needed without a predefined limit, mitigating the risk of overflow . This makes linked lists more memory efficient as they do not allocate memory unless required. However, linked lists may have a slight overhead due to memory allocation and deallocation operations .

In an array-based queue, the queue is full when the rear index equals the size of the array minus one (Rear = N-1). This signifies that the queue cannot accept more elements unless some are removed from the front. In a circular queue, a queue is full when (Front = (Rear % Length) + 1). This condition arises because the rear pointer has wrapped around and is trying to overtake the front pointer, indicating there are no free slots. In terms of operation, if an ENQUEUE is attempted in a full array-based queue, it results in error indicating overflow . A circular queue's logic automatically resets indices using modulo operations, thus efficiently reusing the space and avoiding overflow by wrapping around the queue's end . Error handling differs as the circular queue requires the additional check of the frontal alignment to detect fullness .

A DEQUEUE operation changes the status of a queue from non-empty to empty when the queue has only one element—indicated by front and rear pointers being equal before the operation . After the DEQUEUE operation, both pointers are typically reset to -1 in array implementations or NULL in linked list implementations, signifying the queue's emptiness . This operation ensures that subsequent operations correctly interpret the queue as empty . The impact of this operation sets the stage for subsequent ENQUEUE operations to adjust the pointers correctly by recognizing these reset indicators as conditions for inserting the first new element in the now empty queue .

The decision between implementing a queue as a circular queue or a deque depends on the specific use case and operational requirements. Circular queues are ideal when space optimization is critical in a fixed-size array context since they reuse space when the queue elements wrap around . They are suitable for cyclic operations where the order of processing is strictly FIFO and operations at both ends aren't needed . In contrast, deques are preferable when flexibility in operation at both ends of the queue is required, such as allowing both FIFO and LIFO operations . Deques provide versatility in application, functioning as both queues and stacks, which is particularly useful in scenarios where predictable operation order isn't fixed, but operational access to both ends is necessary .

Linked-list implementations address the static memory allocation problem by utilizing dynamic memory allocation . Unlike array implementations with fixed size, linked lists allocate memory as needed for each node, thus not limiting the queue to a predetermined capacity . This allows the queue to grow as large as available system memory permits, eliminating the overflow errors associated with attempting to ENQUEUE in a full array . This dynamic characteristic ensures efficient memory usage and greater flexibility, particularly in applications where the number of elements in the queue is not known in advance or varies significantly .

The circular queue differs from a standard queue because both its front and rear pointers wrap around at the end of the array, creating a circular structure . Elements in a circular queue can be added and deleted in a manner where, after reaching the last position in the array, the next position to insert or delete is the initial position if it is free . This circular nature resolves the limitation of wasted space left in the queue when elements are removed from the front in a standard linear array representation of a queue, thus enabling the reuse of previously occupied positions and optimizing memory utilization .

A deque, or double-ended queue, allows insertion and deletion at both ends, unlike a traditional queue which restricts these operations to one end for each—adding at the rear and removing from the front . This dual operation capability makes deques more versatile, as they can function both as stacks and queues, allowing more complex data operations . The flexibility in the deque structure supports operations such as push at the front, pop from the front, inject at the rear, and eject from the rear, which traditional queues do not support . These operations facilitate various advanced algorithms where both ends of the sequence might need manipulation, thus providing a significant advantage in flexibility and utility .

An input restricted deque allows insertion at only one end (typically the rear) but permits deletion from both ends. This makes it ideal for situations where multiple consumer-like entities need to be managed, as it enables complex removal patterns while maintaining controlled insertion . In contrast, an output restricted deque permits deletion at only one end (often the front) but supports insertion at both ends, allowing for flexibility in adding elements while enforcing a specific removal order . Potential applications include scheduling systems, where input restricted deques can manage task priorities, and output restricted deques handling queue-like input while ensuring ordered processing .

Circular arrays ensure efficient queue operation by allowing rear and front pointers to wrap around the end of the array to the beginning, minimizing wasted space . This wrapping is managed by modulo arithmetic, where addition to a pointer beyond the last index results in restarting from the first index (e.g., Rear = (Rear % Length) + 1). This operation allows full utilization of the array despite its linear layout, resolving the issue of static memory underutilization common in linear arrays . As such, circular arrays efficiently handle cyclic operations, offering continuous memory use without shifting elements, leading to stable O(1) complexity for insertion and deletion .

You might also like