Unit Iii_queue Adt
Unit Iii_queue Adt
Front (Head)
Rear (Tail)
Operations on Queues
• Insert(item): (also called enqueue)
– It adds a new item to the tail of the queue
• Remove( ): (also called delete or dequeue)
– It deletes the head item of the queue, and
returns to the caller. If the queue is already
empty, this operation returns NULL
• getHead( ):
– Returns the value in the head element of the
queue
• getTail( ):
– Returns the value in the tail element of the
queue
• isEmpty( )
– Returns true if the queue has no items
• size( )
– Returns the number of items in the queue
Creation of Queue
2 last
4 8 6
Array Implementation
• A queue can be implemented with an array, as
shown here. For example, this queue contains the
integers 4 (at the front), 8 and 6 (at the rear).
4 8 6
An array of integers
to implement a We don't care what's in
queue of integers this part of the array.
An Enqueue Operation
3 last
8 6 2
Queue Insertion
2 last
4 8 6
Queue Deletion
void deQueue()
{
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not
possible!!!");
else
{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
display() - Displays the elements of a Queue
void display()
{
if(rear == -1)
printf("\nQueue is Empty!!!");
Else
{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++) printf("%d\t",queue[i]);
}
}
Array Implementation of Queues
//Basic
Time Complexity:
enqueue(num):
Idea O(1)
rear++
queue[rear]=nu
m return
Dequeue in Array Implementation of Queue
• The array implementation cannot be used for the large scale applications
where the queues are implemented. One of the alternative of array
implementation is linked list implementation of queue.
• In a linked queue, each node of the queue consists of two parts i.e. data
part and the link part. Each element of the queue points to its immediate
next element in the memory.
• In the linked queue, there are two pointers maintained in the memory i.e.
front pointer and rear pointer. The front pointer contains the address of
the starting element of the queue while the rear pointer contains the
address of the last element of the queue.
• Insertion and deletions are performed at rear and front end respectively. If
front and rear both are NULL, it indicates that the queue is empty.
LINKED LIST REPRESENTATION OF QUEUES
Operations on Linked
The linked representation of queue is
shown in the following figure.
Queue
• There are two
operations which can
basic
be implemented on the
linked queues.
• The operations are
Insertion and Deletion.
Implementing Queue ADT:
Circular Array Queue
7 0
Q: 0 size - 1
b c d e f
fron back
t
7/27/2023 SRM Institutue of Science and Technology 25
Circular Array Queue
Q: 0 size - 1
b c d e f
fron back
t
// Basic idea!
enqueue(x) {
Q[back] = x;
back = (back + 1) % size
}
/ Basic idea!
dequeue() {
x = Q[front];
front = (front + 1) % size;
return x;
} 7/27/2023 SRM Institutue of Science and Technology 26
Exercise: Linked List Queue
Implementation
• Implement a queue class that stores String values using a
singly linked list with both nodes to indicate the front
and the back of the queue as below. The queue should
implement the interface on the next slide.
b c d e f
fron back
t
29
Sample Question
1.How many stacks are required to implement a queue? Where no other data structure like
arrays or linked lists is available.
• 1
• 2
• 3
• 4
Solution: b. 2.
2. Is the statement true about linked list implementation of the Queue?
• In a push operation, if new nodes are inserted at the beginning of the linked list, then in pop
operation and nodes must be removed from the end.
• In a push operation, if new nodes are inserted at the end, then in pop operation, nodes must be
removed from the beginning.
• Both of the above
• None of the above
Solution: c. Both of the above
As First In First Out order, a queue can be implemented using a linked list in any of the given two
ways.
30