QUEUE USING LINKED LIST

For Algorithm and Program related to queue using integer array follow this link :
https://codstudio.wordpress.com/2014/02/23/queue-using-integer-array/

Program using linked list :

/***********************************
		AUTHOR- TANMAY BARANWAL
		    C#ODE STUDIO
***********************************/
#include"iostream"
#include"conio.h"
using namespace std;
class linkedq
{
	struct queue
	{
		int info;
		queue *link;
	}*front,*rear;
	public:
		linkedq()
		{
			front=NULL;
			rear=NULL;
		}
		int push(int);
		int pop();
		int disp();
};
int linkedq::push(int x)
{
	queue *ptr;
	ptr=new queue;
	ptr->info=x;
	ptr->link=NULL;
	if(front==NULL)
		front=ptr;
	else
		rear->link=ptr;
	rear=ptr;
	disp();
}
int linkedq::pop()
{
	queue *ptr;
	if(front==NULL)
		cout<<"\nUNDERFLOW ";
	else
	{
		ptr=front;
		front=front->link;
	}
	cout<<"\nELEMENT DELETED IS : "<<ptr->info;
	disp();
}
int linkedq::disp()
{
	queue *ptr;
	if(front==NULL)
	{
		cout<<"\nUNDERFLOW ";
	}
	else
	{
		ptr=front;
		cout<<"\nQUEUE => ";
		while(ptr!=NULL)
		{
			cout<<ptr->info<<"=> ";
			ptr=ptr->link;
		}
		cout<<" NULL"<<endl;
	}
}
main()
{
	linkedq q;
	int num,choice;
	do
	{
		cout<<"\n1:PUSH"
			<<"\t2:POP"
			<<"\t3:DISPLAY"
			<<"\t4:EXIT"<<endl;
		cout<<"\nMAKE CHOICE : ";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nINSERT VALUE : ";
					cin>>num;
					q.push(num);
					break;
			case 2: q.pop();
					break;
			case 3: q.disp();
					break;
			case 4: cout<<"\nEXITING PROGRAM "<<endl;
					break;
			default: cout<<"\nENTER CORRECT CHOICE";
		}
		cout<<"________________________________________"<<endl;
	}while(choice!=4);
	cout<<"\n\n\tPROGRAMMING @ C#ODE STUDIO";
	getch();
}

OUTPUT – :

queue_ll

QUEUE USING INTEGER ARRAY

QUEUE :

A queue is another special kind of list, where items are inserted at one end (the rear)
and deleted at the other end (the front). Another name for a queue is a “FIFO” or
“first-in first-out” list. The operations for a queue are analogous to those for a stack,
the substantial differences being that insertions go at the end of the list, rather than
the beginning, and that traditional terminology for stacks and queues is different.

ALGORITHM :

INSERTION( A, N, ITEM, FRONT, REAR ) 1: IF ( FRONT=1 && REAR = N ) OR FRONT=REAR+1 WRITE “ OVERFLOW” & RETURN 2: IF ( FRONT =REAR=NULL then SET FRONT:=REAR:=1 ELSEIF (REAR = N) SET REAR:=1 ELSE SET REAR:=REAR+1; ENDIF 3: SET A [ REAR ] := ITEM 4: RETURN

 

DELETION( A, N, ITEM, FRONT, REAR ) 1: IF ( FRONT = REAR = NULL ) WRITE “ UNDERFLOW” & RETURN 2: ITEM:=A[ FRONT ]
3: IF( FRONT =REAR)
             SET FRONT:=NULL & REAR :=NULL
   ELSEIF( FRONT =1 )
             SET FRONT:=1
   ELSE
             SET FRONT:=FRONT + 1
   ENDIF
4: RETURN

 

 

PROGRAM : USING INTEGER ARRAY

/***********************************
		AUTHOR- TANMAY BARANWAL
		    C#ODE STUDIO
***********************************/
#include<conio.h>
#include<iostream>
using namespace std;
class queue
{
	private:
		int *a;
		int msize,csize,dsize,val,i,j;
	public:
		queue(int z)
		{			
			msize=z;
			a=new int[100];
			csize=0;
			dsize=0;
			val=0;
			i=0;
			j=0;
		}
		int qin()
		{
			if(csize == msize)
				cout<<"Queue is full"<<endl;
			else
			{
				cout<<"\nEnter Value : ";
				cin>>val;
				a[csize]=val;
				csize++;
			}
		}
		int queout()
		{
			if(csize==0)
				cout<<"Queue is empty"<<endl;
			else
			{
				val=a[dsize];
				msize++;
				dsize++;
				cout<<"\nValue Out : "<<val<<endl;
			}
		}
		int display()
		{
			if(csize==0)
				cout<<"\nQueue is empty"<<endl;
			else
				for(i=csize-1;i>=dsize;i--)
				{
					cout<<"\t||  "<<a[i]<<"  ||"<<endl;
				}
		}
};
int main()
{
	int a,choice;
	cout<<"\nENTER QUEUE SIZE:";
	cin>>a;
	queue obj(a);
	do
	{
		cout<<"\n1:PUSH, 2:POP, 3:DISPLAY, 4:EXIT   :::  ";
		cin>>choice;
		cout<<"_____________________________"<<endl;
		switch(choice)
		{
			case 1: obj.qin();
				break;
			case 2: obj.queout();
				break;
			case 3: obj.display();
				break;
			case 4: cout<<"\nTERMINATING PROGRAM ";
					break;
			default: cout<<"\nMAKE CORRECT CHOICE"<<endl;
		}
	}while(choice!=4);
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
}
/*******************************
			END
*******************************/

queu

STACK LINKED LIST

A stack is a data holding structure represented whether as linked list or array, for stack using array click here.

A stack follows LAST IN FIRST OUT for which we have to follow insertion at end and deletion in end .
Same in queue : we have to inbuilt two functions INSERTION AT END and DELETION AT BEGINING as it follows FIRST IN FIRST OUT : NEXT PROGRAM WILL BE ON QUEUES.

Program:

#include"conio.h"
#include"iostream"
/*STACK USING LINKED LIST BY C#ODE STUDIO*/
using namespace std;
class list
{
	struct node
	{
		int info;
		node *link;
	}*p,*start; /*STRUCTURE CONTAINING INFO AND LINK BY C#ODE STUDIO*/
	int size,m;
	public:
		list(int b)
		{
			p=NULL;
			size=b; /*SIZE OF STACK IS INITIALIZED IN CONSTRUCTOR*/
			m=0;
		}
		int push(int x);
		int pop();
		int disp();
};
/*PUSH FUNCTION BY C#ODE STUDIO*/
int list::push(int x)
{
	if(m<size)
	{
		if(p==NULL)
		{
			node *ptr;
			ptr=p;
			p=new node;
			p->info=x;
			p->link=ptr;
			cout<<"\nITEM INSERTED AT BEGINING"<<endl;
		}
		else
		{
			node *ptr,*t;
			ptr=p;
			while(ptr->link!=NULL)
				ptr=ptr->link;
			t=new node;
			t->info=x;
			t->link=ptr->link;
			ptr->link=t;
			cout<<"\nITEM INSERTED AT LIST : "<<x<<endl;
		}
		m+=1;
	}
	else
	{
		cout<<"\nOVERFLOW OCCURS : ";
	}
	disp();	
}
/*POP FUNCTION BY C#ODE STUDIO*/
int list::pop()
{
	node *ptr,*temp;
	int inf;
	ptr=p;
	if(ptr==NULL)
		cout<<"\nUNDERFLOW, NO NODES ARE AVAILABLE"<<endl;
	else
	{
		while(ptr->link!=NULL)
		{
			temp=ptr;
			ptr=ptr->link;
		}
		inf=temp->info;
		temp->link=NULL;
		cout<<"\nELEMENT POPPED : "<<inf<<endl;
	}
	disp();
}
/*DISPLAY FUNCTION BY C#ODE STUDIO*/
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<endl<<"STACK => Start => ";
	while(ptr!=NULL)
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->link;
	}
	cout<<" NULL"<<endl<<endl;
}
int main()
{
	int siz;
	cout<<"\nENTER SIZE OF STACK : ";
	cin>>siz;
	list l(siz); /*CONSTRUCTOR OF LIST L BY C#ODE STUDIO*/
	int i=1,num,choice;
	while(i!=0)
	{
		cout<<"\n1:PUSH A ELEMENT"
			<<"\n2:POP A ELEMENT"
			<<"\n3:DISP"<<endl;
		cout<<"\nENTER CHOICE : ";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nENTER NUMBER : ";
					cin>>num;
					l.push(num);
					break;
			case 2: l.pop();
					break;
			case 3: l.disp();
					break;
			default: cout<<"\nWRONG CHOICE";
		}
		cout<<"\n1:CONTINUE OR 0:EXIT :: MAKE CHOICE : ";
		cin>>i;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
}

Output :

stack_ll

Continue reading