Lab 07
Lab 07
• A Queue is a linear structure which follows a particular order in which the operations are performed.
• A good example of a queue is any queue of consumers for a resource where the consumer that came
first is served first.
• The difference between stacks and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the least recently added.
• A queue is a useful data structure in programming. It is similar to the ticket queue outside a cinema hall, where
the first person entering the queue is the first person who gets the ticket.
Queue
Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other
is used to remove data (dequeue).
Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first.
More real-world examples can be seen as queues at the ticket windows and bus-stops.
Queue
Basic Operations
The following steps should be taken to enqueue (insert) data into a queue −
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
class queue
{
private:
int array[size];
int first,last,count;
public:
queue();
void insert(int);
int remove();
};
Queue using Arrays
void queue::insert(int value)
{
queue::queue():first(0),last(-1),count(0)
{ if(count>=size)
} {
cout<<"Queue is full\n";
return;
}
if(last>=size-1)
{
last=-1;
}
array[++last]=value;
count++;
}
Queue using Arrays
int queue::remove()
{
if(count<=0)
{
cout<<"Queue is empty\n";
return NULL;
}
if(first>=size)
{
first=0;
}
count--;
return array[first++];
}
Queue using Arrays cout<<q.remove()<<endl;
cout<<q.remove()<<endl;
int main()
{ cout<<q.remove()<<endl;
queue q; cout<<q.remove()<<endl;
q.insert(10); cout<<q.remove()<<endl;
q.insert(20); cout<<q.remove()<<endl;
q.insert(30); cout<<q.remove()<<endl;
cout<<q.remove()<<endl; cout<<q.remove()<<endl;
cout<<q.remove()<<endl; cout<<q.remove()<<endl;
cout<<q.remove()<<endl; cout<<q.remove()<<endl;
q.insert(40); cout<<q.remove()<<endl;
q.insert(50); }
q.insert(60);
q.insert(70);
q.insert(80);
q.insert(90);
q.insert(100);
q.insert(110);
q.insert(120);
q.insert(130);
Queue using Linked List
#include <iostream>
#include <conio.h>
class node
{
public:
int data;
node *link;
node(){data=0;link=NULL; }
};
Queue using Linked List
class queue
{
private:
node *first,*last;
public:
queue():first(NULL),last(NULL){
}
void insert(int);
int remove();
~queue();
};
Queue using Linked List
void queue::insert(int value)
if(first==NULL)
{
{
node *ptr=NULL;
first=ptr;
ptr=new node;
first->data=value;
if(ptr==NULL)
first->link=NULL;
{
last=first;
cout<<"Queue is full\n";
return;
return;
}
}
last->link=ptr;
last=ptr;
last->data=value;
last->link=NULL;
}
Queue using Linked List
int queue::remove()
{
if(first==NULL)
{
cout<<"Queue empty!\n";
return NULL;
}
node *ptr=first;
first=first->link;
int value = ptr->data;
delete ptr;
return value;
}
Queue using Linked List
queue::~queue()
{
node *ptr=first;
while(first!=NULL)
{
first=first->link;
delete ptr;
ptr=first;
}
}
Queue using Linked List
int main()
{
queue q;
int i;
q.remove();
for(i=1;i<=5;i++)
q.insert(i);
for(i=1;i<=5;i++)
cout<<q.remove()<<" ";
}