Vous êtes sur la page 1sur 20

Linked Lists

Linked Lists are another useful data structure. It has great advantage over Array data structure. In comparison to array it is more advantageous in the following senseIn Array as a programmer we have to specify the size/capacity of that array. This restriction is dangerous whenever we need to maintain extra record eyond specified size. Also in case of less record then size. In this case a lot of memory cells are wasted. If array is full! it is never possi le to further store extra records in this array.

IN LINKED REQUIRED:

LISTS

FOLLOWING

STEPS

ARE

IMPORTANT TERMS:
(ode1 The components which form the list. Temp1 "ontains the temporary address which is pointer varia le. ,ead1 "ontains the address of first node. "/node1 "ontains the address of current node. (ext1 "ontains the address of next node. If there is only one node in the list then it holds )(*LL+.

"reation of memory #using malloc $%&. "ollection the ase address returned y malloc $% into a temporary pointer varia le. 'toring own data into the information part of the node. (ow store data into the node. 'tore )(*LL+ into the next part of the node. If head contains )(ull+ then ,ead-temp prev info next .lse c/node-0 next-temp And at last store temp into c/node.

2epresentation of a Linked List

Operations on a inked ist:


5. 6. 7. 8. "reation of a linked list 3isplay Traversing Insertion 3eletion 'earching "oncatenation 4erging 'ingly Linked List 3ou ly Linked List "ircular Linked List "ircular 3ou ly Linked List

T!pes o" inked ists:

#$ Sin% ! Linked List:


&'(ode to (reate a Sin% ! Linked List:'&
9include:stdio.h0 9include:alloc.h0 ;oid main$% < "har ch= 'truct node < Int info= 'truct node >next= ?= 'truct node >head! >temp! >c/node= Int data= ,ead-(*LL= do<

printf$@.nter data into list1AnB%= scanf$@CdB!Ddata%= if$head--(*LL% < temp-$struct node>%malloc$sizeof$struct node%%= temp-0info-data= temp-0next-(*LL= head-temp= c/node-temp= ?
else < temp-$struct node >%malloc$sizeof$struct node%%= temp-0info-data= temp-0next-(*LL= c/node-0next-temp= c/node-temp= ? printf$@.nter E to "ontinueFFFAnB%= ?while$ch--+y+GGch--+E+%= printf$@Linked List successfully "reatedAnB%=

&'(ode "or disp a!'&


c/node-head= while$c/nodeH-(*LL% < printf$@CdAnB!c/node-0info%= c/node-c/node-0next= ?

&'(ode "or Tra)ersa '&

int n= c/node-head= printf$@.nter a .lement To searchAn B%= scanf$@CdAn!DnB%= printf$@The List .lements are at Cdth no. AnB!n%= while$c/nodeH-(*LL% < printf$@CdAnB!c/node-0info%= if$c/node-0info--n% Ireak= c/node-c/node-0next= ?

&'(ode "or Insertion o" a data'&


printf$@.nter 3ata To Ie InsertedAtB%= scanf$@CdB!Ddata%=

&'(ode "or Insertion o" +e%innin% O" Link List'&

data

at

t*e

temp-$struct node >%malloc$sizeof$struct node%%= temp-0info-data= temp-0next-head= head-temp= ?

&'(ode "or Insertion o" a data at t*e End O" Link List'&
temp-$struct node >%malloc$sizeof$struct node%%= struct node >cnode-head= while$cnode-0nextH-(*LL% < "node-cnode-0next= ? temp-0info-data= temp-0next-(*LL= cnode-0next-temp= ?

&'(ode "or Insert a data at Spe,ia Position O" Link List'&


printf$@.nter 3ata and The Josition (o. where Eou want to insert the 3ataAnB%= int i!k= struct node >p= for$i-K!temp-head=k:pos=kLL% < temp-temp-0next= if$temp--(*LL% < printf$@node in the list at less than M(.AnB%= return= ? ? p-$struct node >%malloc$sizeof$struct node%%= p-0info-data= p-0next-pos-0next= pos-0next-p= ?

&'(ode "or De ete a data at t*e +e%innin% o" Link List'&


struct node >ptr= ptr-head= head-head-0next= printf$@* have 3eleted Cd ThisAnB!ptr-0info%= free$ptr%= ?

&'(ode "or De ete a data at t*e +e%innin% o" Link List'&


struct node >add!>pnode= add-head= pnode-(*LL= while$add-0nextH-(*LL% < pnode-ptr= add-add-0next= ? pnode-0next-(*LL= free$add%= ?

&'(ode "or De ete a position o" Link List'&

data

"ro-

spe,i"ied

struct node >ptr!>temp= int i!pos= printf$@.nter Josition to 3eleteAnB%= scanf$@CdB!Dpos%= ptr-head= for$i-5=i:-pos=iLL% < temp-ptr= ptf-ptr-0next= ? printf$@The (*4I.2 Nhich * have deleted is CdB!ptf-0info%= temp-0next-ptf-0next= free$ptr%= ?

&'(ode "or Sear,*in% a data in SINGL. Linked List'&


$In particular Ounction ody% void search$% < printf$@Nhich 3ata do * wanna '.A2",1AnB%= scanf$@CdB!Ddata%= struct node >cnode-head= int i-K!flag-K= while$cnodeH-(*LL% < iLL= if$cnode-0info--data% < flag-5= printf$@*2 3ATA OM*(3 M( (M3. (M CdB!i%= I2.AP=

? cnode-cnode-0next= ? if$flag--K% printf$@'M22E 3ATA (MT OM*(3A(B%== ?

&'(ode "or (ON(ATENATION t/o inked ists'&


void concat$struct node >head5!struct node >head6% < struct node >cnode= cnode-head5= while$cnode-0nextH-(*LL% < printf$@CdB!cnode-0info%= cnode-cnode-0next= ? while$cnodeH-(*LL% < printf$@CdB!cnode-0info%= cnode-cnode-0next= ? ?

&'(ode "or SORTING a Sin% ! Linked List'&


void sort$struct node >head% < struct node >temp!>cnode!>curr= int c-K= cnode-head= while$cnodeH-(*LL% cLL= cnode-cnode-0next= ? temp-head= for$i-K=i:c-5=iLL% < curr-temp-0next= for$Q-iL5=Q:c=QLL% < if$temp-0info0curr-0info% < t-temp-0info= temp-0info-curr-0info= curr-0info-t= ? temp-temp-0next= ? curr-curr-0next= ?

<

0$ (IR(ULAR LINKED

&'(reation o" a (ir,1 ar Linked List'&


9include:stdio.h0 9include:alloc.h0 struct node < int num= struct node >next= ?>head!>temp!>rear= void main$% < printf$@.nter 3ata to fill "ircular Linked List1AnB%= scanf$@CdB!Ddata%= if$head--(*LL% < temp-$struct node >%malloc$sizeof$struct node%%=

temp-0num-data= temp-0next-head= head-temp= rear-temp= ? else < temp-$struct node >%malloc$sizeof$struct node%%= temp-0num-data= temp-0next-head= rear-0next-temp= rear-temp= ? ?

&'Insertin% a node at t*e 2e%innin% in a (ir,1 ar Linked List'&


void insert/ eg$% < struct node >temp= temp-$struct node >%malloc$sizeof$struct node%%= printf$@.nter a (um er to I('.2T it at the eginning of ""L1B%= scanf$@CdB!Dtemp-0info%= temp-0next-head= head-temp= rear-0next-head= ?

&'Insertin% node at a %i)en Position in a (ir,1 ar Linked List'&


;oid main$% < int n= printf$@.nter Josition (um er1B%= scanf$@CdB!Dn%= n--= specifiedpos$n%= ? void specifiedpos$int Q% < int c-K!k-Q-6= struct node >num!>prev= printf$@Linked List *pto Cd element-B!Q%= do< printf$@CdB!head-0info%= head-head-0next= if$k--c%prv-head= cLL= ?while$c:Q%=

num-$struct node >%malloc$sizeof$struct node%%= printf$@.nter a .lement1B%= scanf$@CdB!Dnum-0info%= num-0next-head= prv-0next-num= head-temp= ?

&'De etin% t*e #st node "ro- a (ir,1 ar Linked List'&


void del/ eg$% < struct node >cnode= cnode-temp= temp-head-head-0next= free$cnode%= rear-0next-head= c--= ?

&'De etin% t*e Last node "ro- a (ir,1 ar Linked List'&


void del/end$% < int k-K= while$kLL:$c-6%% < head-head-0next= ? free$rear%= rear-head= rear-0next-temp= head-temp= c--= ?

&'De etin% a node "ro- spe,i"ied position in a (LL'&


prinft$@.nter a position no1AnB%= scanf$@CdB!Dn%= del/spe$%= ? void del/spe$int x% < struct node >prv!>temp5= int k-K!p= p-x-6= while$kLL:x% <

head-head-0next= if$k--x% prv-head= if$k--$xL5%%temp5-head= ? c--= prv-0next-head= head-temp= free$temp5%= ?

&'(ode For Disp a!in% A (ir,1 ar Linked List'&


#st Met*od
void show$% < printf$@Linked List .lements are1B%= do< printf$@CdB!head-0info%= head-head-0next= if$rear-0next--head%Ireak= ?while$5%= head-temp= ?

0nd Met*od

void show6$% < node >p-head= while$p-0next-head% < printf$@CdB!p-0info%= p-p-0next= ? ?

3$ DOU+L. LINKED LIST

In dou ly linked list! each structure type node contains 7 parts1 Info! (ext D Jrv. )Jrv+ contains the address of Qust previous node. )(ext+ contains the address of Qust next node and )Info+ contains the information $varia le%.

&'(reation o" Do12 ! Linked List'&

void create$% < do< temp-$struct node >%malloc$sizeof$struct node%%= printf$@An.nter *r 3ataB%= scanf$@CdB!Dtemp-0info%= if$front--(*LL% < temp-0next-(*LL= temp-0prv-(*LL= rear-front-temp= cnode-temp= ? else < temp-0next-(*LL= temp-0prv-cnode= cnode-0next-temp= cnode-cnode-0next= rear-temp= ? printf$@An.nter E to "ontinue1B%= ch-getche$%= ?while$ch--+y+GGch--+E+%= ?

&'Insertion At Front'&
void insert/ eg$int data% < node >ptr= prt-$node >%malloc$sizeof$node%%= ptr-0info-data= ptr-0prv-(*LL= ptr-0next-front= front-0prv-ptr= front-ptr= ?

&'Insertion at t*e End'&


void insert/end$int data% < node >ptr= ptr-$node >%malloc$sizeof$node%%= ptr-0info-data= ptr-0next-(*LL= ptr-0prv-rear= rear-0next-ptr= rear-ptr= ?

&'De etion o" a Node "ro- spe,i"ied position in Do12 ! Linked List'&
4i5 First Met*od void del/spe$% < struct node >case5!>yahoo!>ali!>ptr= int data!c-K= printf$@An.nter Josition of nodeB%= scanf$@CdB!Ddata%= case5-front= if$data--K% < ptr-front= free$ptr%= front-front-0next= front-0prv-(*LL= ? else < while$case5H-(*LL% < cLL= if$c--$data-5%% < ptr-case5= ? if$c--data% < yahoo-case5= free$yahoo%= ? if$c--$dataL5%% ali-case5= case5-case5-0next= ? ptr-0next-ali= ? ? 4ii5 Se,ond Met*od cnode-front= while$cnodeH-(*LL% < if$cnode-0info--data%Ireak= node-node-0next= ? cnode-0next-0prv-cnode-0prv= cnode-0prv-0next-cnode-0next=

free$cnode%=

&'Sear,*in% data in Do12 ! Linked List'&


fnode-front= rnode-rear= flag-K!f-K!l-K!data-5K= while$fnodeH-(*LLGGrnodeH-(*LL% < fLL= lLL= if$fnode-0info--data% < printf$@3ata found At Cd from startingB!f%= flag-5= reak= ? if$rnode-0info--data% < printf$@3ata is found At Cd from lastB!l%= flag-5= reak= ? fnode-fnode-0next= rnode-rnode-0prv= ? if$flag--K% printf$@AnTry Again 3ata (ot Oound1B%= ?

6$ (IR(ULAR DOU+L. LINKED LIST

&'(reation o" (ir,1 ar Do12 ! Linked List'&


#st -et*od

9include:stdio.h0 9include:conio.h0 9include:alloc.h0 struct node < int info= struct node >left!>right= ?>head= head-$struct node >%malloc$sizeof$struct node%%= head-0left-head-0right-head= head-0info-K=

0nd -et*od /it* rear and "ront ,on,ept


front-rear-(*LL= do< temp-$struct node >%malloc$sizeof$struct node%%= scanf$@CdB!Dtemp-0info%= if$front--(*LL% < fornt-temp= temp-0prv-(*LL= temp-0next-front= rear-temp= cnode-temp= ? else < temp-0next-front= temp-0prv-cnode= cnode-0next-temp= rear-temp= cnode-cnode-0next= ? printf$@An Jress E to continueB%= ch-getche$%= ?while$ch--+y+GGch--+E+%=

&'Insertion At 2e%innin%'&
void insert/ eg$% < node >ptr!>temp= int item= prt-$node >Kmalloc$sizeof$node%%= printf$@An.nter the (o1B%= scanf$@CdB!Ditem%= ptr-0info-item= temp-head-0right= head-0right-ptr= ptr-0left-head=

ptr-0right-temp= temp-0left-ptr= ?

&'Insertion At Last'&
void insert/end$% < node >ptr!>temp= int item= ptr-$node >%malloc$sizeof$node%%= scanf$@CdB!Dptr-0info%= temp-head-0left= temp-0right-ptr= ptr-0left-temp= ptr-0right-head= head-0left-ptr= ?

&'De etion "ro- +e%innin%'&


void del/ eg$% < node >temp= if$head-0right--head% < return= ? else < temp-head-0right= printf$@Adeleted element is -Cd AnB!temp-0info%= head-0right-temp-0right= temp-0right-temp-0left-head= free$temp%= ? ?

&'De ete "ro- Last'&

void del/end$% < node >temp= if$head-0right--head%return= else < temp-head-0left= printf$@An3eleted element is Cd AnB!temp-0info%= head-0left-temp-0left= free$temp%= ?

7eader Linked List


The ,eader Linked List is similar to the single linked list ut it contains some extra information. This ,eader list+s 5st node holds that vita information such as the total num er of nodes.

In the figure ,eader node is holding count of total nodes i.e.1 7.

App i,ation o" Linked List


Addition of two @JMLE(M4IAL'B111111111111111111111111111111111111111111111111111111111111 Jolynomials are expressions containing num er of terms with non zero coefficients and exponents. ex1 p-RxSL6xTL5KxLU=

2epresentation of the given eVuation RxSL6xTL5KxLU

(reation o" a POL.NOMIAL


----------------------------------------------------------------void main$% < int c!e= struct poly < int coef= int expo= struct poly >next= ?>temp!>cnode!>head!>ele!>rear= char ch= printf$@.nter co-efficientAnB%= printf$@.nter .xponentAnB%= scanf$@CdCdB!Dc!De%= temp-$struct poly >%malloc$sizeof$struct poly%%= temp-0next-(*LL= if$head--(*LL% <

head-temp= cnode-temp= ? else < +. SIR cnode-0next-temp= cnode-cnode-0next= ? printf$@Jress E to continueeeeeee1AnB%= ch-getche$%= ?while$ch--+y+GGch--+E+%= --------------------------------------------------------------------------------------------------------------------------------rear-temp= for$==% < printf$@.nter co-efficientAnB%= ele-$struct node >%malloc$sizeof$struct node%%= scanf$@CdB!Dcoef%= if$ele-0c--K%Ireak= printf$@.nter .xponent1AnB%= scanf$@CdB!De%= ele-0expo-e= if$ele-0expo:-K% < printf$@I(;ALI3 I(J*TAnB%= Ireak= ? ele-0next-(*LL= rear-0next-ele= rear-ele= ? temp-temp-0next= ? -----------------------------------------------------------------

+. +OOK

&'Disp a!in% a Po !no-ia '&


cnode-head= Jrintf$@AnCdxWCd+!cnode-0coef!cnode-0expo%= cnode-cnode-0next= while$cnodeH-(*LL% < if$cnode-0coer0K% printf$@LB%= else if$cnode-0expo--K% printf$@CdB!cnode-0coef%= printf$@CdxWCdB!cnode-0coef!cnode-0expo%=

cnode-cnode-0next= ?

ADDITION

The address of 5st Jolynomial is stored in s5 and 6nd Jolynomial is stored in s6. t7-polyadd$s5!s7%= show$t7%= struct poly >polyadd$struct poly >s5!struct poly >s6% < struct poly >s7-K!>p7!>tempo= if$s5--KDDs6--K%return s7= while$s5H--KDDs6H--K% < tempo-$struct poly >%malloc$sizeof$struct poly%%= if$s7--K% < s7-tempo= p7-s7= ? else < p7-0next-tempo= p7-p7-0next= ? if$s5-0expo0s6-0expo% < tempo-0coef-s5-0coef= tempo-0expo-s5-0expo= s5-s5-0next= ? else if$s6-0expo0s5-0expo% < tempo-0coef-s6-0coef= tempo-0expo-s6-0expo= s6-s6-0next= ? else if$s5-0expo--s6-0expo% < tempo-0coef-s5-0coefLs6-0coef= tempo-0expo-s5-0expo= s5-s5-0next= s6-s6-0next= ? ? while$s5H-K% < tempo-$struct node >%malloc$sizeof$struct poly%%= tempo-0coef-s5-0coef= tempo-0expo-s5-0expo= if$s7--K%

< s7-tempo= p7-s7= ? else < p7-0next-tempo= p7-p7-0next= ? s5-s5-0next= ? while$s6H-K% < tempo-$struct poly >%malloc$sizeof$struct node%%= tempo-0coef-s6-0coef= tempo-0expo-s6-0expo= if$s7--K% < s7-tempo= p7-s7= ? else < p7-0next-tempo= p7-p7-0next= ? p7-0next-K= return t7= ?

Vous aimerez peut-être aussi