0% found this document useful (0 votes)
21 views

Struct Stu (Int A Stu Next )

The document defines functions to implement a linked queue data structure using C++. The insert function adds a node containing a value to the rear of the queue. The remove function deletes the node at the front and returns the value. The show function traverses the queue and prints the values. A sample main function demonstrates inserting values into an empty queue, traversing the queue, inserting more values, removing values and traversing the modified queue.

Uploaded by

SurabhiSoumya
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Struct Stu (Int A Stu Next )

The document defines functions to implement a linked queue data structure using C++. The insert function adds a node containing a value to the rear of the queue. The remove function deletes the node at the front and returns the value. The show function traverses the queue and prints the values. A sample main function demonstrates inserting values into an empty queue, traversing the queue, inserting more values, removing values and traversing the modified queue.

Uploaded by

SurabhiSoumya
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* prg linked queue*/

struct stu
{
int a;
stu *next;
};

stu *insert(stu *rear,intval)


{
stu *x;
x=new stu;
x->a=val;
x->next=NULL;
if(rear!=NULL)
rear->next=x;
rear=x;
return rear;
}
stu *remove(stu *front,int&val)
{
stu *x;
x=front;
if(x==NULL)
{
cout<<"queue empty";
}
else
{
front=front->next;
val=x->a;
delete x;
}
return front;
}
void show(stu *front)
{
stu *x;
x=front;
cout<<"the queue is...\n";
while(x!=NULL)
{
cout<<x->a;
x=x->next;
}
}

void main()
{
clrscr();
stu *front,*rear;
intval,ch,i;
front=rear=NULL;
for(i=0;i<2;i++)
{
cout<<"enter the number to be inserted : ";
cin>>val;
rear=insert(rear,val);
if(front==NULL)
front=rear;
}
do
{
cout<<"\n\nmenu\n1.insert\n2.delete\n3.traverse\n4.
quit\nenter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<"enter the number to be inserted : ";
cin>>val;
rear=insert(rear,val);
if(front==NULL)
front=rear;
break;
case 2 : front=remove(front,val);
cout<<"\n the deleted value is : "<<val;
if(front==NULL)
rear=front;
break;
case 3 : show(front);
break;
}
}
while(ch!=4);
getch();
}

/* output linked Queue*/


enter the number to be inserted : 4
enter the number to be inserted : 6
menu
1.insert
2.delete
3.traverse
4.quit
enter your choice : 3
the queue is...
46
menu
1.insert
2.delete
3.traverse
4.quit
enter your choice : 1
enter the number to be inserted : 7
menu
1.insert
2.delete
1.insert
2.delete
3.traverse
4.quit
enter your choice : 3
the queue is...
467
menu
1.insert
2.delete
3.traverse
4.quit
enter your choice : 1
enter the number to be inserted : 8

menu
1.insert
2.delete
3.traverse
4.quit
enter your choice : 3
the queue is...
4678
menu
1.insert
2.delete
3.traverse
4.quit
enter your choice : 2
the deleted value is : 4
menu
1.insert
2.delete
3.traverse
4.quit
enter your choice : 3
the queue is...
678
menu
1.insert
2.delete
3.traverse
4.quit
enter your choice :

You might also like