Sunteți pe pagina 1din 60

1

Array Implementation of Linked List


Exp No:1 Aim:
To write a program to implement the linked list using array. Algorithm: Step 1:Start. Step 2:Read the size and elements o the array. Step !:Read the re"uired #hoi#e. Step $:Read the elements and position or insertion. Step %:&nsert and display the array elements. Step ':(or deletion read the position o the element. Step ):*isplay the deleted element. Step +:(or rea#hing read the num,er to ,e sear#hed. Step -:*isplay the element position. Step 1.:Stop.

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0de ine size 1. stru#t array 3 int 4alue5 int #hain5 6 a7size85 4oid main9:

3 int n;i;position;#hoi#e;start5 #har ans5 int #reate9stru#t array a78;int:5 4oid display9stru#t array a78;int:5 int sear#h9stru#t array a78;int;int:5 #lrs#r9:5 print 9<=n program or array implementaion o >ist <:5 do 3 #lrs#r9:5 print 9<=n=t=t main menu<:5 print 9<=n1.?reate=n2.*isplay=n!.sear#h<:5 s#an 9<@d<;A#hoi#e:5 swit#h9#hoi#e: 3 #ase 1: print 9<=n Bow many elements *o u want in >ist C<:5 s#an 9<@d<;An:5 or9iD.5i1n5iEE: 3 a7i8.4alueDF15 a7i8.#hainDF15 6 startD#reate9a;n:5 ,reak5 #ase 2: display9a;start:5 ,reak5 #ase !: print 9<=n Enter the position at or sear#hing: <:5 s#an 9<@d<;Aposition:5 iDsear#h9a;start;position:5 i 9iGDF1: print 9<The @d postion india#ate as a 4alue@d<;position;a7i8.4alue:5 ,reak5 de ault:print 9<=n "uit<:5 ,reak5 6 print 9<=n *o u want to #on...<:5 ansDget#h9:5 6

while9ansDDHyH:5 get#h9:5 6 int #reate9stru#t array a7size8;int n: 3 int i; lagD.;index;startIindex;pre4Iindex5 i 9n2size: 3 print 9<Elements ex#eed the list size<:5 get#h9:5 exit9.:5 6 iD.5 print 9<=n Enter the lo#ation and 4alue at that lo#ation<:5 while9i1n: 3 i 9 lagDD.: 3 s#an 9<@d<;Aindex:5 s#an 9<@d<;Aa7index8.4alue:5 startIindexDindex5 pre4IindexDstartIindex5 lagD15 6 else 3 s#an 9<@d<;Aindex:5 s#an 9<@d<;Aa7index8.4alue:5 a7pre4Iindex8.#hainDindex5 pre4IindexDindex5 6 iEE5 6 a7index8.#hainDF--5 return startIindex5 6 4oid display9stru#t array a7size8;int startIindex: 3 int i5 print 9<=n J h4 entered the list=n<:5 or9iDstartIindex5a7i8.#hainGDF--5iDa7i8.#hain: print 9<@dFF2<;a7i8.4alue:5 print 9<@dFF2NJ>><;a7i8.4alue:5

6 int sear#h9stru#t array a7size8;int startIindex;int position: 3 int i;#ountD.5 or9iDstartIindex5a7i8.#hainGDF--5iDa7i8.#hain: 3 #ountEE5 i 9#ountDDposition: return i5 6 print 9<=n The element is not present<:5 return F15 6

Sample &nputKLutput:
Main menu 1.?reate 2.*isplay !.Sear#h Enter your #hoi#e: 1 Bow many elements do you want to listC ! Enter the lo#ation and 4alue at that lo#ation: 1 2. 2 21 ! 22 *o you want to #ontinue....... Enter your #hoi#e: 2 Nou ha4e entered the list 2. O 21O 22ONJ>> *o you want to #ontinue.......

Enter your #hoi#e: ! Enter the position or sear#hing: 2 The 2 position indi#ates as a 4alue 21 *o you want to #ontinuePP.

Result:
Thus the program or array implementation using linked list has ,een exe#uted su##ess ully and output is 4eri ied.

Implementation of Linked List Using Pointer


Exp No:2 Aim:
To write a program to implement the linked list using pointer. Algorithm: Step 1:start. Step 2:#all un#tion #reate9nodeQp: Step !:#all #ount9nodeQp: Step $:headD9nodeQ:mallo#9sizeo 9node:: Step %:#reate9head: Step ':print head. Step ):stop. ?reation o list: Step 1:read listF2num,er Step 2:i 9listF2num,erDDF--:;then Step !:listF2next the NJ>>;else Step $:listF2nextD9nodeQ:mallo#9sizeo 9node:: Step %:#reate9listR1:;next Step ':return Step ):stop

Sear#h:

Step1:Set the element whi#h to ,e sear#hed. Step 2:i 9tempF2dataGDkey: Step !:tempDtempF2next; Else Step $: oundDTRJE5 Step%:The element is present is the list Step':else Element is not present in the list *elete: Step1:Set the Element whi#h is to ,e *eleted. Step2: tempDsear#h9Qhead;key:5 Step!: i 9tempGDNJ>>: Step$:pre4DgetIpre49Qhead;key:5 Step%:i 9pre4GDNJ>>: Step':pre4F2nextDtempF2next5 Step): ree9temp:5else Step+:QheadDtempF2next5 Step-: ree9temp:5 Step1.:The element is deleted *isplay: Step 1:i 9listF2nextGDNJ>>:then print listF2num,er Step 2:return Step !:int #ount9nodeQlist: Step $:i 9listEnextDDNJ>>:;return;else Step %:return91E#ount9listF2next:: Step ':return Step ):stop

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0de ine TRJE 1 0de ine (A>SE . typede stru#t S>> 3 int data5 stru#t S>> Qnext5 6node5 node Q#reate9:5 4oid main9: 3 int #hoi#e;4al5 #har ans5 node Qhead5 4oid display9nodeQ:5 node Qsear#h9nodeQ;int:5 4oid insert9nodeQ:5 4oid dele9nodeQQ:5 headDNJ>>5 do 3 #lrs#r9:5 print 9<=tSingly >inked >ist <:5 print 9<=n1.?reate<:5 print 9<=n2.*isplay=n!.sear#h<:5 print 9<=n$.&nsert<:5 print 9<=n%.*elete<:5 print 9<=n'.Tuit<:5 print 9<=nEnter your #hoi#e:=n<:5 s#an 9<@d<;A#hoi#e:5 swit#h9#hoi#e: 3 #ase 1: headD#reate9:5 ,reak5 #ase 2: display9head:5 ,reak5 #ase !:

print 9<Enter the element u want to sear#h:=n<:5 s#an 9<@d<;A4al:5 sear#h9head;4al:5 ,reak5 #ase $: insert9head:5 ,reak5 #ase %: dele9Ahead:5 ,reak5 #ase ': exit9.:5 de ault: #lrs#r9:5 print 9<&n4alid #hoi#e...<:5 get#h9:5 6 6 while9#hoi#eGD.:5 6 node Q#reate9: 3 node Qtemp;QNew;Qhead5 int 4al; lag5 #har ansDHyH5 node QgetInode9:5 temp DNJ>>5 lagDTRJE5 do 3 print 9<=nEnter the element:=n<:5 s#an 9<@d<;A4al:5 NewDgetInode9:5 i 9NewDDNJ>>: print 9<=nMemory is not allo#ated.<:5 NewF2dataD4al5 i 9 lag: 3 headDNew5 tempDhead5 lagD(A>SE5 6 else

10

3 tempF2nextDNew5 tempDNew5 6 print 9<=n*o u want to enter more elementsC9yKn:C=n<:5 ansDget#h9:5 6 while9ansDDHyH:5 print 9<=nThe singly list is #reated:=n<:5 get#h9:5 #lrs#r9:5 return head5 6 node QgetInode9: 3 node Qtemp5 tempD9nodeQ:mallo#9sizeo 9node::5 tempF2nextDNJ>>5 return temp5 6 4oid display9node Qhead: 3 node Qtemp5 tempDhead5 i 9tempDDNJ>>: 3 print 9<=nThe list is empty.=n<:5 get#h9:5 #lrs#r9:5 return5 6 while9tempGDNJ>>: 3 print 9<@dF2<;tempF2data:5 tempDtempF2next5 6 print 9<NJ>><:5 get#h9:5 #lrs#r9:5 6 node Qsear#h9node Qhead;int key: 3 node Qtemp5

11

int ound5 tempDhead5 i 9tempDDNJ>>: 3 print 9<The list is empty.=n<:5 get#h9:5 #lrs#r9:5 return NJ>>5 6 oundD(A>SE5 while9tempGDNJ>>AAG ound: 3 i 9tempF2dataGDkey: tempDtempF2next5 else oundDTRJE5 6 i 9 ound: 3 print 9<=nThe element is present is the list.=n<:5 get#h9:5 return temp5 6 else 3 print 9<Element is not present in the list.=n<:5 get#h9:5 return NJ>>5 6 6 4oid insert9node Qhead: 3 node Qtemp;QNew5 int 4al5 tempDhead5 i 9tempDDNJ>>: 3 print 9<=n &nsertion is not possi,le.=n<:5 get#h9:5 return5 6 #lrs#r9:5 print 9<=nEnter the element a ter whi#h is want to insertC=n<:5

12

s#an 9<@d<;A4al:5 tempDsear#h9head;4al:5 i 9tempGDNJ>>: s#an 9<@d<;A4al:5 NewD9nodeQ:mallo#9sizeo 9node::5 i 9NewDDNJ>>: print 9<Memory is not allo#ated.=n<:5 NewF2dataD4al5 NewF2nextDNJ>>5 NewF2nextDtempF2next5 tempF2nextDNew5 print 9<=nThe element is inserted.=n<:5 get#h9:5 #lrs#r9:5 6 node QgetIpre49node Qhead;int 4al: 3 node Qtemp;Qpre45 int lag5 tempDhead5 i 9tempDDNJ>>: return NJ>>5 lagD(A>SE5 pre4DNJ>>5 while9tempGDNJ>>AAG lag: 3 i 9tempF2dataGD4al: 3 pre4Dtemp5 tempDtempF2next5 6 else lagDTRJE5 6 i 9 lag: return pre45 else return NJ>>5 6 4oid dele9node QQhead: 3 node Qtemp;Qpre45 int key5

13

tempDQhead5 i 9tempDDNJ>>: 3 print 9<=nThe list is empty.<:5 get#h9:5 #lrs#r9:5 return5 6 #lrs#r9:5 print 9<=nEnter the element u want to delete:=n<:5 s#an 9<@d<;Akey:5 tempDsear#h9Qhead;key:5 i 9tempGDNJ>>: 3 pre4DgetIpre49Qhead;key:5 i 9pre4GDNJ>>: 3 pre4F2nextDtempF2next5 ree9temp:5 6 else 3 QheadDtempF2next5 ree9temp:5 6 print 9<=nThe element is deleted.=n<:5 get#h9:5 6 6

Sample &nputKLutput:
Singly >inked >ist 1.?reate 2.*isplay !.sear#h $.&nsert %.*elete '.Tuit Enter your #hoi#e: 1

14

Enter the element: 11 *o u want to enter more elementsC9yKn:C Enter the element: 1$ *o u want to enter more elementsC9yKn:C Enter the element: 1% *o u want to enter more elementsC9yKn:C The singly list is #reated: Enter your #hoi#e: 2 11F21$F21%F2NJ>> Enter your #hoi#e: ! Enter the element u want to sear#h: 11 The element is present is the list. Enter the element a ter whi#h is want to insertC 11 The element is present is the list. -The element is inserted. Enter the element u want to delete: -The element is present is the list.

Result:
Thus the program or implementation o linked list using pointer has ,een exe#uted su##ess ully and output is 4eri ied.

15

Cursor Implementation of List


Exp No:! Aim:
To write a program or #ursor implementation o linked list. Algorithm: Step 1:Start. Step 2:Read the size and elements o the array. Step !:Read the re"uired #hoi#e. Step $:Read the elements and position or insertion. Step %:&nsert and display the array elements. Step ':(or deletion read the position o the element. Step ):*isplay the deleted element. Step +:(or rea#hing read the num,er to ,e sear#hed. Step -:*isplay the element position. Step 1.:Stop.

16

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0de ine MAU 2. int list7MAU85 4oid main9: 3 int #hoi#e;len;pos5 int #reate9:5 #har ans5 4oid display9int:5 int sear#h9int:5 4oid delet9int:5 do 3 #lrs#r9:5 print 9<Main menu=n<:5 print 9<=n1.#reate<:5 print 9<=n2.*isply<:5 print 9<=n!.Sear#h or a num,er<:5 print 9<=n$.*elete<:5 print 9<=n%."uit<:5 print 9<=nEnter ur ?hoi#e 91F':<:5 s#an 9<@d<;A#hoi#e:5 swit#h9#hoi#e: 3 #ase 1: lenD#reate9:5 ,reak5 #ase 2: display9len:5 ,reak5 #ase !: posDsear#h9len:5 ,reak5 #ase $: delet9len:5 ,reak5 #ase %: print 9<=n *o u want to Exit9yKn:<:5 ansDget#he9:5

17

i 9ansDDHyH: exit9.:5 else ,reak5 de ault: #lrs#r9:5 print 9<=n &n4alid ?hoi#e;Try again<:5 get#h9:5 6 6 while9#hoi#eGD%:5 6 int #reate9: 3 int n;i5 #lrs#r9:5 print 9<Bow many elements u want in the list:<:5 s#an 9<@d<;An:5 i 9n2MAU: print 9<=n Error no o Elements ex#eeds the limit<:5 or9iD.5i1n5iEE: 3 print 9<Enter the element Num,er@d<;iE1:5 s#an 9<@d<;Alist7i8:5 6 print 9<The list is #reated<:5 get#h9:5 return9n:5 6 4oid display9int n: 3 int i5 #lrs#r9:5 print 9<=n The >ist is ....=n<:5 or9iD.5i1n5iEE: 3 i 9list7i8GDF1: print 9<@dFF2<;list7i8:5 6 print 9<=n /resss any key to #ontinue....=n<:5 get#h9:5 6

18

int sear#h9int n: 3 int i;key5 #lrs#r9:5 print 9<=n Enter the num,er you want sear#h<:5 s#an 9<@d<;Akey:5 or9iD.5i1n5iEE: 3 i 9list7i8DDkey: 3 print 9<=n the Si4en num,eris at position @d=n<;iE1:5 get#h9:5 return i5 6 6 print 9<=n The gi4en num,er is not present in the list=n<:5 get#h9:5 6 4oid delet9int n: 3 int i5 iDsear#h9n:5 list7i8DF15 print 9<=n The elements is now deleted<:5 get#h9:5 6

19

Sample &nputKLutput:
Main menu 1.?reate 2.*isplay !.Sear#h Enter your #hoi#e: 1 Bow many elements do you want to listC ! Enter the lo#ation and 4alue at that lo#ation: $ 2. % 21 ' 22 *o you want to #ontinue....... Enter your #hoi#e: 2 Nou ha4e entered the list 2. O 21O 22ONJ>> *o you want to #ontinue....... Enter your #hoi#e: ! Enter the position or sear#hing: 2 The 2 position indi#ates as a 4alue 21 *o you want to #ontinuePP.

Result:
Thus the program or #ursor implementation o linked list has ,een exe#uted su##ess ully and output is 4eri ied.

20

Implementation of Stack Using Array


Exp No: $ Aim:
To write a program to implement a sta#k using array. Algorithm: Step 1:start. Step 2:read the re"uired #hoi#e using swit#h #ase. Step !:#all the re"uired un#tions. Step $:stop. /ush: Step 1:#he#k i sta#k is ull; then Step 2:display sta#k o4er lows; else Step !:#he#k i sta#k is empty; then Step $:read the 4alue ,y in#rementing top 4alue Step %:stop. /op: Step 1:#he#k i the sta#k is empty; then Step 2:display sta#k under lows; else Step !:#he#k i top is not e"ual to .; then Step $:display the top 4alue and delete the element Step %:stop.

21

*isplay: Step 1:#he#k whether the sta#k is empty. Step 2:display the sta#k. Step !:stop.

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0de ine size % stru#t sta#k 3 int s7size85 int top5 6 st5 int st ull9: 3 i 9st.top2DsizeF1: return 15 else return .5 6 4oid push9int item: 3 st.topEE5 st.s7st.top8Ditem5 6 int stempty9: 3 i 9st.topDDF1: return 15 else return .5 6 int pop9: 3 int item5 itemDst.s7st.top85

22

st.topFF5 return9item:5 6 4oid display9: 3 int i5 i 9stempty9:: print 9<=n sta#k is emptyG<:5 else 3 or9iDst.top5i2D.5iFF: print 9<=n@d<;st.s7i8:5 6 6 4oid main9:3 int item;#hoi#e5 #har ans5 st.topDF15 #lrs#r9:5 print 9<=n=t=t &mplementation o sta#k<:5 do 3 print 9<=nMain Menu<:5 print 9<=n 1./ush=n 2./op=n !.*isplay=n $.exit<:5 print 9<=nenter your #hoi#e =n<:5 s#an 9<@d<;A#hoi#e:5 swit#h9#hoi#e: 3 #ase 1:print 9<=n Enter The &tem to pushed=n<:5 s#an 9<@d<;Aitem:5 i 9st ull9:: print 9<=nsta#k is ullG<:5 else push9item:5 ,reak5 #ase 2:i 9stempty9:: print 9<=n Empty Sta#k Jnde(lowGG<:5 else 3 itemDpop9:5 print 9<=nThe poped element is @d<;item:5 6 ,reak5

23

#ase !: display9:5 ,reak5 #ase $:exit9.:5 6 print 9<=n do u want to #ontinueC<:5 ansDget#h9:5 6 while9ansDDHyHVVansDDHyH:5 get#h9:5 6

Sample &nputKLutput:
Main Menu 1./ush 2./op !.*isplay $.Exit Enter your #hoi#e: 1 Enter The &tem to ,e pushed 11 *o you want to #ontinueC Enter your #hoi#e: 1 *o you want to #ontinueC Enter your #hoi#e: 1 Enter The &tem to ,e pushed %' *o you want to #ontinueC Enter your #hoi#e: ! %' !! 11 *o you want to #ontinueC

24

Enter your #hoi#e: 2 Enter The &tem to ,e poped: %' *o you want to #ontinueC Enter your #hoi#e: ! !! 11 *o you want to #ontinueC Enter your #hoi#e:$

Result:
Thus the program or implement a sta#k using array has ,een exe#uted su##ess ully and output is 4eri ied.

25

Implementation of Stack Using Linked List


Exp No: % Aim:
To write a program to implement a sta#k using linked list. Algorithm: Step 1:Start. Step 2:Read the #hoi#e. Step !:?all the re"uired un#tions. Step $:Stop. /ush: Step 1:?he#k i sta#k is ull.then Step 2:Read the 4alue to ,e pushed. Step !:Stop. /op: Step 1:?he#k i sta#k is empty;then Step 2:*eleted the top 4alue. Step !:*isplay the popped element. Step $:Stop.

26

*isplay: Step 1:?he#k i sta#k is empty. Step 2:*isplay the sta#k. Step !:Stop.

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1pro#ess.h2 0in#lude1stdli,.h2 0in#lude1allo#.h2 typede stru#t sta#k 3 int data5 stru#t sta#k Qnext5 6 node5 4oid main9: 3 node Qtop5 int data;item;#hoi#e5 #har ans;#h5 4oid push9int;node QQ:5 4oid display9node QQ:5 int pop9node QQ:5 int sempty9node Q:5 #lrs#r9:5 topDNJ>>5 print 9<=n=t=t sta#k Jsing >inked list<:5 do 3 print 9<=n=n the main menu<:5 print 9<=n 1.push=n 2.pop=n !.*isplay =n $.exit=n<:5 print 9<=nEnter your #hoi#e<:5 s#an 9<@d<;A#hoi#e:5 swit#h9#hoi#e: 3 #ase 1:print 9<=nEnter The *ata<:5 s#an 9<@d<;Adata:5

27

push9data;Atop:5 ,reak5 #ase 2: i 9sempty9top:: print 9<=n Sta#k Jnder (lowG<:5 else 3 itemDpop9Atop:5 print 9<=n The /oped Node &s@d<;item:5 6 ,reak5 #ase !: display9Atop:5 ,reak5 #ase $: print 9<=n *o J Want To TuitC9NKN:<:5 #hDget#h9:5 i 9#hDDHyH: exit9.:5 else ,reak5 6 print 9<=n *o J Want To ?ontinueC<:5 ansDget#h9:5 #lrs#r9:5 6 while9ansDDHNHVVansDDHyH:5 get#h9:5 6 4oid push9int &tem;node QQtop: 3 node Qnew5 node QgetInode9int:5 newDgetInode9&tem:5 newF2nextDQtop5 QtopDnew5 6 node QgetInode9int item: 3 node Qtemp5 tempD9node Q: mallo#9sizeo 9node::5 i 9tempDDNJ>>:

28

print 9<=n Memory ?annot Xe Allo#ated=n<:5 tempF2dataDitem5 tempF2nextDNJ>>5 return9temp:5 6 int sempty9nodeQtemp: 3 i 9tempDDNJ>>: return 15 else return .5 6 int pop9node QQtop: 3 int item5 node Qtemp5 itemD9Qtop:F2data5 tempDQtop5 QtopD9Qtop:F2next5 ree9temp:5 return9item:5 6 4oid display9node QQhead: 3 node Qtemp5 tempDQhead5 i 9sempty9temp:: print 9<=nThe Sta#k &s Empty<:5 else 3 while9tempGDNJ>>: 3 print 9<@d=n<;tempF2data:5 tempDtempF2next5 6 6 get#h9:5 6

29

Sample &nputKLutput:
Main menu 1./ush 2.pop !.*isplay $.Exit Enter your #hoi#e:1 Enter The *ata:11 Enter your #hoi#e:1 Enter The *ata:!$ *o you Want To ?ontinueC Enter your #hoi#e:! !$ 11 *o you Want To ?ontinueC Enter your #hoi#e:2 The /oped Node &s:!$ *o you Want To ?ontinueC Enter your #hoi#e:$

Result:
Thus the program to implement a sta#k using array has ,een exe#uted su##ess ully and output is 4eri ied.

30

Implementation of Queue Using Array


Exp No: ' Aim:
To write a program to implement a Tueue using array. Algorithm: Step 1:Start. Step 2:Read the re"uired #hoi#e using swit#h #ase. Step !:?all the re"uired un#tions. Step $:Stop. En"ueue: Step 1:?he#k i "ueue is ull;then Step 2:*isplay "ueue o4er lows.else; Step !:?he#k i "ueue is empty;then Step $:Read the 4alue ,y in#rementing rear 4alue. *e"ueue: Step 1:?he#k i "ueue is empty;then Step 2:*isplay "ueue under lows.else Step !:?he#k i ront e"uals rear. Step $:*isplay the deleted element. *isplay: Step 1:?he#k whether "ueue is empty. Step 2:*isplay the "ueue. Step !:Stop.

31

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0de ine size % stru#t "ueue 3 int "ue7size85 int ront;rear5 6 "5 " ull9: 3 i 9".rear2DsizeF1: return 15 else return .5 6 int insert9int item: 3 i 9". rontDDF1: ". rontEE5 "."ue7EE".rear8Ditem5 return ".rear5 6 int "empty9: 3 i 99". rontDDF1:VV9". ront2".rear:: return 15 else return .5 6 int dele9: 3 int item5 itemD"."ue7". ront85 ". rontEE5 print 9<=n The *eleted &tem &s @d<;item:5 return ". ront5 6 4oid display9:

32

3 int i5 or9iD". ront5i1D".rear5iEE: print 9<@d<;"."ue7i8:5 6 4oid main9: 3 int #h;item5 #har a5 #lrs#r9:5 ". rontDF15 ".rearDF15 do 3 print 9<=n Tueue Jsing Array<:5 print 9<=nMain Menu=n<:5 print 9<=n 1.&nsert =n 2.*elete =n !.*isplay<:5 print 9<=n Enter Jr ?hoi#e:=t<:5 s#an 9<@d<;A#h:5 swit#h9#h: 3 #ase 1: i 9" ull9:: print 9<=n Tueue &s (ull =n<:5 else 3 print 9<=nEnter The Num,er To Xe &nserted:=t<:5 s#an 9<@d<;Aitem:5 insert9item:5 6 ,reak5 #ase 2: i 9"empty9:: print 9<=Tueue &s Jnder(low=n<:5 else dele9:5 ,reak5 #ase !: i 9"empty9:: print 9<=nTueue &s Empty =n<:5 else display9:5 ,reak5

33

de ault:print 9<=nWrong ?hoi#eG<:5 ,reak5 6 print 9<=n *o J want To ?ontinue9NKN:C<:5 aDget#h9:5 6 while9aDDHNHVVaDDHyH:5 6

Sample &nputKLutput
Main Menu 1.En"ueue 2.*e"ueue !.*isplay Enter Jr ?hoi#e: 1 Enter The Num,er To Xe En"ueue: *o J want To ?ontinue9NKN:C Enter Jr ?hoi#e:1 Enter The Num,er To Xe En"ueue:!$ Enter Jr ?hoi#e:2 Enter The Num,er To Xe *e"ueue:11 Enter Jr ?hoi#e:! !$

11

Result:
Thus the program to implement a Tueue using array has ,een exe#uted su##ess ully and output is 4eri ied.

34

Implementation of Queue Using Pointer


Exp No: ) Aim:
To write a program or implement a Tueue using pointer. Algorithm: Step 1:Start. Step 2:Read the re"uired #hoi#e. Step !:Stop En"ueue: Step 1:?he#k i "ueue is ull;then Step 2:*isplay "ueue o4er low.else; Step !:Read the 4alue ,y in#rementing the rear 4alue. *e"ueue: Step 1:?he#k i "ueue is empty;then Step 2:*isplay "ueue under lows;else Step !:*isplay the deleted element in the ront position. *isplay: Step 1:?he#k i "ueue is empty;then Step 2:*isplay "ueue under lows;else Step !:*isplay the "ueue. Step $:Stop.

35

/rogram:
0in#lude1stdio.h2 0in#lude1stdli,.h2 0in#lude1#onio.h2 typede stru#t node 3 int data5 stru#t node Qnext5 6T5 T Q ront;Qrear5 4oid main94oid: 3 #har ans5 int #hoi#e5 4oid En"ueue9:5 T Q*e"ueue9:5 4oid display9T Q:5 rontDNJ>>5 rearDNJ>>5 do 3 print 9<=n=t Tueue =n<:5 print 9<=n Main Menu<:5 print 9<=n 1.En"ueue=n 2.*e"ueue=n !.*isplay<:5 print 9<=n Enter ur #hoi#e<:5 s#an 9<@d<;A#hoi#e:5 swit#h9#hoi#e: 3 #ase 1: En"uue9:5 ,reak5 #ase 2: rontD*e"ueue9:5 #ase !: display9 ront:5 ,reak5 de ault: print 9<=n wrong #hoi#e=n<:5 ,reak5 6 print 9<=n *o u want to #ontinue9yKn:C=n<:5 lushall9:5

36

ansDget#h9:5 6 while9ansDDHyHVVansDDHNH:5 get#h9:5 #lrs#r9:5 6 T QgetInode9T Qtemp: 3 tempD9TQ:mallo#9sizeo 9T::5 tempF2nextDNJ>>5 return temp5 6 4oid insert9: 3 #har #h5 T Qtemp5 #lrs#r9:5 tempDgetInode9temp:5 print 9<=n &nsert the element=n<:5 s#an 9<@d<;AtempF2data:5 i 9 rontDDNJ>>: 3 rontDtemp5 rearDtemp5 6 else 3 rearF2nextDtemp5 rearDrearF2next5 6 6 int Tempty9T Q ront: 3 i 9 rontDDNJ>>: return 15 else return .5 6 T Qdelet9: 3 T Qtemp5 tempD ront5 i 9Tempty9 ront::

37

3 print 9<=n The "ue"ue is Empty=n<:5 print 9<=n ?an not delete the element<:5 6 else 3 print 9<n=t The deleted element &s @d<;tempF2data:5 rontD rontF2next5 tempF2nextDNJ>>5 ree9temp:5 6 return ront5 6 4oid display9T Q ront: 3 i 9Tempty9 ront:: print 9<=n The "ueue is Empty=n<:5 else 3 print 9<=n=t The *isplay o the "ueue is=n<:5 or95 rontGDrearF2next5 rontD rontF2next: print 9<=t@d<; rontF2data:5 6 get#h9:5 6

38

Sample &nputKLutput:
Main Menu 1.En"ueue 2.*e"ueue !.*isplay Enter Jr ?hoi#e: 1 Enter The Num,er To Xe En"ueue: *o J want To ?ontinue9NKN:C Enter Jr ?hoi#e:1 Enter The Num,er To Xe En"ueue:!$ Enter Jr ?hoi#e:2 Enter The Num,er To Xe *e"ueue:11 Enter Jr ?hoi#e:! !$

11

Result:
Thus the program or implement a Tueue using pointer has ,een exe#uted su##ess ully and output is 4eri ied.

39

Welformed Balanced Paranthesis Using Array


Exp No: + Aim:
To write a program or wel ormed ,alan#ed paranthesis using array. Algorithm: Step1:?reate an Empty Sta#k. Step2:Read the ?har#ter until the ile. Step!:& the ?hara#ter is opening sym,olthen push it into the sta#k. Step$:& the #hara#ter is an opening sym,ol and & the sta#k is empty report an error else pop it rom the sta#k. Step%:& the sym,ol is not the #orresponding opening Sym,ol then report an error. Step':At the end o ile; i the sta#k is not empty;report an error.

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0de ine size % stru#t sta#k 3 #har s7size85 int top5 6st5 4oid push9#har item: 3 st.topEE5

40

st.s7st.top8Ditem5 6 int stempty9: 3 i 9st.topDDF1: return 15 else return .5 6 #har pop9: 3 #har item5 itemDst.s7st.top85 st.topFF5 return9item:5 6 4oid main94oid: 3 #har item5 #har ans;,ra#ket71.85 int i5 st.topDF15 #lrs#r9:5 print 9<=n=t=t Enter the Expression and put Y at the end=n<:5 s#an 9<@s<;A,ra#ket:5 iD.5 i 9,ra#ket7i8DDH:H: print 9<=n The expression is in4alid<:5 else 3 do 3 while9,ra#ket7i8DDH9H: 3 push9,ra#ket7i8:5 iEE5 6 while9,ra#ket7i8DDH:H: 3 itemDpop9:5 iEE5 6 6

41

while9,ra#ket7i8GDHYH:5 i 9Gstempty9:: print 9<=n The expression is in4alid<:5 else print 9<=n The expression has well ormed paerenthis<:5 6 get#h9:5 6

Sample &nputKLutput:
Enter the Expression and put Yat the end 99::Y The exp has well (ormed parenthesis Enter the Expression and put Yat the end 99:Y The expression is in4alid

Result:
Thus the program or wel ormed ,alan#ed paranthesis using array has ,een exe#uted su##ess ully and output is 4eri ied.

42

Welformed Balanced Paranthesis Using pointer


Exp No: Aim:
To write a program or wel ormed ,alan#ed paranthesis using pointer. Algorithm: Step1:?reate an Empty Sta#k. Step2:Read the ?har#ter until the ile. Step!:& the ?hara#ter is opening sym,olthen push it into the sta#k. Step$:& the #hara#ter is an opening sym,ol and & the sta#k is empty report an error else pop it rom the sta#k. Step%:& the sym,ol is not the #orresponding opening Sym,ol then report an error. Step':At the end o ile; i the sta#k is not empty;report an error.

43

/rogram:
0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1pro#ess.h2 0in#lude1stdli,.h2 0in#lude1allo#.h2 0in#lude1sta#ks.h2 4oid main94oid: 3 #har ans;,ra#ket71.85 #har item5 node Qtop5 int #hoi#e5 int i5 4oid push9#har;nodeQQ:5 4oid *isplay9node QQ:5 #har pop9nodeQQ:5 int Sempty9node Q:5 topDNJ>>5 #lrs#r9:5 print 9<=n=t=t Enter the Expression and put Yat the end<:5 s#an 9<@s<;,ra#ket:5 iD.5 i 9,ra#ket7i8DDH:H: print 9<=n The expression is in4alid<:5 else 3 do 3 i 9,ra#ket7i8DDH9H: 3 push9,ra#ket7i8;Atop:5 6 else i 9,ra#ket7i8DDH:H: 3 i 9Sempty9top:: 3 print 9<=n The Expression is in4alid <:5 get#h9:5 exit9.:5 6 itemDpop9Atop:5

44

6 iEE5 6 while9,ra#ket7i8GDHYH:5 i 9GSempty9top:: print 9<=n The expression is in4alid<:5 else print 9<=n The exp has well (ormed parenthesis<:5 6 get#h9:5 6 4oid *isplay9node QQhead: 3 node Qtemp5 tempDQhead5 i 9Sempty9temp:: print 9<=n The sta#k is EmptyG<:5 else 3 while9tempGDNJ>>: 3 print 9<@#=n<;tempF2data:5 tempDtempF2next5 6 6 get#h9:5 6 KKSta#k.h Beader (ile 0in#lude1stdio.h2 0in#lude1#onio.h2 typede stru#t sta#k 3 #har data5 stru#t sta#k Qnext5 6 node5 4oid push9#har item;node QQtop: 3 node QNew5 node QgetInode9#har:5 NewDgetInode9item:5

45

NewF2nextDQtop5 QtopDNew5 6 node QgetInode9#har item: 3 node Qtemp5 tempD9node Q:mallo#9sizeo 9node::5 i 9tempDDNJ>>: print 9<=n Memory #an not ,e allo#ated=n<:5 tempF2dataDitem5 tempF2nextDNJ>>5 return9temp:5 6 int Sempty9node Qtemp: 3 i 9tempDDNJ>>: return 15 else return .5 6 #har pop9node QQtop: 3 #har item5 node Qtemp5 itemD9Qtop:F2data5 tempDQtop5 QtopD9Qtop:F2next5 ree9temp:5 return9item:5 6

Sample &nputKLutput:
Enter the Expression and put Yat the end 99::Y The exp has well (ormed parenthesis Enter the Expression and put Yat the end 99:Y The expression is in4alid

Result:
Thus the program or wel ormed ,alan#ed parenthesis using pointer has ,een exe#uted su##ess ully and output is 4eri ied.

46

E aluating Postfi! E!pression Using Array


Exp No: 1. Aim:
To write a program or e4aluating post ix using array. Algorithm: Step 1:Start. Step 2:Read the post ix expression rom le t to right. Step !:& the input sym,ol is an operand then; Step $:/ush it on to the sta#k. Step %:& the operator is read /L/ two operands and per orm arithmeti# operations Step ':& operator is E then resultDoperand1Eoperand2 F then resultDoperand1Foperand2 Q then resultDoperand1Qoperand2 K then resultDoperand1Koperand2 Step ):/ush the result on to the sta#k. Step +:Repeat steps 1F$ till the post ix expression is not o4er. Step -:Stop.

47

/rogram: 0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0in#lude1string.h2 0in#lude1math.h2 0in#lude<arrayIst.h< 0de ine size +. 4oid main9: 3 #har exp7size85 int len5 dou,le result5 dou,le post9:5 #lrs#r9:5 print 9<enter the post ix expression=n<:5 s#an 9<@s<;exp:5 lenDstrlen9exp:5 exp7len8DHYH5 resultDpost9exp:5 print 9<The 4alue o the exp is@ =n<;result:5 get#h9:5 exit9.:5 6 dou,le post9#har exp78: 3 #har #h;Qtype5 dou,le result;4al;op1;op25 4oid push9dou,le:5 dou,le pop9:5 int i5 st.topD.5 iD.5 #hDexp7i85 while9#hGDHYH: 3 i 9#h2DH.HAA#h1DH-H: typeD<operand<5 else i 9#hDDHEHVV#hDDHFHVV#hDDHQHVV#hDDHKHVV#hDDHZH: typeD<operator<5 i 9str#mp9type;<operand<:DD.:KKi the #har is opreand

48

3 4alD #h F $+5 push94al:5 6 else i 9str#mp9type;<operator<:DD.:KKi it is operator 3 op2Dpop9:5 op1Dpop9:5 swit#h9#h: 3 #ase HEH: resultDop1Eop25 ,reak5 #aseHFH: resultDop1Fop25 ,reak5 #aseHQH: resultDop1Qop25 ,reak5 #aseHKH: resultDop1Kop25 ,reak5 #aseHZH:resultDpow9op1;op2:5 ,reak5 6 push9result:5 push9result:5 6 iEE5 #hDexp7i85 6 resultDpop9:5 return9result:5 6

49

KKArrayIst.h Beader (ile 0in#lude1stdio.h2 0in#lude1#onio.h2 0de ine MAU 1. stru#t sta#k 3 dou,le s7MAU85 int top5 6 st5 4oid push9dou,le 4al: 3 i 9st.topE12DMAU: print 9<=n sta#k is (ull=n<:5 st.topEE5 st.s7st.top8D4al5 6 dou,le pop9: 3 dou,le 4al5 i 9st.topDDF1: print 9<=n sat#k is empty<:5 4alDst.s7st.top85 st.topFF5 return94al:5 6

Sample &nputKLutput
Enter the post ix Expression 12E!$QE The 4alue o the exp is1%...

Result:
Thus the program or e4aluating post ix using array has ,een exe#uted su##ess ully and output is 4eri ied.

50

E aluating Postfi! E!pression Using Linked List


Exp No: 11 Aim:
To write a program or e4aluating post ix using >inked list. Algorithm: Step 1:Start. Step 2:Read the post ix expression rom le t to right. Step !:& the input sym,ol is an operand then; Step $:/ush it on to the sta#k. Step %:& the operator is read /L/ two operands and per orm arithmeti# operations Step ':& operator is E then resultDoperand1Eoperand2 F then resultDoperand1Foperand2 Q then resultDoperand1Qoperand2 K then resultDoperand1Koperand2 Step ):/ush the result on to the sta#k. Step +:Repeat steps 1F$ till the post ix expression is not o4er. Step -:Stop.

51

/rogram: 0in#lude1stdio.h2 0in#lude1#onio.h2 0in#lude1stdli,.h2 0in#lude1string.h2 0in#lude1math.h2 0in#lude<STA?[S.B< 0de ine size +. 4oid main9: 3 #har exp7size85 int len5 dou,le result5 dou,le post9:5 #lrs#r9:5 print 9<=nEnter the post ix Expression<:5 s#an 9<@s<;exp:5 lenDstrlen9exp:5 exp7len8DHYH5 resultDpost9exp:5 print 9<The 4alue o the exp is@ <;result:5 get#h9:5 exit9.:5 6 dou,le post9#har exp78: 3 #har #h;Qtype5 dou,le result;4al;op1;op25 int i5 node Qtop5 4oid push9#har;node QQ:5 #har pop9node QQ:5 topDNJ>>5 iD.5 #hDexp7i85 while9#hGDHYH: 3 i 9#h2DH.HAA#h1DH-H: typeD<operand<5 else i 9#hDDHEHVV#hDDHFHVV#hDDHQHVV#hDDHKHVV#hDDHZH: typeD<operator<5

52

i 9str#mp9type;<operand<:DD.: 3 4alD#hF$+5 push94al;Atop:5 6 else i 9str#mp9type;<operator<:DD.: 3 op1Dpop9Atop:5 op2Dpop9Atop:5 swit#h9#h: 3 #ase HEH: resultDop1Eop25 ,reak5 #aseHFH: resultDop1Fop25 ,reak5 #aseHQH: resultDop1Qop25 ,reak5 #aseHKH: resultDop1Kop25 ,reak5 #aseHZH: resultDpow9op1;op2:5 ,reak5 6 push9result;Atop:5 6 iEE5 #hDexp7i85 6 resultDpop9Atop:5 return9result:5 6

53

Sample &nputKLutput Enter the post ix Expression 12E!$QE The 4alue o the exp is1%.......

Result:
Thus the program or e4aluating post ix using >inked list has ,een exe#uted su##ess ully and output is 4eri ied.

54

"EAP S#$%
Exp No: 12 Aim:
To write a program to sort the elements using Beap sort. Algorithm: Step 1:start Step 2:read the elements to ,e sorted. Step !:i the elements are to sorted in an as#ending order; do the ollowing to sort;until there is no other element. Step !.1:apply deletemin9: routine to get the smallest element rom the array. Step !.2:store the elements returned ,y deletemin in the last position in the array. Step !.!:de#rement the num,er o elements ,y one. Step $:stop the pro#ess.

55

/rogram: 0in#lude1stdio.h2 0in#lude1#onio.h2 4oid heap9int a78;int n:5 4oid #reateIheap9int a78;int n:5 4oid main9: 3 int a7%.8;i;n5 #lrs#r9:5 print 9<Enter the limit:<:5 s#an 9<@d<;An:5 print 9<Enter the elements<:5 or9iD.5i1n5iEE: s#an 9<@d<;Aa7i8:5 heap9a;n:5 print 9<The sorted list is:<:5 or9iD.5i1n5iEE: print 9<=n @d<;a7i8:5 get#h9:5 6 4oid #reateIheap9int a78;int n: 3 int i;\;";key5 or9"D15a1n5"EE: 3 iD"5 keyDa7"85 \D9int:9iK2:5 while99i2.:AA9key2a7\8:: 3 a7i8Da7\85 iD\5 iD\5 \D9int:9iK2:5 i 9\1.: \D.5 6 a7i8Dkey5 get#h9:5 6 6 4oid heap9int a78;int n:

56

3 int i;\;";key;temp5 #reateIheap9a;n:5 or9"DnF15"2D15"FF: 3 tempDa7.85 a7.8Da7"85 a7"8Dtemp5 iD.5 keyDa7.85 \D15 i 99\E1:1": i 9a7\E182a7\8: \D\E15 while99\1D9"F1::AA9a7\82key:: 3 a7i8Da7\85 iD\5 \D2Qi5 i 99\E1:1": i 9a7\E181": \D\E15 else i 9\2nF1: \DnF15 a7i8Dkey5 6 6 6

57 Sample &nputKLutput Enter the limit:% Enter the elements11 !$ -$% 22 The sorted list is: 22 !$ $% -11

Result: Thus the Si4en Elements is sorted using Beap sort and output is ]eri ied.

58

QUIC& S#$%
Exp No: 1! Aim:
To write a program to sort the elements using Tui#k sort.

Algorithm: Step 1:start. Step 2:read the limit and elements. Step !:#all the un#tion. Step $:display the sorted list. Step %:stop. Tui#k sort: Step 1:#ompute the pi4ot element. Step 2:sort the list. Step !:swap i ne#essary. Step $:stop.

59

/rogram: 0in#lude1stdio.h2 0in#lude1stdli,.h2 0in#lude1#onio.h2 0de ine MAU 1. 4oid main9: 3 int n;i;x7MAU85 4oid "ui#k9int x78;int;int:5 #lrs#r9:5 print 9<=n Enter how many elements do u want sort<:5 s#an 9<@d<;An:5 or9iD.5i1n5iEE: 3 print 9<=n Enter the Elements:<:5 s#an 9<@d<;Ax7i8:5 6 "ui#k9x;.;nF1:5 print 9<=n The sorted Elements are<:5 or9iD.5i1n5iEE: print 9<=n=t=t@d<;x7i8:5 get#h9:5 6 4oid "ui#k9int x7MAU8;int l,;int u,: 3 int newIpi4ote5 int partition9int x7MAU8;int l,;int u,:5 i 9l,1u,: 3 newIpi4oteDpartition9x;l,;u,:5 "ui#k9x;l,;newIpi4oteF1:5 "ui#k9x;newIpi4oteE1;u,:5 6 6 int partition9int x7MAU8;int l,;int u,: 3 int ";pi4ot;i;lower5 4oid inter#hange9int x7MAU8;int a; int ,:5 inter#hange9x;l,;9l,Eu,:K2:5 pi4otDx7l,85 lowerDl,5 or9iDl,E15i1Du,5iEE:

60

3 i 9x7i81pi4ot: 3 lowerDlowerE15 inter#hange9x;lower;i:5 6 6 inter#hange9x;l,;lower:5 "Dlower5 return9":5 6 4oid inter#hange9int x7MAU8;int a;int ,: 3 int temp5 tempDx7a85 x7a8Dx7,85 x7,8Dtemp5 6
Sample &nputKLutput Enter how many elements do u want sort % Enter the Elements:12 Enter the Elements:)) Enter the Elements:+. Enter the Elements:1 Enter the Elements:. The sorted Elements are . 1 12 )) +.

Result: Thus the Si4en elements are sorted using Tui#k sort and output is 4eri ied.

S-ar putea să vă placă și