Data Structure3
Data Structure3
A queue is a data structure for storing a number of data items that have the same
type.
It can be accessed at two ends.
Items are inserted at the one end ( called the rear end or tail of queue).
items may be deleted at other end (called front or head of the queue).
A queue obeys the First In First Out (FIFO) principle, which is identical to the Last
In Last Out (LILO) principle.
Dequeue Enqueue
Operations of a Queue
Clear: (also called flush or reset) removes all the items of the queue (destroy or flush the queue).
IsEmpty: A boolean function that returns true if the queue is empty.
IsFull: A boolean function that returns true if the queue is full.
First: A function that returns the value of the first item without dequeuing it.
Applications of Queue
Two ways:
Using an array (ordinary or dynamic).
Using a linked list (singly or doubly linked).
Implementations using dynamic array
}
}
Implementations using dynamic array
// print queue elements
void Display()
void Dequeue() {
{ int i;
if (front == -1) if (front == -1)
cout<<"\nQueue is empty\n"; cout<<"\nQueue is Empty\n";
else { // traverse front to rear and print elements
for (int i = 0; i < rear; i++) for (i = 0; i <= rear; i++)
queue[i] = queue[i + 1]; cout<<queue[i]<<"---";
rear--; }
if (rear==-1)front =-1; // print front of queue
} void queueFront()
} {
if (front == -1)
cout<<"\nQueue is Empty\n";
else
cout<<"\nFront Element is:"<< queue[front];
}};
Implementations using dynamic array
void main(void)
{// Create a queue of capacity 4
Queue q(4);
q.Display();
q.Enqueue(20);
q.Enqueue(30);
q.Enqueue(40);
q.Enqueue(50);
q.Display();
q.Enqueue(60);
q.Display();
q.Dequeue();
q.Dequeue();
cout<<"\n\nafter two node deletion\n\n";
q.Display();
q.queueFront();
}
Implementations using linked list
Enqueue Dequeue
In a circular queue, the last element points to the first element making a circular link.
The main advantage of a circular queue over a simple queue is better memory
utilization.
If the last position is full and the first position is empty, we can insert an element in
the first position. This action is not possible in a simple queue.
Circular Queue
rear = 0 rear = 6
rear = 1 front = 0
rear = -1, front = -1 front = 0 front = 0
rear = 6 rear = 0
front = 1 front = 1
Priority Queue
A Priority Queue is a queue in which items have a priority criterion. When dequeuing
such a queue, the first item with highest priority is chosen for dequeuing.
If elements with the same priority occur, they are served according to their order in
the queue.
Priority Queue
In a double ended queue, insertion and removal of elements can be performed from
either from the front or rear. Thus, it does not follow the FIFO (First In First Out)
rule.
Enqueue Enqueue
Dequeue Dequeue
Stack VS. Queue
BASIS FOR
STACK QUEUE
COMPARISON
Working principle LIFO (Last in First out) FIFO (First in First out)
Structure Same end is used to insert and delete One end is used for insertion, i.e., rear end
.elements and another end is used for deletion of
.elements, i.e., front end
Number of pointers used One Two (In simple queue case)
Operations performed Push and Pop Enqueue and dequeue
Examination of empty Top == -1 Front == -1
condition
Examination of full Top == Max - 1 Rear == Max - 1
condition
Variants .It does not have variants It has variants like circular queue, priority
.queue, doubly ended queue