Data Structure Lab File
Data Structure Lab File
S.NO.
1.
Program Name
Program for insert and delete the Element.
Remarks
2.
3.
4.
5.
!.
#.
$.
&&Program for insert and delete the Element. 'include(stdio.h) 'include(conio.h) void main*+ , int n%a-45.%i%s%pos%d%b-45./ clrscr*+/ printf*0Enter the ma1imum number for insert20+/ scanf*03d0%4n+/ printf*0Enter the array number20+/ for*i51/i(5n/i66+ scanf*03d0%4a-i.+/ printf*0Enter the position of element20+/ scanf*03d0%4pos+/ printf*0Enter the number u 7ant to insert20+/ scanf*03d0%4s+/ for*i5n/i)51/i88+ , a-i61.5a-i./ if*i55pos+ , a-pos.5s/ break/ 9 9 printf*0:he list after insert element20+/ for*i51/i(5n61/i66+ printf*0;n3d0%a-i.+/ printf*0Enter the ma1imum number for delete20+/ scanf*03d0%4n+/ printf*0Enter the array element20+/ for*i51/i(5n/i66+ scanf*03d0%4b-i.+/ printf*0Enter the position of the number you 7ant to delete20+/ scanf*03d0%4d+/ for*i5n/i)51/i88+ , if*a-d.55a-i.+ continue/ 9 printf*0Element after <elete20+/ for*i51/i(5n81/i66+ printf*0;n3d0%b-i.+/ getch*+/ 9
=>:P>:2
Enter the ma1imum number for insert2 3 Enter the array number2 1 2 3 Enter the position of element2 2 Enter the number u 7ant to insert2 :he list after insert element2 1 2 3 Enter the ma1imum number for delete2 3 Enter the array element2 1 2 3 Enter the position of the number you 7ant to delete2 2 Element after <elete2 1 2
&& ?@P for insertion sort in array. 'include(stdio.h) 'include(conio.h) void main*+ , int a-45.%n%i%pos%c/ printf*0Enter the number of element of array20+/ scanf*03d0%4n+/ printf*0Enter the array element20+/ for*i51/i(5n/i66+ scanf*03d0%4a-i.+/ for*i52/i(5n/i66+ , c5a-i./ pos5i81/ 7hile*c(a-pos. 44 pos)51+ , a-pos61.5a-pos./ pos5pos81/ 9 a-pos61.5c/ 9 printf*0:he sorted array are20+/ for*i51/i(5n/i66+ printf*0;n3d0%a-i.+/ getch*+/ 9
=>:P>:2
Enter the number of element of array2 $ Enter the array element2 12 2 5 1 # 3 4 :he sorted array are2 1 2 3 4 5 # 11 12
11
&&?@P for Bubble sort in array. 'include(stdio.h) 'include(conio.h) void bubble*int a-.% int n+/ int i%A%n%temp%a-25.%count5B/ void main*+ , printf*0;n;n Enter ho7 many number you 7ant to sort;n;n0+/ scanf*03d0%4n+/ printf*0;n Enter the number;n0+/ for*i5B/i(n/i66+ , scanf*03d0%4a-i.+/ 9 bubble*a%n+/ printf*0;n;nCinal sorted list is;n0+/ for*i5B/i(n/i66+ printf*034d0%a-i.+/ printf*0;nDomple1ity is5 3d0%count+/ getch*+/ 9 void bubble*int a-.%int n+ , int k/ for*i5B/i(n8i/i66+ , printf*0;n;npass8)3d0%i61+/ for*A5i61/A(5n81/A66+ , if*a-i.)a-A.+ , temp5a-i./ a-i.5a-A./ a-A.5temp/ 9 count66/ 9 for*k5B/k(n/k66+ printf*0;t34d0%a-k.+/ &&printf*0;n0+/ 9
}
=>:P>: Enter ho7 many number you 7ant to sort 4 Enter the number 542! pass8)1 pass8)2 2 2 5 4 4 5 ! !
&&?@P for selection sort in array. 'include(stdio.h) 'include(conio.h) void selectionsort*int a-.% int n+/ int i%A%temp%a-25.%n%count/ void main*+ , printf*0;n;nEnter ho7 many number you 7ant to sort;n;n0+/ scanf*03d0%4n+/ printf*0;nEnter the number;n0+/ for*i5B/i(n/i66+ , scanf*03d0%4a-i.+/ 9 selectionsort*a%n+/ printf*0;n;nCinal sorted list is0+/ for*i5B/i(n/i66+ printf*03d0%a-i.+/ printf*0;n;nDomple1ity is 3d0%count+/ getch*+/ 9 void selectionsort*int a-.% int n+ , int temp% small%loc%i%A%k/ for*i51/i(5n81/i66+ , printf*0pass8)3d;t0%i+/ small5a-i81./ loc5i81/ for*A5i/A(5n81/A66+ , if*a-A.(small+ , small5a-A./ loc5A/ 9 count66/ 9 if*locE5*i81++ , temp5a-i81./ a-i81.5a-loc./ a-loc.5temp/ 9
=>:P>:2
2 2 2 2 2
3 3 3 3 3 2
5 5 4 4 4 3
5 5 5 5 5 4 5 5
4 4 5 5
&&?@P to print the element of linked list in reverse order. 'include(stdio.h) 'include(conio.h) typedef struct node , int item/ struct node Fne1t/ 9 node/ node Fhead% Ftail% Ftemp/ void add*+/ void reverse*+/ void display*+/ void main*+ , int n%i/ printf*0;t;tEnter the number of element2;t0+/ scanf*0;t;t3d0%4n+/ head5G>HH/ for*i5B/i(n/i66+ , add*+/ 9printf*0;n;t;t=riginal order of linked list;n0+/ display*+/ reverse*+/ printf*0;n;t;tIeverse of linked list;n0+/ display*+/ 9 void reverse*+ , node Fp%F"%Fr/ if*head55G>HH+ , return/ 9 p5head/"5p8)ne1t/ p8)ne1t5G>HH/ 7hile*"E5G>HH+ , r5"8)ne1t/ "8)ne1t5p/ p5"/"5r/ 9 head5p/ 9 void add*+
, node Ftemp/ temp5*nodeF+malloc*siJeof*node++/ printf*0;n;tEnter the data2;t0+/ scanf*03d0%4temp8)item+/ temp8)ne1t5head/ head5temp/ 9 void display*+ , node Fcur5head/ printf*0;n;tKead8)0+/ 7hile*curE5G>HH+ , printf*0;t3d0%cur8)item+/ cur5cur8)ne1t/ 9 printf*08)null;n;n0+/ 9
=>:P>:2 Enter the number of element2 Enter the data2 2 Enter the data2 4 Enter the data2 Enter the data2 # =riginal order of linked list Kead8) # 4 28)G>HH 4
&&?@P for stack implementation thro7 array. 'include(stdio.h) 'include(conio.h) 'include(ctype.h) 'include(stdlib.h) 'define L@MNOPE 3 int stack-L@MNOPE./ int top5B/ void main*+ , void push*int+/ int pop*+/ int choice51%i%num%1/ &&clrcsr*+/ 7hile*choice551+ , printf*0;n;t;t5555555555L@OG LEG>5555555555;n;t1.P>NK opration on stack;n;t2.P=P operation on stack;n;t3.E1it0+/ printf*0;n;tEnter your choice50+/ scanf*03d0%4choice+/ s7itch*choice+ , case 12 printf*0;nEnter the element50+/ scanf*03d0%4num+/ push*num+/ break/ case 22 i5pop*+/ printf*0;nQalue returned from pop function is53d0%i+/ break/ case 32 e1it*B+/ default2 printf*0;n;tOnvalid choice.0+/ 9 printf*0;nPress 1 to do further operation or 3 to e1it0+/ scanf*03d0%4choice+/ 9 9 void push*int y+ , if*top)L@MNOPE+ , printf*0;nN:@DR C>HH0+/ return/
9 else , top66/ stack-top.5y/ printf*0;n:he top of stack is53d0%top+/ printf*0;n:he value of stack top is53d0%stack-top.+/ 9 9 int pop*+ , int a/ if*top(B+ , printf*0;nN:@DR ELP:S0+/ return B/ 9 else , a5stack-top./ top88/ printf*0;n:he top of stack is53d0%top+/ 9 return*a+/ 9
=>:P>:2
5555555555L@OG LEG>5555555555 1.P>NK opration on stack 2.P=P operation on stack 3.E1it Enter your choice51 Enter the element52 :he top of stack is51 :he value of stack top is52 Press 1 to do further operation or 3 to e1it 1 5555555555L@OG LEG>5555555555 1.P>NK opration on stack 2.P=P operation on stack 3.E1it Enter your choice52 :he top of stack is5B Qalue returned from pop function is52 Press 1 to do further operation or 3 to e1it
&&?@P for circular "ueue implementation. 'include(stdio.h) 'include(conio.h) 'define L@MTNOPE 2B int "ueue-L@MTNOPE.%f581%r581/ main*+ , int ch%1/ printf*0;nU>E>E =PEI@:O=GN0+/ printf*0;n 1. insert operation0+/ printf*0;n 2. delete operation0+/ printf*0;n 3. e1it0+/ 7hile*1+ , printf*0;nEnter your choice20+/ scanf*03d0%4ch+/ s7itch*ch+ , case 12 printf*0;nEnter the number to be inserted20+/ scanf*03d0%41+/ insert*1+/ break/ case 22 15delete*+/ if*1E51+ printf*0;n:he number delete is23d0%1+/ else printf*0;n:he "ueue is empty0+/ break/ case 32 e1it*B+/ default2 printf*0;nEnter correct choice0+/ break/ 9 9 9 insert*int 1+ , if*f55B 44 r55*L@MTNOPE81+ VV *f55r61++ , printf*0;noverflo70+/ return*81+/ 9
if*f5581+ , f5r5B/ 9 else , if*r55L@MTNOPE81+ r5B/ else r5r61/ 9 "ueue-r.51/ return/ 9 int delete*+ , int 1/ if*f5581+ , printf*0;nunderflo70+/ return/ 9 15"ueue-f./ if*f55r+ f5r581/ else , if*f55L@MTNOPE81+ f5B/ else f5f61/ 9 return*1+/ 9
=>:P>:2 U>E>E =PEI@:O=GN 1. insert operation 2. delete operation 3. e1it Enter your choice21 Enter the number to be inserted21B Enter your choice21 Enter the number to be inserted21 Enter your choice22 :he number delete is21B Enter your choice22 :he number delete is21 Enter your choice22 underflo7 :he "ueue is empty Enter your choice23
&& for implement linked list insertion and deletion. 'include(stdio.h) 'include(conio.h) struct linkedTlist , int info/ struct linkedTlist Fne1t/ 9/ void insertTatTbegin*int+/ void insertTatTend*int+/ void insertTatTmiddle*int%int+/ void del*int+/ typedef struct linkedTlist node/ node Fhead5G>HH/ void main*+ , int i%A%n%n1/ clrscr*+/ 7hile*1+ , printf*01. Onsertion;n0+/ printf*02. <eletion;n0+/ printf*03. E1it;n0+/ printf*0;nEnter your choice20+/ scanf*03d0%4i+/ s7itch*i+ , case 12 printf*01. :o insert at beginning;n0+/ printf*02. :o insert at End;n0+/ printf*03. :o insert at Liddle;n0+/ printf*0Enter your choice;n0+/ scanf*03d0%4A+/ if*A(1 VV A)3+ , printf*0Please re8try;n0+/ break/ 9 printf*0Enter the number to be insert;n0+/ scanf*03d0%4n1+/ s7itch*A+ , case 12 insertTatTbegin*n1+/ break/ case 22
insertTatTend*n1+/ break/ case 32 printf*0Enter the number after 7hich the number is to be inserted;n0+/ scanf*03d0%4n+/ insertTatTmiddle*n%n1+/ break/ 9 break/ case 22 printf*0Enter the number to be delete;n0+/ scanf*03d0%4n1+/ del*n1+/ break/ case 32 e1it*B+/ break/ default2 printf*0Please re8try;n0+/ break/ 9 getch*+/ 9 9 void insertTatTbegin*int n1+ , node Fp/ p5*nodeF+malloc*siJeof*node++/ p8)info5n1/ p8)ne1t5head/ head5p/ 9 void insertTatTend*int n1+ , node Fp%F"/ "5head/ p5*nodeF+malloc*siJeof*node++/ p8)info5n1/ p8)ne1t5G>HH/ 7hile*"8)ne1tE5G>HH+ "5"8)ne1t/ "8)ne1t5p/ 9 void insertTatTmiddle*int n%int n1+ , node Fp%F"/
p5head/ "5*nodeF+malloc*siJeof*node++/ "8)info5n1/ 7hile*p8)infoE5n+ p5p8)ne1t/ "8)ne1t5p8)ne1t/ p8)ne1t5"/ 9 void del*int n1+ , node Fp%F"/ "5head/ if*head55G>HH+ return/ if*"8)info55n1+ , head5head8)ne1t/ free*"+/ return/ 9 7hile*"8)infoE5n1+ , p5"/ "5p8)ne1t/ 9 p8)ne1t5"8)ne1t/ free*"+/ 9
=>:P>:2 1. Onsertion 2. <eletion 3. E1it Enter your choice21 1. :o insert at beginning 2. :o insert at End 3. :o insert at Liddle Enter your choice 1 Enter the number to be insert 12 1. Onsertion 2. <eletion 3. E1it Enter your choice22 Enter the number to be delete 12 1. Onsertion 2. <eletion 3. E1it Enter your choice23