Ds Unit2
Ds Unit2
Basic Operations
peek: Gets an element at the front of the queue without removing it.
QUEUE ABSTRACT DATA STRUCTURE
Insertion
Deletion
This is an empty queue and thus we have rear and empty set to -1.
Next, we add 1 to the queue and as a result, the rear pointer moves ahead by
one location.
In the next figure, we add element 2 to the queue by moving the rear pointer
ahead by another increment.
In the following figure, we add element 3 and move the rear pointer by 1.
At this point, the rear pointer has value 2 while the front pointer is at the
0th location.
Next, we delete the element pointed by the front pointer. As the front pointer
is at 0, the element that is deleted is 1.
Thus the first element entered in the queue i.e. 1 happens to be the first
element removed from the queue.
Program to implements the queue using an array is given as follows
#include <iostream>
using namespace std;
int Q[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Q Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in Q : "<<endl;
cin>>val;
rear++;
Q[rear] = val;
}
}
Program Continue…..
void Delete()
{
if (front == - 1 || front > rear)
{
cout<<"Q Underflow ";
}
else
{
cout<<"Element deleted from Q is : "<< Q[front] <<endl;
front++;
}
}
void Display()
{
if (front == - 1)
cout<<"Q is empty"<<endl;
else
{
cout<<"Q elements are : ";
for (int i = front; i <= rear; i++)
cout<<Q[i]<<" ";
cout<<endl;
}
}
Program Continue…..
int main() {
int ch;
cout<<"1) Insert "<<endl;
cout<<"2) Delete "<<endl;
cout<<"3) Display "<<endl;
cout<<"4) Exit "<<endl;
do
{
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
Stack Vs. Queue
Stacks and queues are secondary data structures which can be used to store
data. They can be programmed using the primary data structures like arrays
and linked lists.
Differences between these two data structures.
Stacks Queues
Uses LIFO (Last in, First out) Uses FIFO (First in, First out)
approach. approach.
Items are added or deleted from only Items are added from “Rear” end of
one end called “Top” of the stack. the queue and are removed from the
“front” of the queue.
The basic operations for the stack are The basic operations for a queue are
“push” and “Pop”. “enqueue” and “dequeue”.
We can do all operations on the stack In queues, we need to maintain two
by maintaining only one pointer to pointers, one to access the front of
access the top of the stack. the queue and the second one to
access the rear of the queue.
The stack is mostly used to solve Queues are used to solve problems
recursive problems. related to ordered processing.
CIRCULAR QUEUE
A circular queue is a linear data structure that is used to store data
items. It performs operations by following the FIFO (First In, First Out)
approach and the last position in the queue is connected back to the first
position to form a circle. A circular queue is an extension of the basic queue
that we have discussed earlier. It is also known as “Ring buffer”.
Diagram shows a
circular queue
The above image shows a circular data structure of size 10. The
first six elements are already in the queue and we see that the first position
and last position are joined. Due to this arrangement, space doesn’t go
wasted as it happens in a linear queue.
CIRCULAR QUEUE
Main Difference is:
In the circular queue, when the queue is full, and when we remove
elements from the front since last and first positions are connected, we can
insert the elements at the rear which was vacated by deleting the element.
Basic Operations
Some of the basic operations of the circular queue are as follows:
Front: Returns the front position in the circular queue.
Rear: Returns the rear position in the circular queue.
Insert: Enqueue (value) is used to insert an element in the circular
queue. The element is always inserted at the rear end of the
queue.
CIRCULAR QUEUE
Insert:
1) Check if the circular queue is full:
test ((rear == SIZE-1 && front == 0) || (rear == front-1)),
where ‘SIZE’ is the size of the circular queue.
2) If the circular queue is full then it displays a message as
“Queue is full”.
If queue is not full then,
check if (rear == SIZE – 1 && front != 0).
If it is true then set rear=0 and insert element.
Delete:
1) Check if the circular queue is Empty: check if (front==-1).
2) If it is empty then display the message “Queue is empty”.
If queue is not empty then perform step 3.
3) Check if (front==rear). If it is true then set
front=rear= -1
else
check if (front==size-1),
if it is true then set front=0 and return the element.
A detailed illustration of adding/removing elements in the circular
queue.
Consider the following circular queue of 5 elements as shown below:
Now we delete the two elements i.e. item 1 and item 3 from the
queue as shown below
Next, we insert or enqueue element 11 in the circular queue as
represented below.
Again let us insert element 13 in the circular queue. The queue will
look as shown below.
The difference between Queue and Deque is that it does not follow
the FIFO (First In, First Out) approach. The second feature of Deque
is that we can insert and remove elements from either front or rear
ends.
DEQUEUE OR DOUBLE ENDED QUEUE
Basic Deque Operations
The following are the basic operations that can be performed on deque:
deleteFront: Delete or remove the item from the front of the queue.
deleteLast: Delete or remove the item from the rear of the queue.
2) Each move consists of taking the upper disk from one of the
stacks and placing it on top of another stack i.e. a disk can
only be moved if it is the uppermost disk on a stack.
If no. of disks greater than 1 then Move n-1 disks from source
to auxiliary with the help of destination.
//main program
int main() {
int n;
cout<<"Enter no. of disks:";
cin>>n;