Queue Using Linked List
Queue :- In programming, a queue is a data structure in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out).
Program#include <iostream>
#include <conio.h>
#define MAX 10
using namespace std;
struct queue
{
int data;
queue *prev, *next;
}*ctr, *temp, *start=NULL;
void push();
void show();
void pop();
int main()
{ int count=0;
while(1)
{
cout <<"MAIN MENU\n" ;
cout <<"\tPress 1 for PUSH \n" ;
cout <<"\tPress 2 for SHOW \n" ;
cout <<"\tPress 3 for POP \n" ;
cout <<"\tPress 0 for exit\n\nAns. " ;
char ch;
cin>>ch;
switch(ch)
{
case '1':
count++;
if(count<=MAX)
push();
else
cout<<"Queue if full";
cout<<endl<<endl<<endl;
break;
case '2':
show();
getch();
break;
case '3':
count--;
if(count<=0)
cout<<"Queue is Empty\n";
else
pop();
break;
case '0':
return 0;
}
}
getch();
return 0;
}
void push()
{
temp = new queue;
temp -> next = NULL;
cout << "Enter data : " ;
cin >> temp -> data ;
if(start == NULL)
{
start = temp;
temp -> prev = NULL;
}
else
{
ctr= start;
while(ctr->next != NULL)
{
ctr = ctr->next;
}
ctr->next = temp;
temp->prev = ctr;
}
}
void show()
{ cout<<"\n\nQueue\n\n";
ctr=start;
while(ctr!=NULL)
{
cout<<"-------"<<endl<<"|| "<<ctr->data<<" ||"<<endl<<"-------"<<endl;
ctr=ctr->next;
}
cout<<endl<<endl<<endl;
}
void pop()
{
temp=start;
cout<<"\nDATA POPED \n";
if(temp==NULL)
cout<<"There is no Info present in Queue. Queue is empty.";
else
{
start=temp->next;
temp->next=NULL;
}
cout<<endl<<endl<<endl;
}