0% found this document useful (0 votes)
16 views14 pages

Unit4 Queue Updated

Uploaded by

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

Unit4 Queue Updated

Uploaded by

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

Queue

1
Queue

• Like a stack, a queue is also a list. However, with a queue, insertion is done at
one end, while deletion is performed at the other end.

• Accessing the elements of queues follows a First In, First Out (FIFO) order.
– Like customers standing in a check-out line in a store, the first customer in
is the first customer served.

• Basic operations:
– enqueue: insert an element at the rear of the list
– dequeue: delete the element at the front of the list

2
The Queue Operations
• A queue is like a line of people waiting for a bank teller. The
queue has a front and a rear ends.
• New people must enter the queue at the rear. To insert an element
in queue is called enqueue operation.
• People must leave from the queue at the front. To remove an
element in queue is called dequeue operation.

$ $

Front
Rear
3
Enqueue and Dequeue
• Primary queue operations: Enqueue and Dequeue

• Like check-out lines in a store, a queue has a front and a rear.

• Enqueue
– Insert an element at the rear of the queue

• Dequeue
– Remove an element from the front of the queue

• Implementation of Queue: Just as stacks can be implemented as arrays or linked lists,


so with queues.

Remove Insert
(Dequeue) front rear (Enqueue)
4
Queue Implementation
Queues can be implemented in two ways:-
a)Arrays
b)Linked List
Way I: Queues can be easily represented using linear arrays.
In an array of size 6 , FRONT = 0 and REAR = 5.
•Suppose we want to add another element with value 45, then REAR would be
incremented by 1 and the value would be stored at the position pointed by REAR.
FRONT = 0 and REAR = 6
•If we want to delete an element from the queue, then the value of FRONT will be
incremented.

3 6

5
Queue Operations switch(option)
#include<iostream> {
#define MAX 10 case 1:
cout<<"enter the value to insert";
using namespace std;
cin>>val;
int queue[MAX]; insert_element(val);
int front = -1, rear =-1; break;
void insert_element(int value); case 2:
int delete_element(); val = delete_element();
int peek(); if (val != -1)
void display(); cout<<"\n The number deleted is :"<<
int main() val<<endl;
break;
{ int option, val;
case 3:
char choice='Y'; val = peek();
while(choice=='Y') if (val != -1)
{ cout<<endl<<" ***** MAIN MENU cout<<"\n The first value in queue is : "<<
*****"; val<<endl;
cout<<endl<<" 1. Insert an element"; break;
cout<<endl<< "2. Delete an element"; case 4:
cout<<endl<< "3. Peek"; display();
break;
cout<<endl<<" 4. Display the queue";
}
cout<<endl<<" Enter your option : "; cout<<"Enter choice";
cin>>option; cin>>choice;
}
return 0;
6
}
Queue Operations int peek() // To find the first element
{
void insert_element(int value) // Enqueue if(front==-1 )
{ {
cout<<"\n QUEUE IS EMPTY";
if(rear == MAX-1) return -1;
cout<<"\n OVERFLOW"; }
else if(front == -1 && rear == -1) else
front = rear = 0; {
else return queue[front];
rear++; }
queue[rear] = value; }
} void display() // To show the data of queueu
int delete_element() // Dequeue {
{ int i;
int val; cout<<"\n";
if(front == -1) if(front == -1 )
{ cout<<"\n QUEUE IS EMPTY";
cout<<"\n UNDERFLOW"; else
return -1; {
} for(i = front; i <= rear; i++)
else cout<<"\t "<< queue[i];
{ }
val = queue[front]; }
front++;
return val;
7
}}
Queue Implementation with Linked List
Way II: Queues can be easily represented using single linked list.
In case of an array, In a linked queue, every element has two parts, one that stores the
data and another that stores the address of the next element. The START pointer of the
linked list is used as FRONT. Here, we will also use another pointer called REAR, which
will store the address of the last element in the queue.

3 6

8
Queue Operations switch(option)
#include<iostream> {
#include<stdlib.h> case 1:
cout<<"enter the value to insert";
using namespace std;
cin>>val;
enqueue(val);
struct queue break;
{ case 2:
int data; dequeue();
struct queue *next; break;
}; case 3:
struct queue *temp,*front,*rear; display();
break;
}
void enqueue(int value); cout<<"Enter choice";
void dequeue(); cin>>choice;
void display(); }
int main() return 0;
{ int option, val; }
char choice='Y';
while(choice=='Y')
{ cout<<endl<<" ***** MAIN MENU
*****";
cout<<endl<<" 1. Insert an element";
cout<<endl<< "2. Delete an element";
cout<<endl<<" Enter your option : "; 9
cin>>option;
void enqueue(int value)
{ void display() // To show the data of queue
temp=new queue(); {
temp =front;
temp->data =value;
cout<<"\n";
if(front ==NULL && rear ==NULL) // empty if(front == NULL )
queue cout<<"\n QUEUE IS EMPTY";
{ front = rear = temp; } else
else {
{ while(temp!=NULL)
rear->next =temp; {
} cout<<temp->data;
temp=temp->next;
}
}
void dequeue() }
{
temp = front;
if(front == NULL)
{
cout<<“underflow”;
break;
}
else
{
front= temp->next;
free(temp); 10
}}
DEQUES(Double Ended Queue)
• A deque is a linear list in which elements can be added or removed at either end
but not in middle( Left ,Right pointer).

• There are two variation of a Deque- namely an Input Restricted and an output-
Restricted deque.

• An input restricted deque is a deque which allows insertions at only one end
of a list but allows deletions on both the ends of the list

• An output restricted Deque is a deque which allows deletions at only one end
of the list but allows insertions at both the ends of the list.

11
Circular Queue

If you want to insert another value, it will not be possible because the queue is
completely full. There is no empty space where the value can be inserted. Consider a
scenario in which two successive deletions are made.
front = 2 and REAR = 9.

•Even though there is space available, the overflow condition still exists because the
condition rear = MAX – 1 still holds true. This is a major drawback of a linear queue.

•The second option is to use a circular queue. In the circular queue, the first index
comes right after the last index.

12
Some applications of Queue:
•Applied as waiting lists for a single shared resource like
CPU, Disk, and Printer.
•Applied as buffers on MP3 players and portable CD
players.
•Applied on Operating system to handle the interruption.
•Applied to add a song at the end or to play from the
front.

13
Ques

Ques : Write an algorithm/program to implement enqueue and


dequeue operations.
Ques : How many types of queue are there ? Explain
Ques : Implement the queue using Linked List.

14

You might also like