VELAMMAL ENGINEERING COLLEGE
An Autonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi
Department of Computer Science and
Engineering
UNIT 2 -QUEUE
Queue ADT
Mrs. G. Sumathi
Assistant Professor
Velammal Engineering College
Chennai
VELAMMAL ENGINEERING COLLEGE
An Autonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi
Dept of CSE, Velammal Engineering College,
Chennai
3
Agenda
• Queue ADT
• Array implementation of queue
• Linked list implementation of queue
• Types
– Circular Queue
– Double Ended Queue
– Priority Queue
• Applications of Queue
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
4
Queue ADT
• Linear data structure
• Insertion occurs only at one end called rear
• Deletion occurs only at one end called front
• Element that is inserted first is deleted (FIFO
principle)
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
5
Operations in queue:
• Enqueue: Inserting an element into the queue (at
rear)
• Dequeue: Deleting an element from the queue (at
front)
Exceptional Conditions:
• Overflow: Attempting to insert an element into
queue when it is full
• Underflow: Attempting to delete an element from
queue when it is empty
Queue ADT
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
6
Array Implementation of Queue
Enqueue operation
Void Enqueue (int num) //Let num = 12 and max=5
{
if (rear==max-1)
print(“Queue Overlfow”);
else if (front == -1 && rear == -1)
{
front = rear = 0;
Q[rear] = num;
}
else
{
rear++;
Q[rear] = num;
}
}
12 25 18
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
7
Dequeue Operation
Void Dequeue()
{
int val;
if (front == -1 || front > rear)
print(“Queue Underflow”);
else
{
val= Q[front];
front++;
}
}
10 20 30
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
8
Display Operation
void Display ()
{
int i;
if (front == -1 || front > rear)
print (“Queue Underflow”);
else
{
for(i=front; i<=rear; i++)
print(Q[i]);
}
}
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
9
Linked List Implementation of Queue
Enqueue operation
void Enqueue (int num) // Insert at end of linked list
{
n=(struct node *) malloc (sizeof (struct node));
ndata=num;
nnext=NULL;
if(rear == NULL)
front = rear= n;
else
{
rearnext = n;
rear = n;
}
}
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
10
Dequeue Operation
Void Dequeue() // Delete at beginning of linked list
{
struct node *t;
if (front == NULL)
print(“Underflow”);
else
{
t = front;
front = front  next;
free(t);
}
}
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
11
Circular Queue
• Type of queue
• Last position of the queue is connected to the first
position in a circular fashion
• Overcome the disadvantage of wastage of front end
memory space after performing dequeue operation
in linear queue
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
12
• In linear queue, after performing dequeue, the
memory space at the front end remains
unused
• This situation does not occur in circular queue
Need for Circular Queue
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
13
Enqueue Operation
void Cir_Enqueue (int num) // Let max=6
{
if((front == 0 && rear == max-1) || (rear ==(front-1)))
print(“Queue Overflow”);
else if (front == -1 && rear == -1)
{ front = rear = 0;
Q[rear] = num;
}
else if (rear == max-1 && front != 0)
{ rear = 0;
Q[rear] = num;
}
else
{ rear ++;
Q[rear] = num;
}
}
30
40
50 60
70
50
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
14
Dequeue Operation
Void Cir_Dequeue()
{
int val;
if (front ==-1)
print (“Queue Underflow”);
val = Q[front];
if (front == rear)
front = rear = – 1;
else if (front == max – 1)
front = 0;
else
front ++;
}
20
40
20
30
20
30
40
front
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
15
Double Ended Queue (DEQUE)
DEQUE is of two types
• Input restricted DEQUE
– Insertion at only one end
– Deletion at both ends
• Output restricted DEQUE
– Deletion at only one end
– Insertion at both ends
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
16
Priority Queue
• Every item has a priority associated with it
• Element with higher priority is served first
• If two elements have same priority then they served
according to the order in the queue
• Generally, the value of the element itself is
considered as higher priority
• Applications:
– CPU scheduling
– Graph algorithms like Dijkstra’s shortest path, Prim’s
minimum spanning tree, etc,.
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
17
Applications of Queue
• Data getting transferred between the IO Buffers
(Input Output Buffers).
• CPU scheduling and Disk scheduling.
• Managing shared resources between various
processes.
• Job scheduling
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
18
• Traffic light functioning is the best example
for circular queues . The colors in the traffic light
follow a circular pattern.
• In page replacement algorithms, a circular list of
pages is maintained and when a page needs to be
replaced, the page in the front of the queue will be
chosen.
Applications of Circular Queue
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
19
Queries??
26/5/2020
Dept of CSE, Velammal Engineering College,
Chennai
20
Thank You !!!
26/5/2020

Comprehensive Guide to Queue Data Structure with Operations, Types, and Applications in C Programming

  • 1.
    VELAMMAL ENGINEERING COLLEGE AnAutonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi Department of Computer Science and Engineering UNIT 2 -QUEUE
  • 2.
    Queue ADT Mrs. G.Sumathi Assistant Professor Velammal Engineering College Chennai VELAMMAL ENGINEERING COLLEGE An Autonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi
  • 3.
    Dept of CSE,Velammal Engineering College, Chennai 3 Agenda • Queue ADT • Array implementation of queue • Linked list implementation of queue • Types – Circular Queue – Double Ended Queue – Priority Queue • Applications of Queue 26/5/2020
  • 4.
    Dept of CSE,Velammal Engineering College, Chennai 4 Queue ADT • Linear data structure • Insertion occurs only at one end called rear • Deletion occurs only at one end called front • Element that is inserted first is deleted (FIFO principle) 26/5/2020
  • 5.
    Dept of CSE,Velammal Engineering College, Chennai 5 Operations in queue: • Enqueue: Inserting an element into the queue (at rear) • Dequeue: Deleting an element from the queue (at front) Exceptional Conditions: • Overflow: Attempting to insert an element into queue when it is full • Underflow: Attempting to delete an element from queue when it is empty Queue ADT 26/5/2020
  • 6.
    Dept of CSE,Velammal Engineering College, Chennai 6 Array Implementation of Queue Enqueue operation Void Enqueue (int num) //Let num = 12 and max=5 { if (rear==max-1) print(“Queue Overlfow”); else if (front == -1 && rear == -1) { front = rear = 0; Q[rear] = num; } else { rear++; Q[rear] = num; } } 12 25 18 26/5/2020
  • 7.
    Dept of CSE,Velammal Engineering College, Chennai 7 Dequeue Operation Void Dequeue() { int val; if (front == -1 || front > rear) print(“Queue Underflow”); else { val= Q[front]; front++; } } 10 20 30 26/5/2020
  • 8.
    Dept of CSE,Velammal Engineering College, Chennai 8 Display Operation void Display () { int i; if (front == -1 || front > rear) print (“Queue Underflow”); else { for(i=front; i<=rear; i++) print(Q[i]); } } 26/5/2020
  • 9.
    Dept of CSE,Velammal Engineering College, Chennai 9 Linked List Implementation of Queue Enqueue operation void Enqueue (int num) // Insert at end of linked list { n=(struct node *) malloc (sizeof (struct node)); ndata=num; nnext=NULL; if(rear == NULL) front = rear= n; else { rearnext = n; rear = n; } } 26/5/2020
  • 10.
    Dept of CSE,Velammal Engineering College, Chennai 10 Dequeue Operation Void Dequeue() // Delete at beginning of linked list { struct node *t; if (front == NULL) print(“Underflow”); else { t = front; front = front  next; free(t); } } 26/5/2020
  • 11.
    Dept of CSE,Velammal Engineering College, Chennai 11 Circular Queue • Type of queue • Last position of the queue is connected to the first position in a circular fashion • Overcome the disadvantage of wastage of front end memory space after performing dequeue operation in linear queue 26/5/2020
  • 12.
    Dept of CSE,Velammal Engineering College, Chennai 12 • In linear queue, after performing dequeue, the memory space at the front end remains unused • This situation does not occur in circular queue Need for Circular Queue 26/5/2020
  • 13.
    Dept of CSE,Velammal Engineering College, Chennai 13 Enqueue Operation void Cir_Enqueue (int num) // Let max=6 { if((front == 0 && rear == max-1) || (rear ==(front-1))) print(“Queue Overflow”); else if (front == -1 && rear == -1) { front = rear = 0; Q[rear] = num; } else if (rear == max-1 && front != 0) { rear = 0; Q[rear] = num; } else { rear ++; Q[rear] = num; } } 30 40 50 60 70 50 26/5/2020
  • 14.
    Dept of CSE,Velammal Engineering College, Chennai 14 Dequeue Operation Void Cir_Dequeue() { int val; if (front ==-1) print (“Queue Underflow”); val = Q[front]; if (front == rear) front = rear = – 1; else if (front == max – 1) front = 0; else front ++; } 20 40 20 30 20 30 40 front 26/5/2020
  • 15.
    Dept of CSE,Velammal Engineering College, Chennai 15 Double Ended Queue (DEQUE) DEQUE is of two types • Input restricted DEQUE – Insertion at only one end – Deletion at both ends • Output restricted DEQUE – Deletion at only one end – Insertion at both ends 26/5/2020
  • 16.
    Dept of CSE,Velammal Engineering College, Chennai 16 Priority Queue • Every item has a priority associated with it • Element with higher priority is served first • If two elements have same priority then they served according to the order in the queue • Generally, the value of the element itself is considered as higher priority • Applications: – CPU scheduling – Graph algorithms like Dijkstra’s shortest path, Prim’s minimum spanning tree, etc,. 26/5/2020
  • 17.
    Dept of CSE,Velammal Engineering College, Chennai 17 Applications of Queue • Data getting transferred between the IO Buffers (Input Output Buffers). • CPU scheduling and Disk scheduling. • Managing shared resources between various processes. • Job scheduling 26/5/2020
  • 18.
    Dept of CSE,Velammal Engineering College, Chennai 18 • Traffic light functioning is the best example for circular queues . The colors in the traffic light follow a circular pattern. • In page replacement algorithms, a circular list of pages is maintained and when a page needs to be replaced, the page in the front of the queue will be chosen. Applications of Circular Queue 26/5/2020
  • 19.
    Dept of CSE,Velammal Engineering College, Chennai 19 Queries?? 26/5/2020
  • 20.
    Dept of CSE,Velammal Engineering College, Chennai 20 Thank You !!! 26/5/2020

Editor's Notes

  • #16 Data compression : It is used in Huffman codes which is used to compresses data. Artificial Intelligence : A* Search Algorithm : The A* search algorithm finds the shortest path between two vertices of a weighted graph, trying out the most promising routes first. The priority queue (also known as the fringe) is used to keep track of unexplored routes, the one for which a lower bound on the total path length is smallest is given highest priority.
  • #18 Pallindrome checker. A-steal job scheduling algorithm      - The A-steal algorithm implements task scheduling for multiple processors (multiprocessor scheduling).      - The processor gets the first element from the double ended queue.      - When one of the processors completes execution of its own thread, it can steal a thread from other processors.      - It gets the last element from the deque of another processor and executes it. Undo-redo operations in software applications.