0% found this document useful (0 votes)
4 views4 pages

C++ Queue Implementation Example

The document defines a queue class that implements a queue data structure using linked nodes. The class contains methods to add nodes to the queue, delete nodes from the queue, and traverse the queue to output the node values. A sample main function demonstrates using the queue class by adding nodes with different values, deleting nodes, and traversing the queue to output the values at each step.

Uploaded by

Keshav Aggarwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

C++ Queue Implementation Example

The document defines a queue class that implements a queue data structure using linked nodes. The class contains methods to add nodes to the queue, delete nodes from the queue, and traverse the queue to output the node values. A sample main function demonstrates using the queue class by adding nodes with different values, deleting nodes, and traversing the queue to output the values at each step.

Uploaded by

Keshav Aggarwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/* STRUCTURE_QUEUE*/

#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct queue
{
int no;
struct queue *next;
};

class que{
queue *start;
public: void add();
int del();
void traverse();
que(){
start=NULL;
}
};
void main()
{
int value,ch;
char choice;
clrscr();
que q;
do
{
//clrscr();
cout<<endl<<"----1. add"<<endl;
cout<<"----2. delete"<<endl;
cout<<"----3. traverse"<<endl;
cout<<"----4. exit"<<endl;
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{
case 1: [Link]();
break;
case 2: value=[Link]();
cout<<endl<<"the deleted element is "<<value;
break;
case 3: [Link]();
break;
case 4: return;
default : cout<<endl<<"wrong choice";
};
}
while(choice!=4);

Pritee Parewkar Page 1


cin>>choice;
}
void que::add()
{
struct queue *p,*temp;
temp=start;
p=new(struct queue);
cout<<"Enter the data ";
cin>>p->no;
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int que::del()
{
struct queue *temp;
int value;
if(start==NULL)
{
cout<<endl<<"queue is empty";
getch();
return(0);
}
else
{
temp=start;
value=temp->no;
start=start->next;
delete(temp);
}
return(value);
}
void que::traverse()
{
struct queue *temp;
temp=start;
while(temp->next!=NULL)
{

Pritee Parewkar Page 2


cout<<endl<<"no= "<<temp->no;
temp=temp->next;
}
cout<<endl<<"no="<<temp->no;
getch();
}

The output of the above program is:


----1. add
----2. delete
----3. traverse
----4. exit
Enter your choice 1
Enter the data 12

----1. add
----2. delete
----3. traverse
----4. exit
Enter your choice 1
Enter the data 23

----1. add
----2. delete
----3. traverse
----4. exit
Enter your choice 1
Enter the data 45

----1. add
----2. delete
----3. traverse
----4. exit

Enter your choice 3

no= 12
no= 23
no=45
----1. add
----2. delete
----3. traverse
----4. exit
Enter your choice 2

the deleted element is 12


----1. add
----2. delete

Pritee Parewkar Page 3


----3. traverse
----4. exit
Enter your choice 3

no= 23
no=45
----1. add
----2. delete
----3. traverse
----4. exit
Enter your choice 4

Pritee Parewkar Page 4

You might also like