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

Circular Queue Linkedlist Adt

The document contains a C++ implementation of a circular queue using a linked list. It includes methods for enqueueing, dequeueing, checking if the queue is empty, and displaying the queue contents. The main function provides a menu-driven interface for user interaction with the queue operations.

Uploaded by

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

Circular Queue Linkedlist Adt

The document contains a C++ implementation of a circular queue using a linked list. It includes methods for enqueueing, dequeueing, checking if the queue is empty, and displaying the queue contents. The main function provides a menu-driven interface for user interaction with the queue operations.

Uploaded by

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

#incl

ude<i
ostream>
usingnamespacest
d;

str
uctNode{
i
ntdata;
Node*next
;
};

cl
assCircul
arQueue{
pr
ivate:
Node*front
;
Node*rear;

publ
i
c:
Cir
cul
arQueue(
):f
ront
(nul
l
ptr
),r
ear
(nul
l
ptr
){}

bool
isEmpt
y()
{
ret
urnf
ront==nul
l
ptr
;
}

v
oidenQueue(
intv
alue)
{
Node*newNode=newNode(
);
newNode-
>data=value;
newNode-
>next=null
ptr
;

i
f(i
sEmpt y(
))
{
front=rear=newNode;
rear-
>next=fr
ont;
//Poi
ntr
eart
ofr
ontf
orci
rcul
arbehav
ior
}
el
se
{
rear-
>next=newNode;
rear=newNode;
rear-
>next=fr
ont;
//Poi
ntr
eart
ofr
ontf
orci
rcul
arbehav
ior
}
}

i
ntdeQueue()
{
if(
isEmpty(
))
{
cout<<"Queueisempty.
"<<endl
;
ret
urn-1;/
/Indi
cateanerr
ororunder
fl
owcondi
ti
on
}

i
ntdata=fr
ont
->data;
Node*temp=front
;

i
f(f
ront==rear){
//Ift
here'sonl
yoneelementi
nthequeue,
resetf
rontandr
ear
.
fr
ont=r ear=null
ptr
;
}
el
se
{
front=front
->next
;
rear-
>next=front;
//Updat
ereart
opoi
ntt
othenewf
rontf
orci
rcul
arbehav
ior
}

del
etetemp;
ret
urndata;
}

v
oiddi
splay
(){
i
f(i
sEmpty(
)){
cout<<"Queuei
sempt
y."<<endl
;
ret
urn;
}

Node*current=front;
do
{
cout<<cur r
ent->dat
a<<"";
current=current-
>next
;
}whil
e(current!
=f r
ont);
//Trav
erset
heci
rcul
arqueue

cout<<endl
;
}
}
;

i
ntmain(
)
{
Ci
rcul
arQueuemyQueue;
i
ntchoi
ce,val
ue;

do{
cout<<"
\nMenu:\n"
;
cout<<"
1.Enqueue\n";
cout<<"
2.Dequeue\n";
cout<<"
3.CheckifEmpt y
\n";
cout<<"
4.Quit
\n";
cout<<"
Enteryourchoi
ce:";
ci
n>>choice;

swi
tch(choice){
case1:
cout<<" Ent erval
uet oenqueue:"
;
cin>>v alue;
my Queue. enQueue( v
alue);
break;
case2:
value=my Queue.deQueue();
i
f( value!=- 1)
cout<<" Dequeuedv al
ue:"<<val
ue<<endl
;
break;
case3:
i
f( my Queue. i
sEmpt y
())
cout<<" Queueisempt y.
"<<endl
;
el
se
cout<<"Queueisnotempt y.
"<<endl;
break;
case4:
cout<<"Exiti
ngtheprogr
am. "<<endl;
break;
defaul
t:
cout<<"Inval
idchoi
ce.Pleasetryagai
n."<<endl
;
break;
}

myQueue.
displ
ay(
);/
/Di
spl
ayt
heci
rcul
arqueueaf
tereachoper
ati
on
}whi
l
e(choi
ce!=4);

r
etur
n0;
}

You might also like