Вы находитесь на странице: 1из 5

LINKED LIST PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.

h> struct Node { int data; struct Node *link; }; struct Node *head; //Global Declaration & head has address field alone static int size; oid !nsert"int #$int %os& { struct Node *te'%("struct Node*&'alloc"sizeof"struct Node&&;//Node to be inserted te'%)>data(#; if"%os((*&//!nsertion at be+innin+ { te'%)>link(head; head(te'%; size,,; return; } else if"%os<("size,*&&//!nsertion else-here { int i; struct Node *te'%*(head;//.ointer ariable created for ta ersal u%to n th %osition; ///ead should ne er be 'odified because it is the onl0 'ark -e ha e for our list. for"i(1;i<%os)2;i,,& //3e ha e to connect -ith the n)* th link$ but -e tra erse fro' head { te'%*(te'%*)>link; } te'%)>link(te'%*)>link; te'%*)>link(te'%; size,,; return; }

else { %rintf"4.osition e#ceeds size5n4&; return; } } oid !nsert6t7nd"int a& { struct Node *te'%("struct Node*&'alloc"sizeof"struct Node&&; struct Node *te'%*(head;//)))))))))))))))) * te'%)>data(a;//)))))))))))))))) 2 if"head((N899& { !nsert"a$*&; //te'%)>link(head; //head(te'%; //size,,; :0 %ro+ra' sto%s e#ecution at this %oint$ !f ! use this state'ent. return; } -hile"te'%*)>link;(N899& { te'%*(te'%*)>link; } te'%)>link(te'%*)>link; te'%*)>link(te'%; size,,; } oid Delete"int %os& { struct Node *te'%*(head; if"te'%*((N899& { %rintf"47'%t0 9ist4&; return; } if"%os>size& { %rintf"4!n alid %osition5n4&; return; }

if"%os((*& { head(te'%*)>link; free"te'%*&; size)); return; } int i; for"i(1;i<%os)2;i,,& { te'%*(te'%*)>link; } struct Node *te'%2(te'%*)>link; te'%*)>link(te'%2)>link; free"te'%2&; size)); } oid <e erse=0!teration"& { struct Node *current$*%re $*ne#t;//current> te'% for tra ersal; //%re > te'% for storin+ address that +ets deleted on detach'ent //ne#t> 6ddress of follo-in+ link as this also +ets lost on detach'ent %re (N899; current(head; -hile"current;(N899& { ne#t(current)>link; current)>link(%re ; %re (current; current(ne#t; } head(%re ; } oid Dis%la0"& { struct Node *te'%(head; %rintf"45n4&; -hile"te'%;(N899& {

%rintf"4?d))>4$te'%)>data&; te'%(te'%)>link; } %rintf"4N8995n4&; //%rintf"45n@ize> ?d5n4$size&; } oid Dis%la0<ecursion"struct Node *head& { if"head((N899& { %rintf"4N8995n4&; return; } %rintf"4?d))>4$head)>data&; Dis%la0<ecursion"head)>link&; } oid Dis%la0<ecursion<e "struct Node *head& { if"head((N899& { %rintf"4N8994&; return; } Dis%la0<ecursion<e "head)>link&; %rintf"4<))?d4$head)>data&; } oid <e erse=0<ecursion"struct Node *%& { if"%)>link((N899& { head(%; return; } <e erse=0<ecursion"%)>link&; struct Node *A(%)>link; A)>link(%; %)>link(N899; } int 'ain"& { %rintf"49inked 9ist .ro+ra'5n4&;

head(N899; int #$n$a$i$%os$%; struct Node *t; %rintf"4Nu'ber of ele'ents to be inserted 4&; scanf"4?d4$&n&; for"i(1;i<n;i,,& { %rintf"47le'ent to be inserted 4&; scanf"4?d4$&#&; %rintf"4.osition at -hich ele'ent is to be inserted 4&; scanf"4?d4$ &%os&; !nsert"#$%os&; %rintf"47le'ent to be inserted 4&; scanf"4?d4$&a&; !nsert6t7nd"a&; // ! canBt 'ake use of !nsert and !nsert6t7nd at the sa'e ti'e. :0 %ro+ra' sto%s e#ecution. // 3hen ! e#chan+ed state'ents * and 2 in !nsert6t7nd it -orks fine Dis%la0"&; } %rintf"4.osition at -hich ele'ent is to be deleted 4&; scanf"4?d4$ &%os&; Delete"%os&; Dis%la0"&; <e erse=0!teration"&;//<e ersed 9ist Dis%la0"&; <e erse=0!teration"&;//Cri+inal list //t(head; ))) !t is not necessar0 to use te'%orar0 ariable for %assin+ head to a function$ the alue of head is not altered Dis%la0<ecursion"head&; Dis%la0<ecursion<e "head&; <e erse=0!teration"&;//Dhis -as done to check if ! could use head in Dis%la0<ecursion as such. Dis%la0"&; <e erse=0<ecursion"head&; Dis%la0"&; %rintf"45n@ize> ?d5n4$size&;//! 'ust add a one here -hile usin+ !nsert6t7nd because -hile insertin+ at end$ size,, for e'%t0 list doesnBt -ork; No- this -orks %erfect since ! call !nsert"a$*&$ there size,, -orks return 1; }

Вам также может понравиться