Data Structur Individual Ssignment
Data Structur Individual Ssignment
AUNI
VERST
I
NSTI
TUTEOFTECHNOLOGYDEPARTMENTOFCOMPUTERSCI
ENCE
DATASTRUCTURANDALGORI
THM
I
NDI
VIDUALASSI
GNMENT.
NAME,
Wudal
ewf
ri
ew.
I
D=147530
SUBMI
TEDTO_Fasi
l.A
WOLDI
A,ETHI
OPI
A
I
mpl
ement
ati
onofSt
acks,
Queues(
usi
ngbot
har
ray
sandl
inkedl
ist
s)?
#1.
1Impl
ement
ati
onofst
acki
nar
ray
#i
ncl
ude<i
ost
ream>
#i
ncl
ude<cst
dli
b>
#i
ncl
ude<i
omani
p>
usi
ngnamespacest
d;
consti
ntMAX_
SIZE=5;
i
ntst
ack[
MAX_
SIZE]
;
i
ntt
op=-
1;
v
oidpush(
intv
al){
sy
stem(
"pause"
);
sy
stem(
"cl
s")
;
sy
stem(
"col
orA"
);
i
f(t
op==MAX_
SIZE-1){
cout<<"
Stackov
erf
low,
youcannotpushel
ement
."<<endl
;
}el
se{
t
op++;
st
ack[
top]=v
al;
cout<<v
al<<"
,ispushedi
ntot
hest
ack.
"<<endl
;
}
v
oidpop(
){
sy
stem(
"pause"
);
sy
stem(
"cl
s")
;
sy
stem(
"col
orb"
);
i
f(t
op==-
1){
cout<<"
Stacki
sempt
y,y
oucannotpop.
"<<endl
;
}el
se{
cout<<st
ack[
top]<<"
,i
spoppedf
rom t
hest
ack.
"<<endl
;
t
op-
-;
i
ntt
opEl
ement
(){
sy
stem(
"pause"
);
sy
stem(
"cl
s")
;
sy
stem(
"col
orc"
);
i
f(t
op==-
1){
cout<<"
Stacki
sempt
y."<<endl
;
r
etur
n-1;
}el
se{
r
etur
nst
ack[
top]
;
v
oiddi
spl
aySt
ack(
){
sy
stem(
"pause"
);
sy
stem(
"cl
s")
;
sy
stem(
"col
ord"
);
i
f(t
op==-
1){
cout<<"
Stacki
sempt
y."<<endl
;
}el
se{
cout<<"
Stackel
ement
sis/
are:
";
f
or(
inti
=0;
i<=t
op;
i++){
cout<<st
ack[
i]<<""
;
cout<<endl
;
i
ntmai
n(){
sy
stem(
"pause"
);
sy
stem(
"cl
s")
;
sy
stem(
"col
orE"
);
i
ntchoi
ce,
val
;
do{
cout<<"\nThi
sisstackOperat
ionsi
narray
:\n1.Push\
n2.Pop\
n3.Di
spl
ayTopEl
ement
\n4.
Di
spl
ayStack\n5.Exi
t\
nEntery
ourchoice:
";
ci
n>>choi
ce;
swi
tch(
choi
ce){
case1:
cout<<"
Ent
erv
aluet
opush:
";
ci
n>>v
al;
push(
val
);
br
eak;
case2:
pop(
);
br
eak;
case3:
cout<<"
Topel
ement
:"<< t
opEl
ement
()<<endl
;
br
eak;
case4:
di
spl
aySt
ack(
);
br
eak;
case5:
cout<<"
Exi
ti
ngpr
ogr
am.
"<<endl
;
br
eak;
def
aul
t:
cout<<"
Inv
ali
dchoi
ce.Pl
easet
ryagai
n."<<endl
;
}whi
l
e(choi
ce!
=5)
;
r
etur
n0;
##1.
2Impl
ement
ati
onofst
acki
nli
nkedl
i
st
#i
ncl
ude<i
ost
ream>
usi
ngnamespacest
d;
st
ructNode{
i
ntdat
a;
Node*next
;
}
;
Node*t
op=nul
l
ptr
;
v
oidpush(
intkey
){
Node*newNode=newNode(
);
newNode-
>dat
a=key
;
newNode-
>next=t
op;
t
op=newNode;
cout<<key<<"
,ispushedi
ntot
hest
ack.
"<<endl
;
}
v
oidpop(
){
sy
stem(
"pause"
);
i
f(t
op==nul
l
ptr
){
cout<<"
Stacki
sempt
y,cannotpop.
"<<endl
;
}el
se{
Node*t
emp=t
op;
t
op=t
op-
>next
;
cout<<t
emp-
>dat
a<<"
,i
spoppedf
rom t
hest
ack.
"<<endl
;
del
etet
emp;
i
ntt
opEl
ement
(){
sy
stem(
"pause"
);
i
f(t
op==nul
l
ptr
){
cout<<"
Stacki
sempt
y."<<endl
;
r
etur
n-1;
}el
se{
r
etur
ntop-
>dat
a;
v
oiddi
spl
aySt
ack(
){
sy
stem(
"pause"
);
i
f(t
op==nul
l
ptr
){
cout<<"
Stacki
sempt
y."<<endl
;
}el
se{
cout<<"
Stackel
ement
sis/
are:
";
Node*cur
rent=t
op;
whi
l
e(cur
rent!
=nul
l
ptr
){
cout<<cur
rent
->dat
a<<""
;
cur
rent=cur
rent
->next
;
cout<<endl
;
i
ntmai
n(){
sy
stem(
"pause"
);
i
ntchoi
ce,
key
;
do{
cout<<"\
nThisi
sSt ackOperat
ionsinl
i
nckedl
i
st:\
n1.Pushdatait
em.\n2.Popthekey
.\n3.
Di
splayTopElement
.\n4.Displ
ayStackel
ememt.
\n5.Exi
t.
\nEnt
eryourchoice:
";
ci
n>>choi
ce;
swi
tch(
choi
ce){
case1:
cout<<"
Ent
erv
aluet
opush:
";
ci
n>>key
;
push(
key
);
br
eak;
case2:
pop(
);
br
eak;
case3:
cout<<"
Topel
ementi
s:"<<t
opEl
ement
()<<endl
;
br
eak;
case4:
di
spl
aySt
ack(
);
br
eak;
case5:
cout<<"
Exi
ti
ngf
rom pr
ogr
am.
"<<endl
;
br
eak;
def
aul
t:
cout<<"
Inv
ali
dchoi
ce.Pl
easet
ryagai
n."<<endl
;
}whi
l
e(choi
ce!
=5)
;
/
/Cl
eanupt
her
emai
ningnodesi
nthest
ack
whi
l
e(t
op!
=nul
l
ptr
){
Node*t
emp=t
op;
t
op=t
op-
>next
;
del
etet
emp;
r
etur
n0;
##1.
3Impl
ement
ati
onofqueuei
nar
ray
#i
ncl
ude<i
ost
ream>
usi
ngnamespacest
d;
consti
ntMAX_
SIZE=10;
i
ntqueue[
MAX_
SIZE]
;
i
ntf
ront=-
1;
i
ntr
ear=-
1;
v
oidenqueue(
intv
al){
i
f(r
ear==MAX_
SIZE-1){
cout<<"
Queuei
sful
l
,youcannotenqueue.
"<<endl
;
}el
se{
i
f(f
ront==-
1){
f
ront=0;
r
ear
++;
queue[
rear
]=v
al;
cout<<v
al<<"
,i
senqueuedi
ntot
hequeue.
"<<endl
;
v
oiddequeue(
){
i
f(f
ront==-
1||
front>r
ear
){
cout<<"
Queuei
sempt
y,
youcannotdequeue.
"<<endl
;
}el
se{
cout<<queue[
fr
ont
]<<"
,isdequeuedf
rom t
hequeue.
"<<endl
;
f
ront
++;
i
ntf
ront
Element
(){
i
f(f
ront==-
1||
front>r
ear
){
cout<<"
Queuei
sempt
y."<<endl
;
r
etur
n-1;
}el
se{
r
etur
nqueue[
fr
ont
];
}
v
oiddi
spl
ayQueue(
){
i
f(f
ront==-
1||
front>r
ear
){
cout<<"
Queuei
sempt
y."<<endl
;
}el
se{
cout<<"
Queueel
ement
s:"
;
f
or(
inti
=fr
ont
;i<=r
ear
;i++){
cout<<queue[
i]<<""
;
cout<<endl
;
}
i
ntmai
n(){
i
ntchoi
ce,
val
;
do{
cout<<"
\nThi
sisi
mpl ment
ati
onofQueueinar
ray:\n1.Enqueuedatai
ntoqueue.
\n2.
Dequeuedat
aintoqueue.
\n3.Di
splayFr
ontEl
ement.
\n4.Displ
ayallQueueel
ement.
\n5.
Exi
t\nEnt
ery
ourchoi
ce:";
ci
n>>choi
ce;
swi
tch(
choi
ce){
case1:
cout<<"
Ent
erv
aluet
oenqueue:
";
ci
n>>v
al;
enqueue(
val
);
br
eak;
case2:
dequeue(
);
br
eak;
case3:
cout<<"
Frontel
ement
:"<<f
ront
Element
()<<endl
;
br
eak;
case4:
di
spl
ayQueue(
);
br
eak;
case5:
cout<<"
Exi
ti
ngpr
ogr
am.
"<<endl
;
br
eak;
def
aul
t:
cout<<"
Inv
ali
dchoi
ce.Pl
easet
ryagai
n."<<endl
;
}
}whi
l
e(choi
ce!
=5)
;
r
etur
n0;
##1.
4Impl
ement
ati
onofqueuei
nli
nkedl
i
st
#i
ncl
ude<i
ost
ream>
usi
ngnamespacest
d;
st
ructNode{
i
ntdat
a;
Node*next
;
}
;
Node*
str
_pt
r=nul
l
ptr
;
Node*f
ront=st
r_pt
r;
Node*r
ear=st
r_pt
r;
v
oidenqueue(
intv
al){
sy
stem(
"pause"
);
Node*newNode=newNode(
);
newNode-
>dat
a=v
al;
newNode-
>next=st
r_pt
r;
i
f(r
ear==st
r_pt
r){
f
ront=r
ear=newNode;
}el
se{
r
ear
->next=newNode;
r
ear=newNode;
cout<<v
al<<"enqueuedi
ntot
hequeue.
"<<endl
;
}
v
oiddequeue(
){
sy
stem(
"pause"
);
i
f(f
ront==st
r_pt
r){
cout<<"
Queuei
sempt
y,cannotdequeue.
"<<endl
;
}el
se{
Node*t
emp=f
ront
;
f
ront=f
ront
->next
;
i
f(f
ront==st
r_pt
r){
r
ear=st
r_pt
r;
cout<<t
emp-
>dat
a<<"dequeuedf
rom t
hequeue.
"<<endl
;
del
etet
emp;
}
i
ntf
ront
Element
(){
sy
stem(
"pause"
);
i
f(f
ront==st
r_pt
r){
cout<<"
Queuei
sempt
y."<<endl
;
r
etur
n-1;
}el
se{
r
etur
nfr
ont
->dat
a;
v
oiddi
spl
ayQueue(
){
sy
stem(
"pause"
);
i
f(f
ront==st
r_pt
r){
cout<<"
Queuei
sempt
y."<<endl
;
}el
se{
cout<<"
Queueel
ement
s:"
;
Node*cur
rent=f
ront
;
whi
l
e(cur
rent!
=st
r_pt
r){
cout<<cur
rent
->dat
a<<""
;
cur
rent=cur
rent
->next
;
cout<<endl
;
i
ntmai
n(){
sy
stem(
"pause"
);
i
ntchoi
ce,
val
;
do{
cout<<"
\nThi
sisQueueOperati
onsinli
nckedl
ist
:\
n1.
ForEnqueuedat
ai t
em i
ntothe
queue.\
n2.
ForDequeuedataval
uesfrom t
hequeue.
\n3.
ForDi
splayFr
ontval
ues.
\n4.
ForDi
spl
ay
QueueElement.
\n5.
Exi
t.
\nEnt
eryourchoi
ce:";
ci
n>>choi
ce;
swi
tch(
choi
ce){
case1:
cout<<"
Ent
erv
aluet
oenqueue:
";
ci
n>>v
al;
enqueue(
val
);
br
eak;
case2:
dequeue(
);
br
eak;
case3:
cout<<"
Frontel
ementi
s:"<<f
ront
Element
()<<endl
;
br
eak;
case4:
di
spl
ayQueue(
);
br
eak;
case5:
cout<<"
Exi
ti
ngpr
ogr
am.
"<<endl
;
br
eak;
def
aul
t:
cout<<"
Inv
ali
dchoi
ce.Pl
easet
ryagai
n."<<endl
;
}
}whi
l
e(choi
ce!
=5)
;
/
/Cl
eanupt
her
emai
ningnodesi
nthequeue
whi
l
e(f
ront!
=st
r_pt
r){
Node*t
emp=f
ront
;
f
ront=f
ront
->next
;
del
etet
emp;
r
etur
n0;
2.
Impl
ement
ati
onofI
nfi
xtopost
fi
x;conv
ersi
onandev
aluat
ionofpost
fi
xexpr
essi
on.
#i
ncl
ude<i
ost
ream>
#i
ncl
ude<st
ack>
#i
ncl
ude<st
ri
ng>
usi
ngnamespacest
d;
i
ntpr
ecedence(
charc){
i
f(c=='
+'|
|c=='
-
'){
r
etur
n1;
}el
sei
f(c=='
*'|
|c=='
/'
){
r
etur
n2;
r
etur
n0;
st
ri
ngi
nfi
xToPost
fi
x(st
ri
ngi
nfi
x){
st
ack<char
>oper
ator
s;
st
ri
ngpost
fi
x="
";
f
or(
charc:
inf
ix){
i
f(c=='
'){
cont
inue;
}el
sei
f(i
sdi
git
(c)
){
post
fi
x+=c;
}el
sei
f(c=='
('
){
oper
ator
s.push(
c);
}el
sei
f(c=='
)'
){
whi
l
e(!
oper
ator
s.empt
y()&&oper
ator
s.t
op(
)!='
('
){
post
fi
x+=oper
ator
s.t
op(
);
oper
ator
s.pop(
);
oper
ator
s.pop(
);/
/Di
scar
dthe'
('
}el
se{
whi
l
e(!
oper
ator
s.empt
y()&&pr
ecedence(
oper
ator
s.t
op(
))>=pr
ecedence(
c)){
post
fi
x+=oper
ator
s.t
op(
);
oper
ator
s.pop(
);
oper
ator
s.push(
c);
whi
l
e(!
oper
ator
s.empt
y()
){
post
fi
x+=oper
ator
s.t
op(
);
oper
ator
s.pop(
);
r
etur
npost
fi
x;
}
i
ntev
aluat
ePost
fi
x(st
ri
ngpost
fi
x){
st
ack<i
nt>oper
ands;
f
or(
charc:
post
fi
x){
i
f(i
sdi
git
(c)
){
oper
ands.
push(
c-'
0'
);
}el
se{
i
ntoper
and2=oper
ands.
top(
);
oper
ands.
pop(
);
i
ntoper
and1=oper
ands.
top(
);
oper
ands.
pop(
);
swi
tch(
c){
case'
+'
:
oper
ands.
push(
oper
and1+oper
and2)
;
br
eak;
case'
-
':
oper
ands.
push(
oper
and1-oper
and2)
;
br
eak;
case'
*'
:
oper
ands.
push(
oper
and1*oper
and2)
;
br
eak;
case'
/'
:
oper
ands.
push(
oper
and1/oper
and2)
;
br
eak;
}
r
etur
noper
ands.
top(
);
i
ntmai
n(){
st
ri
ngi
nfi
x;
cout<<"
Ent
erani
nfi
xexpr
essi
on:
";
get
li
ne(
cin,
inf
ix)
;
st
ri
ngpost
fi
x=i
nfi
xToPost
fi
x(i
nfi
x);
cout<<"
Post
fi
xexpr
essi
on:
"<<post
fi
x<<endl
;
i
ntr
esul
t=ev
aluat
ePost
fi
x(post
fi
x);
cout<<"
Resul
t:"<<r
esul
t<<endl
;
r
etur
n0;
3.
Wri
teaC++Pr
ogr
am t
oimpl
ementbal
ancedpar
ent
hesi
susi
ngst
acks?
#i
ncl
ude<i
ost
ream>
#i
ncl
ude<st
ack>
#i
ncl
ude<st
ri
ng>
usi
ngnamespacest
d;
bool
isBal
anced(
str
ingexpr
essi
on){
st
ack<char
>s;
f
or(
charc:
expr
essi
on){
i
f(c=='
('
||c=='
[
'||
c=='
{'
){
s.
push(
c);
}el
sei
f(c=='
)'
||c=='
]
'||
c=='
}'
){
i
f(s.
empt
y()
){
r
etur
nfal
se;
chart
op=s.
top(
);
s.
pop(
);
i
f((
c=='
)'
&&t
op!
='(
')|
|(c=='
]
'&&t
op!
='[
'
)||
(c=='
}'
&&t
op!
='{
')
){
r
etur
nfal
se;
r
etur
ns.
empt
y()
;
}
i
ntmai
n(){
st
ri
ngexpr
essi
on;
cout<<"
Ent
eranexpr
essi
onwi
thpar
ent
heses:
"<<endl
;
get
li
ne(
cin,
expr
essi
on)
;
i
f(i
sBal
anced(
expr
essi
on)
){
cout<<"
Thepar
ent
hesesi
ntheexpr
essi
onar
ebal
anced.
\n"
;
}el
se{
cout<<"
Thepar
ent
hesesi
ntheexpr
essi
onar
enotbal
anced.
\n"
;
r
etur
n0;
}
4.
Wri
tet
hei
mpl
ement
ati
onoft
hesi
ngl
eli
nkedl
i
stoper
ati
onusi
ngC++?
#i
ncl
ude<i
ost
ream>
usi
ngnamespacest
d;
st
ructNode{
i
ntdat
a;
Node*next
;
}
;
Node*head=nul
l
ptr
;
v
oidi
nser
tAt
Begi
nni
ng(
intv
alue){
Node*newNode=newNode;
newNode-
>dat
a=v
alue;
newNode-
>next=head;
head=newNode;
v
oidi
nser
tAt
End(
intv
alue){
Node*newNode=newNode;
newNode-
>dat
a=v
alue;
newNode-
>next=nul
l
ptr
;
i
f(head==nul
l
ptr
){
head=newNode;
r
etur
n;
}
Node*t
emp=head;
whi
l
e(t
emp-
>next!
=nul
l
ptr
){
t
emp=t
emp-
>next
;
t
emp-
>next=newNode;
v
oidi
nser
tAt
Posi
ti
on(
intv
alue,
intposi
ti
on){
Node*newNode=newNode;
newNode-
>dat
a=v
alue;
i
f(posi
ti
on==1){
newNode-
>next=head;
head=newNode;
r
etur
n;
Node*t
emp=head;
f
or(
inti
=1;
i<posi
ti
on-1&&t
emp!
=nul
l
ptr
;i++){
t
emp=t
emp-
>next
;
i
f(t
emp==nul
l
ptr
){
cout<<"
Inv
ali
dposi
ti
on.
"<<endl
;
r
etur
n;
}
newNode-
>next=t
emp-
>next
;
t
emp-
>next=newNode;
v
oiddel
eteFr
omBegi
nni
ng(
){
i
f(head==nul
l
ptr
){
cout<<"
Listi
sempt
y."
<<endl
;
r
etur
n;
Node*t
emp=head;
head=head-
>next
;
del
etet
emp;
v
oiddel
eteFr
omEnd(
){
i
f(head==nul
l
ptr
){
cout<<"
Listi
sempt
y."
<<endl
;
r
etur
n;
i
f(head-
>next==nul
l
ptr
){
del
etehead;
head=nul
l
ptr
;
r
etur
n;
}
Node*t
emp=head;
whi
l
e(t
emp-
>next
->next!
=nul
l
ptr
){
t
emp=t
emp-
>next
;
del
etet
emp-
>next
;
t
emp-
>next=nul
l
ptr
;
v
oiddel
eteAt
Posi
ti
on(
intposi
ti
on){
i
f(head==nul
l
ptr
){
cout<<"
Listi
sempt
y."
<<endl
;
r
etur
n;
i
f(posi
ti
on==1){
Node*t
emp=head;
head=head-
>next
;
del
etet
emp;
r
etur
n;
Node*t
emp=head;
f
or(
inti
=1;
i<posi
ti
on-1&&t
emp!
=nul
l
ptr
;i++){
t
emp=t
emp-
>next
;
}
i
f(t
emp==nul
l
ptr|
|temp-
>next==nul
l
ptr
){
cout<<"
Inv
ali
dposi
ti
on.
"<<endl
;
r
etur
n;
Node*nodeToDel
ete=t
emp-
>next
;
t
emp-
>next=nodeToDel
ete-
>next
;
del
etenodeToDel
ete;
v
oiddi
spl
ayLi
st(
){
Node*t
emp=head;
whi
l
e(t
emp!
=nul
l
ptr
){
cout<<t
emp-
>dat
a<<""
;
t
emp=t
emp-
>next
;
cout<<endl
;
bool
sear
chKey
(intkey
){
Node*t
emp=head;
whi
l
e(t
emp!
=nul
l
ptr
){
i
f(t
emp-
>dat
a==key
){
r
etur
ntr
ue;
//Keyf
ound
}
t
emp=t
emp-
>next
;
r
etur
nfal
se;
//Keynotf
ound
i
ntmai
n(){
i
ntchoi
ce,
val
ue,
posi
ti
on,
key
;
do{
cout<<"
Menuf
orsi
ngl
eli
nckedl
i
stoper
sti
on.
"<<endl
;
cout<<"
1.I
nser
tatBegi
nni
ng.
"<<endl
;
cout<<"
2.I
nser
tatEnd.
"<<endl
;
cout<<"
3.I
nser
tatPosi
ti
on.
"<<endl
;
cout<<"
4.Del
etef
rom Begi
nni
ng.
"<<endl
;
cout<<"
5.Del
etef
rom End.
"<<endl
;
cout<<"
6.Del
eteatPosi
ti
on.
"<<endl
;
cout<<"
7.Di
spl
ayLi
st.
"<<endl
;
cout<<"
8.Sear
chKey
."<<endl
;
cout<<"
9.Exi
t.
"<<endl
;
cout<<"
Ent
ery
ourchoi
ce:
"<<endl
;
ci
n>>choi
ce;
swi
tch(
choi
ce){
case1:
cout<<"
Ent
erv
aluet
oinser
t:"
<<endl
;
ci
n>>v
alue;
i
nser
tAt
Begi
nni
ng(
val
ue)
;
br
eak;
case2:
cout<<"
Ent
erv
aluet
oinser
t:"
<<endl
;
ci
n>>v
alue;
i
nser
tAt
End(
val
ue)
;
br
eak;
case3:
cout<<"
Ent
erv
aluet
oinser
t:"
<<endl
;
ci
n>>v
alue;
cout<<"
Ent
erposi
ti
on:
"<<endl
;
ci
n>>posi
ti
on;
i
nser
tAt
Posi
ti
on(
val
ue,
posi
ti
on)
;
br
eak;
case4:
del
eteFr
omBegi
nni
ng(
);
br
eak;
case5:
del
eteFr
omEnd(
);
br
eak;
case6:
cout<<"
Ent
erposi
ti
ont
odel
ete:
"<<endl
;
ci
n>>posi
ti
on;
del
eteAt
Posi
ti
on(
posi
ti
on)
;
br
eak;
case7:
di
spl
ayLi
st(
);
br
eak;
case8:
cout<<"
Ent
erkeyt
osear
ch:
"<<endl
;
ci
n>>key
;
i
f(sear
chKey
(key
)){
cout<<"
Keyf
oundi
nthel
i
st.
"<<sear
chKey
(key
)<<endl
;
}el
se{
cout<<"
Keynotf
oundi
nthel
i
st.
"<<endl
;
br
eak;
case9:
cout<<"
Exi
ti
ngt
hepr
ogr
am.
..
"<<endl
;
br
eak;
def
aul
t:
cout<<"
Inv
ali
dchoi
ce.Pl
easet
ryagai
n."
<<endl
;
}
}whi
l
e(choi
ce!
=9)
;
r
etur
n0;