0% found this document useful (0 votes)
34 views

Unit Iii_queue Adt

Uploaded by

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

Unit Iii_queue Adt

Uploaded by

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

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

21CSC201J – Data Structures and Algorithms


Unit III- QUEUE

7/27/2023 SRM Institutue of Science and Technology 1


QUEUES

• A queue is a data structure that


models/enforces the first-come first-serve
order, or equivalently the first-in first-out
(FIFO) order.
What is a Queue???????????

• That is, the element that is inserted first into the


queue will be the element that will deleted first, and
the element that is inserted last is deleted last.
OPERATIONS OF A QUEUE
The Queue Operations

• A queue is like a line of people waiting for a


bank teller. The queue has a front and a
rear.

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

Module Create Queue


count = 0
front = -1
rear= -1
End Module Create Queue
enQueue(value) - Inserting value into the
queue

• Step1: Check whether queue is FULL. (rear


== SIZE-1)
• Step 2: If it is FULL, then display "Queue is
FULL!!! Insertion is not possible!!!" and
terminate the function.
• Step 3: If it is NOT FULL, then
increment rear value by one (rear++) and
set queue[rear] = value.
Array Implementation

• The easiest implementation also keeps


track of the number of items in the
queue and the index of the first element
3 size
(at the front of the queue), the last
element (at the rear).
0 first

2 last

[0] [1] [2] [3] [4] [5] ...

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

[0] [1] [2] [3] [4] [5] ...

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

• When an element enters the 3 size


queue, size is incremented, and
last changes, too. 1 first

3 last

[0] [1] [2] [3] [4] [5] ...

8 6 2
Queue Insertion

void enQueue(int value)


{
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not
possible!!!");
else
{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
deQueue() - Deleting a value from the Queue

• Step 1: Check whether queue is EMPTY. (front ==


rear)
• Step 2: If it is EMPTY, then display "Queue is
EMPTY!!! Deletion is not possible!!!" and
terminate the function.
• Step 3: If it is NOT EMPTY, then increment
the front value by one (front ++). Then
display queue[front] as deleted element. Then
check whether both front and rear are equal
(front == rear), if it TRUE, then set
both front and rear to '-1' (front = rear = -1).
A Dequeue Operation

• When an element leaves the queue, 2 size


size is decremented, and first changes,
too. 1 first

2 last

[0] [1] [2] [3] [4] [5] ...

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

• Step 1: Check whether queue is EMPTY.


(front == rear)
• Step 2: If it is EMPTY, then display "Queue
is EMPTY!!!" and terminate the function.
• Step 3: If it is NOT EMPTY, then define an
integer variable 'i' and set 'i = front'.
• Step 3: Display 'queue[i]' value and
increment 'i' value by one (i++). Repeat the
same until 'i' value is equal
to rear (i <= rear)
Queue Display

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

• Queue can be implemented using a one-


dimensional array.
• We need to keep track of 2 variables: front
and rear
• front is the index in which the first element is
present
• rear is the index in which the last element is
present
Enqueue in Array Implementation of Queue

//Basic
Time Complexity:
enqueue(num):
Idea O(1)
rear++
queue[rear]=nu
m return
Dequeue in Array Implementation of Queue

Time Complexity: O(1)


//Basic O(n) if elements are
Idea shifted
num =
dequeue():
queue[front]
front++
return num
Enqueue and Dequeue in Queue
Disadvantage of Array Implementation of
Queue

• Multiple enqueue and dequeue


operations may lead to situation like
this:

• No more insertion is possible (since rear


cannot be incremented anymore) even though
2 spaces are free.
• Workaround: Circular Queue, Shifting
elements after each dequeue
LINKED LIST REPRESENTATION OF QUEUES

• 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

✔ Neat trick: use a circular array to 6 1

insert and remove items from a queue


in constant time. 5 2

✔ The idea of a circular array is that the


3
end of the array “wraps around” to the 4

start of the array.

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

7/27/2023 SRM Institutue of Science and Technology 27


Operation on Linked Queue

There are two basic operations which can be implemented on


the linked queues. The operations are Insertion and Deletion.

Algorithm for Insertion


Step 1: Allocate the space for the new node PTR
Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF] 28
Step 4: END
Algorithm for Deletion

Step 1: IF FRONT = NULL


Write " Underflow "
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END

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

You might also like