0% found this document useful (0 votes)
30 views58 pages

DS - Lecture 06 - Queues

Queues

Uploaded by

Virat D
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)
30 views58 pages

DS - Lecture 06 - Queues

Queues

Uploaded by

Virat D
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
You are on page 1/ 58

DATA STRUCTURES

(CS3401)

Dr. Somaraju Suvvari,


Asst. Prof, Dept. of CSE, NIT Patna.
[email protected];
[email protected];

Dr Somaraju Suvvari NITP -- CS3401 1


The Course

DATA STRUCTURES

Dr Somaraju Suvvari
2
NITP -- CS3401
QUEUES

Dr Somaraju Suvvari
3
NITP -- CS3401
UNIT IV: Queues

Basics of Queue data structure, Implementation of queue using array and linked list,
Operations on queues, Types of queues – queue, double ended queue, priority queue
and Implementation of these.

Dr Somaraju Suvvari
4
NITP -- CS3401
Linear data structure

Queue Deletions insertions

• Queue is also a linear data structure in which the insertion and deletion operations are performed at two
different ends as it is opposed to stacks.

• The insertion is performed at one end (Called as enqueue) and deletion is performed at the other end
(called as dequeue).

• To perform insertion and deletion we used two pointers rear and front.

• Insertion operation is performed at a position pointed by 'rear' and the deletion operation is performed
at a position pointed by 'front'.

• In queue data structure, the insertion and deletion operations are performed based on FIFO (First In
First Out) principle.

Dr Somaraju Suvvari
5
NITP -- CS3401
Queue (Example)
Initially, rear = -1 and front = -1

Queue is empty
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Inserting after first element Rear = Front = 0
rear
10
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
front
Inserting after last element Rear=Max-1 Queue is Full Rear

10 30 40 20 80 50 40 60 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Front Dr Somaraju Suvvari
6
NITP -- CS3401
Queue (Example for Deletion)
Rear

10 30 40 20 80 50 40 60 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Rear Delete
Front {10} is deleted

30 40 20 80 50 40 60 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Deleted elements are


Front Rear {10, 30, 40, 20, 80, 50, 40,
60, 90}
70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] It follows FIFO First In
First Out policy.
Dr Somaraju Suvvari
NITP -- CS3401 Front 7
Types of Queue
1. Simple or Normal Queue - Where the insertion will happen from position zero
to position Max. We cannot use the deleted
Front positions to insert elements again. Rear

30 40 20 80 50 40 60 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

2. Circular Queue - Last location is connected to the first location, when an


element is inserted in the last position and if the first location
is free then we can insert another element in the first location.
Rear
c[4]

c[3] c[0]

c[2] c[1]
Front
Dr Somaraju Suvvari
8
NITP -- CS3401
Types of Queue

1. Deque (Doubly Ended Queue) - It is a Queue where the insertion and deletion
operations are performed at both ends.
Front - 1 Rear-1 Rear-2 Front - 2

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

2. Priority Queue – In priority queues the items are associated with a value called
priority. When you perform delete, first delete the highest
Front priority one before deleting any lower priority item.
PRIORITY Rear
30 (10) 40 (9) 20 (8) 80 (7) 50 (5) 40 (4) 60 (2) 90 (1) 70 (0)

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Dr Somaraju Suvvari
9
NITP -- CS3401
Operations on a Queue
• The following mandatory operations are performed on a queue data structure...

• enQueue(value) - To insert an element into the queue.


• deQueue() - To delete an element from the queue.

• The following Auxilary operations are performed on a queue data


structure...
• display() - To display the elements of the queue

Dr Somaraju Suvvari
10
NITP -- CS3401
APPLICATIONS OF QUEUE

• Queues are widely used as waiting lists for a single shared resource like printer,
disk, CPU, scanner, etc.

• Queues are used to transfer data asynchronously (data not necessarily received at
same rate as sent) between two processes (IO buffers), e.g., pipes, fle IO, sockets.

• Queues are used as buffers on MP3 players and portable CD players, iPod playlist.

• Queues are used in Playlist for jukebox to add songs to the end, play from the
front of the list.

Dr Somaraju Suvvari
11
NITP -- CS3401
APPLICATIONS OF QUEUE

• Queues are used in operating system for handling interrupts. When programming a
real-time system that can be interrupted; for example, by a mouse click, it is
necessary to process the interrupts immediately, before proceeding with the
current job. If the interrupts have to be handled in the order of arrival, then a FIFO
queue is the appropriate data structure.

• Used as auxiliary data structure for algorithms, (for example in Breadth First
Algorithm queue is used as an auxiliary data structure).

Dr Somaraju Suvvari
12
NITP -- CS3401
Implementation of Queue

Queue data structure can be implemented in two ways. They are as follows...

• Using Array

• Using Linked List

Dr Somaraju Suvvari
13
NITP -- CS3401
Implementation of Simple Queue Using Arrays

Dr Somaraju Suvvari
14
NITP -- CS3401
Simple Queue ADT (Array Based)
// Define the queue

#define MAX 100

Element Type Queue[MAX];

int front = -1, rear = -1;

// Define the set of operations on queue

void enQueu(Element Type[], int, int, Element Type);

Element Type deQueue(Element Type[], int, int);

void Display(Element Type [], int, int);


Dr Somaraju Suvvari
15
NITP -- CS3401
enQueue
• enQueue(Queue, front, rear, Element Type) - Inserting value into the queue
step - 1 : IF rear = Max-1
• In a queue, the new element is always inserted at rear position. Display “ Queue Full”
Go to step-4
• We can use the following steps to insert an element into the queue... step – 2: IF front = -1
SET front = 0
• Step 1 - Check whether queue is FULL. Step – 3: rear++
queue[rear] = value
(rear == SIZE-1) Step - 4: End

• Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
•Step 3 - If rear = -1 then set rear =0, front = 0; otherwise increment rear value by one.
•Step 4 - set queue[rear] = value.
Time Complexity = O(1)
Dr Somaraju Suvvari
16
NITP -- CS3401
enQueue Example
The list of elements to be inserted into Queue : 10, 30, 40, 70, 50

Initially Rear = -1 Front = -1


[0] [1] [2] [3]
enQueue(queue, front, rear, 10)
Queue is empty, for insertion, both rear and front are set to position 0 for first element
Rear Rear
Queue[rear]=10

10
[0] [1] [2] [3] [0] [1] [2] [3]

Front Front

Dr Somaraju Suvvari
17
NITP -- CS3401
enQueue Example
The list of elements to be inserted into Queue : 10, 30, 40, 70, 50

Rear

10
[0] [1] [2] [3]

Front
enQueue(queue, front, rear, 30) Queue[rear]=30 enQueue(queue, front, rear, 40)
rear=rear+1 rear=rear+1 Rear
Queue[rear]=40
Rear Rear
10 30 40
10 10 30
[0] [1] [2] [3]
[0] [1] [2] [3] [0] [1] [2] [3]
Front
Front Front

Dr Somaraju Suvvari
18
NITP -- CS3401
enQueue Example
The list of elements to be inserted into Queue : 10, 30, 40, 70, 50
Rear

10 30 40
[0] [1] [2] [3]

Front
enQueue(queue, front, rear, 70) Queue[rear]=70 enQueue(queue, front, rear, 50)
rear=rear+1 Rear
Rear Rear
10 30 40 70
10 30 40 10 30 40 70
[0] [1] [2] [3]
[0] [1] [2] [3] [0] [1] [2] [3]
Front
Front Front
Queue is FULL
Dr Somaraju Suvvari
19
NITP -- CS3401
step - 1 : IF front = -1
deQueue() Display “ Queue Empty”
Go to step-4
step – 2: ele = queue[front]
deQueue (Queue, int, int) – Deletes the element from the queue Step – 3: front++
In a queue, the element is always deleted from front position. queue[rear] = value
Step - 4: if front > rear then set
We can use the following steps to delete an element from the queue... front and rear to -1.
Step 1 - Check whether queue is EMPTY. (front == rear == - 1)
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 display queue[front] as deleted element.
Now increment the front value by one (front++).
Step 4 - Check if front > rear, if it is TRUE, then set both front and rear to '-1' (front = rear = -1).

Time Complexity = O(1)

Dr Somaraju Suvvari
20
NITP -- CS3401
Dequeue
deQueue(queue, front, rear) Rear deQueue(queue, front, rear) Rear
Rear {10} is deleted {30} is deleted
10 30 40 20 30 40 20 40 20
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]

Front
Front Rear Front Rear
deQueue(queue, front, rear)
{20} is deleted deQueue(queue, front, rear)
20
{40} is deleted
[0] [1] [2] [3] [0] [1] [2] [3]

Front Front
Rear
Deleted elements are { 10, 30, 40, 20}
FIFO-Firs In First Out
deQueue(queue, front, rear)
[0] [1] [2] [3]
Front Queue EmptySuvvari
Dr Somaraju
21
NITP -- CS3401
display()
• display(Queue, int, int) - Displays the elements of a Queue
• We can use the following steps to display the elements of a queue...

Step 1 - Check whether queue is EMPTY. (front == rear==-1)

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 4 - Display 'queue[i]' value and increment 'i' value by one (i++).

Step 5 - Repeat the step 4 until 'i' value reaches to rear (i <= rear).
Time Complexity = O(n)
Dr Somaraju Suvvari
22
NITP -- CS3401
Simple Queue using Array program

// Declarations (Assuming only one queue and its parameters are declared globally

#include <stdio.h>
#include <conio.h>
#define MAX 10 // Size of the queue
int queue[MAX];
int front = -1, rear = -1; // queue is empty
void enQueue(int); // For insertion
int deQueue(void); // For deletion
void display(void);

Dr Somaraju Suvvari
23
NITP -- CS3401
Queue using Array program (contd..)

case 2:
int main() v = deQueue();
{ int option, v, n; if (v != -1)
while(1) printf(“\n The number deleted is : %d”, v);
{ printf(“\n\n MAIN MENU ”); break;
printf(“\n 1. Insert an element”); case 3: display();
printf(“\n 2. Delete an element”); break;
printf(“\n 3. Display the queue”); case 5: exit(0);
printf(“\n 4. EXIT”); }
printf(“\n Enter your option : “); } // while(1);
scanf(“%d”,&option); getch();
switch(option) return 0;
{ }
case 1:printf(“\nEnter the number
to be inserted in the
queue :”);
scanf(“%d”, &n);
enQueue(n);
Dr Somaraju Suvvari
break; NITP -- CS3401
24
Queue using Array program (contd..)
int deQueue()
{ int val;
void enQueue(int num) if(front == -1 )
{ printf(“\n Enter the number to be inserted in the queue : “); { printf(“\n Queue is Empty”);
scanf(“%d”, &num);
return -1;
if(rear == MAX-1)
printf(“\n Queue is FULL”); }
else if(front == -1 && rear == -1) else
front = rear = 0; { val = queue[front];
else rear++; front++;
queue[rear] = num; if(front > rear)
} front = rear = -1;
return val;
void display() }
{ int i; }
if(front == -1)
printf(“\n QUEUE IS EMPTY”);
else
{ for(i = front; i <= rear; i++)
printf(“\t %d”, queue[i]);
}
Dr Somaraju Suvvari
} NITP -- CS3401
25
Implementation of Circular Queue Using Arrays

Dr Somaraju Suvvari
26
NITP -- CS3401
Rear
Circular Queue
30 40 20 80 50 40 60 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Front
• For example if we want to insert an element value is 100.

• Since rear==Max(9), it is not possible to insert element, though empty slots are
available.

• To address this issue, circular queue is used, where the elements are connected
in a circular manner as shown in fig.

• A circular queue is a linear data structure follows FIFO (First In First


Out) principle and the last location is connected back to the first location
to make a circle.
Dr Somaraju Suvvari
27
NITP -- CS3401
Conditions : queue is full and empty

• Queue is empty
front = rear =-1
front
rear
• Queue is full
front = = 0 and rear == MAX – 1
or
front==rear+1 rear Queue is Full
front

Queue is Full
Dr Somaraju Suvvari
28
NITP -- CS3401
Operations

• enQueue

• Insert element into queue at rear position

• deQueue

• To delete an element from front position of queue

• Display

• To display the list of elements from front to rear ends.

Dr Somaraju Suvvari
29
NITP -- CS3401
enQueue
step - 1 : IF ( (front = 0 and rear = MAX-1)
or front = rear-1 )
• The enQueue() function inserts that value into the circular queue. Display “ Queue Full”
Go to step-4
• We can use the following steps to insert an element into the circular step – 2: if rear = size-1 then set rear = 0,
queue... otherwise rear = rear +1
Step – 3: queue[rear] = value. If front = -1
Step 1 - Check whether queue is FULL. then set front =0.
( (rear == SIZE-1 && front == 0) || (front == rear+1) ) Step - 4: End

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 check if rear == SIZE - 1 then
set rear = 0 otherwise increment rear. // rear = (rear + 1) %size
Step 4 - set queue[rear] = value
Step 5 – Check if 'front == -1' if it is TRUE, then set front = 0.

Time Complexity = O(1)

Dr Somaraju Suvvari
30
NITP -- CS3401
deQueue
step - 1 : IF ( (front = -1 and rear = -1)
Display “ Queue Full”
• The deQueue() function deletes the first inserted element from the circular Go to step-4
step – 2: Assign queue[front] to ele.
queue.
Step – 3: if front = rear then set front =
• We can use the following steps to delete an element from the circular queue... rear = -1.
if front = Size-1 then set front = 0.
Step 1 - Check whether queue is Empty. Step - 4: End
(rear == -1 && front -1)
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 display queue[front] as deleted element .

Step 4 - Check whether both front and rear are equal (front == rear), if
it TRUE, then set both front and rear to '-1'.
Check whether front == SIZE-1, if it is TRUE, then set front = 0.

Time Complexity = O(1)

Dr Somaraju Suvvari
31
NITP -- CS3401
enQueue enQueue(queue, front, rear, 10)
2
1 Initially

rear
Front=-1
10
Rear=-1 front [0] [1] [2] [3]

[0] [1] [2] [3]

After calling deQueue two times


After calling enQueue on 20, 30, 40
3 rear
4 rear
10 20 30 40
30 40
[0] [1] [2] [3]
[0] [1] [2] [3]
front front

Dr Somaraju Suvvari
32
NITP -- CS3401
enQueue
After doing two dequeue operations and one enque operation
5 enQueue(queue, front, rear, 50)
6
rear

30 40 rear
[0] [1] [2] [3]
front
rear 50 60
50 30 40 [0] [1] [2] [3]
[0] [1] [2] [3] front
front

Dr Somaraju Suvvari
33
NITP -- CS3401
Queue is empty rear
Rear=-1
Front=-1 50 40
[0] [1] [2] [3] [0] [1] [2] [3]
deQueue
rear
front

10 20 30 40 rear
[0] [1] [2] [3] deQueue
front 50
rear [0] [1] [2] [3]
front
deQueue
20 30 40
[0] [1] [2] [3] rear
front
deQueue
rear
[0] [1] [2] [3]
30 40
front rear
[0] [1] [2] [3]
front -1

Dr Somaraju Suvvari front [0] [1] [2] [3]


34
NITP -- CS3401
display() front rear

10 20 30 40
• We can use the following steps to display the elements of a circular queue... [0] [1] [2] [3]
• Step 1 - Check whether queue is EMPTY (front == -1)
• 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'.
rear front
• Step 4 - Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and
increment 'i' value by one (i++). Repeat the same until 'i <= rear’
becomes FALSE. 80 60 30 40
• Step 5 - If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value [0] [1] [2] [3]
by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.
• Step 6 - Set i to 0.
• Step 7 - Again display 'Queue[i]' value and increment i value by one (i++). Repeat the
same until 'i <= rear' becomes FALSE.

Time Complexity = O(n)

Dr Somaraju Suvvari
35
NITP -- CS3401
Implementation of Double ended queue Using Arrays

Dr Somaraju Suvvari
36
NITP -- CS3401
Double Ended Queue
Dequeue - A queue where both the insertions and deletion operations are
performed in both the ends.

There are many ways to implement the deque, some of the implementations may not
follow the principle of FIFO. We are following the implementation where it follows
the FIFO principle with respective to in which way we inserted the elements. We
use four pointers, two referring at one end and the other two referring at the other
end.

Front - 1 Rear-1 Rear-2 Front - 2

12 24 56 23 98 67 43
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Dr Somaraju Suvvari
37
NITP -- CS3401
DeQueue ADT (Array Based)
// Define the queue

#define MAX 100

Element Type deque[MAX];

int front1 = -1, rear1 = -1, front2 = MAX, rear2 = MAX;

// Define the set of operations on queue

void Enqueue(Element Type[], int, int, int, Element Type); // front1, rear1

Element Type Dequeue(Element Type[], int, int); // fonr1, rear1

void Inject(Element Type[], int, int, int, Element Type); // front2, rear2,

Element Type Eject(Element Type[], int, int); // front1, rear1

Dr Somaraju Suvvari
38
NITP -- CS3401
Enqueue
• enQueue(dequeue, front1, rear1, rear2, value) - Inserting value into the queue from
the beginning
• We can use the following steps to insert an element into the queue...
• Step 1 - Check whether queue is FULL.
(rear1 == rear2-1)
• Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
•Step 3 - If rear1 = -1 then set rear1 =0, front1 = 0; otherwise increment rear1 value by one
•Step 4 - set dequeue[rear1] = value.
Time Complexity = O(1)

Dr Somaraju Suvvari
39
NITP -- CS3401
Inject
• Inject(deque, front2, rear2, front1, value) - Inserting value into the dequeue from
the ending
• We can use the following steps to insert an element into the queue...
• Step 1 - Check whether queue is FULL.
(rear1 == rear2 - 1)
• Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
•Step 3 - If rear2 = MAX then set rear2 = MAX-1, front2 = MAX-1; otherwise decrement
rear2 by one.
Time Complexity = O(1)
•Step 4 - set dequeue[rear2] = value;
Dr Somaraju Suvvari
40
. NITP -- CS3401
Deque
• Dequeue(deque, front1, rear1) - Deleting a value from the queue beginning.
• We can use the following steps to insert an element into the queue...
• Step 1 - Check whether queue is EMPTY from the beginning.
(front1 == -1)
• 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 Display the deque[front1] as deleted element.
•Step 4 - if rear1 == front1, set rear1=-1 and front1=-1; otherwise increment front1.

Time Complexity = O(1)

Dr Somaraju Suvvari
41
NITP -- CS3401
Eject
• Eject(deque, front2, rear2) - Deleting a value from the queue beginning.
• We can use the following steps to insert an element into the queue...
• Step 1 - Check whether queue is EMPTY from the ending.
(front2 == MAX)
• 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 Display the deque[front2] as deleted element.
•Step 4 - if rear2==front2, set rear2= MAX and front2=MAX; otherwise decrement front2.

Time Complexity = O(1)

Dr Somaraju Suvvari
42
NITP -- CS3401
Dequeue - Example
Initially
Front1 = -1 Front2 = 10

Rear1 = -1 Rear2 = 10 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Enqueue(12)
Front1
Front1 = 0, Rear1 = 0
12
Deque[rear1] = 12 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Rear1
Enqueue(23)
Front1 Rear1
Rear1 = 1
12 23
Deque[rear1] = 23
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Dr Somaraju Suvvari
43
NITP -- CS3401
Dequeue - Example
Front1 Rear1

12 23
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Inject(45)
Front1 Rear1 Front2
Front2 = 9
Rear2 = 9
12 23 45
Deque[Rear2] = 45
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Inject(5) Rear2
Front1 Rear1 Rear2 Front2
Rear2 = 8

Deque[Rear2] = 5 12 23 5 45
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Dr Somaraju Suvvari
44
NITP -- CS3401
Dequeue - Example
Front1 Rear1 Rear2 Front2

12 23 5 45
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Rear1 Rear2 Front2
Deque()
Front1++ 23 5 45
Deleted Element is 12 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Front1

Rear1 Rear2
Eject()
Front2— 23 5
Deleted Element is 45
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Front1 Front2
Dr Somaraju Suvvari
45
NITP -- CS3401
Dequeue - Example
Rear1 Rear2

23 5
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Front1 Front2
Rear2
Deque()
Front1 = Rear1 = -1 5
Deleted Element is 23
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Front2

Eject()
Front2 = MAX, Rear2 = MAX
Deleted Element is 5 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Dr Somaraju Suvvari
46
NITP -- CS3401
Implementation of Simple Queue Using Singly Linked List

Dr Somaraju Suvvari
47
NITP -- CS3401
Simple Queue ADT (Linked List Based)
// Define the queue
typedef struct QUEUE Q;
struct QUEUE
{ Element Type data;
Q *next;
}*front = NULL, *rear=NULL;

Element Type Queue[MAX];

// Define the set of operations on queue

void enQueu(Q *, Q*, Element Type); // front, rear, Element Type

Element Type deQueue(Q *, Q*); //front, rear,

void Display(Q *,Q *);

Dr Somaraju Suvvari
48
NITP -- CS3401
enQueue
• enQueue(Q *front, Q *rear, Element Type value) –
• Inserting value into the queue
• We can use the following steps to insert an element into the queue...
Step 1 - create a new_node. If new_node creation failed, then display “Queue is FULL!!!
Insertion is not possible!!!" and terminate the function.
Step 2 - If Queue is NOT FULL, then insert the value in the new_node and NULL to
new_node → next .
Step 3 – If rear == NULL, then assign new_node to rear and front.
Step 4 – If rear != NULL, then assign new_node to rear→next also assign new_node
to rear. Time Complexity = O(1)
Dr Somaraju Suvvari
49
NITP -- CS3401
deQueue
• deQueue(Q *front, Q *rear) –
• Deleting a value from the dequeue
• We can use the following steps to insert an element into the queue...
Step 1 – Check if Queue is EMPTY.
front = NULL.
Step 2 - If Queue is NOT EMPTY, then create temp pointer and assign front to temp.
Step 3 – Display the front→data as deleted element.
Step 4 – If rear = = front, then assign NULL to both rear and front; otherwise assign
front→next to front; Free the temp;

Time Complexity = O(1)


Dr Somaraju Suvvari
50
NITP -- CS3401
Implementation of Deque Using Singly Linked List

Dr Somaraju Suvvari
51
NITP -- CS3401
Deque ADT (Linked List Based)
Two enqueue operations and one Inject operation
// Define the queue front-1 rear-1 rear-2
typedef struct QUEUE DQ;
struct QUEUE 20 x3000 30 x1000 10 NULL
{ Element Type data; x2000 x3000 x1000
Q *next;
}*front1 = NULL, *rear1=NULL, *front2 = NULL, *rear2 = NULL; front-2

Element Type Queue[MAX];

// Define the set of operations on queue

void enQueu(Q *, Q*, Q *, Element Type); // front-1, rear-1, front-2, inserting element

Element Type deQueue(Q *, Q *); //front-1, rear-1

void Inject(Q *, Q*, Element Type); // front-2, rear-2, front-1, Element Type

Element Type Eject(Q *, Q*, Q*); //front-2, rear-2, rear-1

Dr Somaraju Suvvari
52
NITP -- CS3401
enQueue
• enQueue(Q *front-1, Q *rear-1,Q *font-2, Element Type value) –
• Inserting value into the Dequeue
• We can use the following steps to insert an element into the Deque from front...
Step 1 - create a new_node. If new_node creation failed, then display “Queue is FULL!!!
Insertion is not possible!!!" and terminate the function.
Step 2 - If Queue is NOT FULL, then insert the value in the new_node and NULL to
new_node → next.
Step 3 – If rear-1 == NULL, then assign new_node to rear-1 and front-1 and assign front-2 to
new_node→next.
Step 4 –If rear-1 != NULL, then assign rear-1→next to new_node→next, new_node to rear-1→next and
update rear-1 with new_node.
Time Complexity = O(1)
Dr Somaraju Suvvari
53
NITP -- CS3401
deQueue
• deQueue(Q* *front-1, Q *rear-1) –
• Deleting a value from the dequeue
• We can use the following steps to delete an element from deque and from front..
Step 1 – Check if Queue is EMPTY.
front-1 = NULL.
Step 2 - If Queue is NOT EMPTY, then create temp pointer and assign front-1 to temp.
Step 3 – Display the front-1→data as deleted element.
Step 4 – If rear-1 = = front, then assign NULL to both rear-1 and front-1; otherwise
assign front-1→next to front-1; free the temp;

Time Complexity = O(1)


Dr Somaraju Suvvari
54
NITP -- CS3401
Inject
• Inject(Q *front-2, Q *rear-2, Q *rear-1, Element Type value) –
• Inserting value into the Dequeue
• We can use the following steps to insert an element into the Deque from end...
Step 1 - create a new_node. If new_node creation failed, then display “Queue is FULL!!!
Insertion is not possible!!!" and terminate the function.
Step 2 - If Queue is NOT FULL, then insert the value in the new_node and NULL to
new_node → next.
Step 3 – If rear-2 == NULL, then assign new_node to rear-2 and front-2; If rear-1 is not NULL then
assign new_node to rear-1→next.
Step 4 –If rear-2 != NULL, then assign new_node to rear-2→next, and new_node
to rear-2.
Time Complexity = O(1)
Dr Somaraju Suvvari
55
NITP -- CS3401
Eject
• Eject(Q *front-2, Q *rear-2, Q* rear-1) –
• Deleting a value from the dequeue from the end
• We can use the following steps to insert an element into the queue...
Step 1 – Check if DeQueue is EMPTY.
front-2 == NULL.
Step 2 - If DeQue is NOT EMPTY, then create temp pointer and assign front-2 to temp.
Step 3 – Display the front-2→data as deleted element.
Step 4 – If rear-2 = = front-2, then assign NULL to both rear-2 and front-2 and if rear-1
is not NULL then assign NULL to rear-1→next; otherwise assign
front-1→next to front-1; Free the temp; Time Complexity = O(1)
Dr Somaraju Suvvari
56
NITP -- CS3401
Notes on implementing Circular Queue using SLL
Circular Queue Implementation using SLL
1. Front store the first node address in Queue
2. Rear points the last node address
3. Last node next stores the first node address.
4. Enqueue is an operation of inserting node at the end of the list.
5. Deque is the operation of deleting the first node in the queue

Dr Somaraju Suvvari
57
NITP -- CS3401
THANK YOU

Dr Somaraju Suvvari
58
NITP -- CS3401

You might also like