128
To implement traversal,insertion,deletion in a linear array. ALGORITHM: TRAVERSAL: This algorithm traverses a linear array A with lower bound 0 and upper bound 9. It traverses A searching the desired number NUMB. Step 1: Repeat for I=0 to 9 Apply search operation for A[I]==NUMB. End of loop. Step 2: Exit. INSERTION: In this A is a linear array with 10 elements and NUMB is any number and POS is the potion at which the NUMB is to be inserted. Step 1: Set I=10. [Initialize counter.] Step 2: Repeat steps 3 and 4 while I>=POS. Step 3: Set A[I+1]=A[I]. [Move Jth element downwards.] Step 4: Set I=I-1. [Decrease counter.] Step 5: Set A[POS]=NUMB. [Insert element.] Step 6: Set 10 to 11. [Reset number of elements.] Step 7: Exit. DELETION:

ds full file.doc

Embed Size (px)

Citation preview

To implement traversal,insertion,deletion in a linear array.ALGORITHM:TRAVERSAL:This algorithm traverses a linear array A with lower bound 0 and upper bound 9. t traverses A sear!hing the desired number "#$%.Step &: Repeat 'or (0 to 9Apply sear!h operation 'or A)*(("#$%.End o' loop.Step +: E,it."SERT-":n this A is a linear array with &0 elements and "#$% is any number and .-S is the potion at whi!h the "#$% is to be inserted.Step &: Set (&0.)nitiali/e !ounter.*Step +: Repeat steps 0 and 1 while 2(.-S.Step 0: Set A)3&*(A)*. )$ove 4th element downwards.*Step 1: Set (5&. )6e!rease !ounter.*Step 7: Set A).-S*("#$%. )nsert element.*Step 8: Set &0 to &&.)Reset number o' elements.*Step 9: E,it.6ELET-":n this A is a linear array with &0 elements and "#$% is any number and this deletes the "#$% number 'rom the array A.Step &: Repeat 'or (0 to 9Apply sear!h operation 'or A)*(("#$%.End o' loop.Step +: Repeat 'or (.-S to :Set A)*(A)3&*)$ove th element upwards.*End o' loopStep 0: Set 9 to :.)Reset number o' elements.*Step 1: E,it.FLOW CHART:PROGRAM CO!:;in!lude@int a)+0*AnumbAposA,A'lag(0@!outStep 1: E,it.PROGRAM CO!:;in!lude@int T-S(0AiAa)7*AvalueA!h@!out=a> KR-"T ( 0=b> REAR ( 0Step 8: O)REAR* ( 6ATAStep 9: Repeat steps + to 7 i' we want to insert more elementsStep :: E,iteletin' an element (rom a $ir$&lar ,&e&eStep &: ' =KR-"T is eSual to P &>=a> 6isplay IOueue is emptyH=b> E,itStep +: Else=a> 6ATA ( O)KR-"T*Step 0: ' =REAR is eSual to KR-"T>=a> KR-"T ( P&=b> REAR ( P&Step 1: Else=a> KR-"T ( =KR-"T 3&> Q SREStep 7: Repeat the steps &A + and 0 i' we want to delete more elementsStep 8: E,itPROGRAM CO!:Linear +&e&e:;in!lude?rear(0@ 'ront(0@C else?rear33@C Sueue)rear*(item@ !out?'ront(5&@ rear(5&@C else i'='ront((SRE5&>?'ront(0@C else?'ront33@C!out ?!ase &: ptr(new node@i'=ptr(("#LL>?!out@breaG@de'ault:!out and =TE$. is not eSual to "#LL>Step 1: TE$. ( TE$. ] ^R.oint@ i ( i 3& Step 7: ' =TE$. not eSual to "#LL> and =i eSual to .-S>=a> Freate a "ew "ode=b> "ew"ode] ^ 6ATA ( 6ATA =c> "ew"ode] ^R.oint ( TE$. ] ^R.oint =d> "ew"ode] ^L.oint ( TE$. =e> =TE$. ] ^R.oint> ] ^L.oint ( "ew"ode =f > TE$. ] ^R.oint ( "ew "ode Step 8: Else=a> 6isplay I.osition "-T 'oundHStep 9: E,it!L!TI#G A #O!Suppose START is the address o' the 'irst node in the linGed list. Let .-S is theposition o' the node to be deleted. TE$. is the temporary pointer to hold the address o' thenode. A'ter deletionA 6ATA will !ontain the in'ormation on the deleted node.Step &: nput the .-SStep +: nitiali/e TE$. ( START@ i ( 0Step 0: Repeat the step 1 i' =i less than .-S> and =TE$. is not eSual to "#LL>Step 1: TE$. ( TE$. ] ^R.oint@ i ( i 3& Step 7: ' =TE$. not eSual to "#LL> and =i eSual to .-S>=a> Freate a "ew "ode=b> "ew"ode] ^ 6ATA ( 6ATA =c> "ew"ode] ^R.oint ( TE$. ] ^R.oint =d> "ew"ode] ^L.oint ( TE$. =e> =TE$. ] ^R.oint> ] ^L.oint ( "ew"ode =f > TE$. ] ^R.oint ( "ew "ode Step 8: Else=a> 6isplay I.osition "-T 'oundHStep 9: E,itPROGRAM CO!:;in!lude@void del=int>@void display=>@void main=>?!lrs!r=>@ int !hAnAmAposAi@ start("#LL@ while=&>?!out ( TL"W =SAVE> ( "EMSAVE ( "EMStep 7: )Mant to insert another node*Read =Fhoi!e>Step 8: )Set the L"W 'ield o' Last inserted element*L"W =SAVE> ( TE$.EEA6Step 9: )Kinished*ReturnAl'orit*m (or t*e insertion o( t*e node in t*e $ir$&lar list"SERT = Y TE$.EEA6A WEL>)This Kun!tion inserts an element a'ter the node whi!h have the in'o 'ield eSual to theWEL variable and TE$.EEA6 is the pointer whi!h points the 'irst element o' the listand SAVE is the temp variable 'or the store address o' the 'irst element*Step &: )Allo!ate the $emory 'or the "EM node*"EM "-6E= >Step +: )Set 'ields o' the "EM node*"K- ="EM> ( TL"W ="EM> ( "#LLStep 0: )Save address o' the 'irst node*KRST ( TE$.EEA6Step 1: )nsertion as 'irst node and 'ind last element o' the list*Repeat while L"W =TE$.EEA6>[ ( "#LLStep 7: )nsert the node*L"W =TE$.EEA6> ( "EML"W ="EM> ( KRSTKRST ( "EMReturn =KRST>Step 8: )nsert in the list other than the 'irst node*Repeat while "K- =L"W =TE$.EEA6>> ( WELStep 9: )Set the linG 'or the "EM node*L"W ="EM> ( L"W =TE$.EEA6>L"W =TE$.EEA6> ( "EMStep :: )Kinished*Return =KRST>Al'orit*m (or t*e eletion an element (rom t*e $ir$&lar list6ELETE =YTE$.EEA6A WEL>)This Kun!tion deletes an element 'rom the !ir!ular list*Step &: )Fhe!G 'or the empty list*' TE$.EEA6 ( "#LLThen write =IEmpty ListH>Step +: )List !ontain Single node*i' L"W =TE$.EEA6> ( TE$.EEA6Return "#LLKree =TE$.EEA6>Step 0: )Save the address o' the 'irst node*KRST ( TE$.EEA6Step 1: )6eletion o' the 'irst node*Repeat while L"W =TE$.EEA6>[ ("#LLStep 7: )6elete the node*L"W =TE$.EEA6> ( L"W =KRST>L"W =KRST> ( KRSTReturn =KRST>Step 8: )Kinding desire node*Repeat while "K- =L"W =TE$.EEA6>> ( WELStep 9: )6eletes the node*TE$. ( L"W =TE$.EEA6>L"W =TE$.EEA6> ( L"W =L"W =TE$.EEA6>>Kree =TE$.>Step :: )Kinished*Return =KRST>Al'orit*m (or display t*e element o( t*e $ir$&lar lin% list6S.LAL =YTE$.EEA6>Step &: )Fhe!G 'or the empty list*' TE$.EEA6 ( "#LLThen write =IEmpty listH>ReturnStep +: ).rint the desire node*Repeat while L"W =TE$.EEA6>[ ( TE$.EEA6Mrite ="K- =TE$.EEA6>>Step 0: )Kinished*ReturnPROGRAM CO!:;in!ludenew=stru!t node>@tmp52in'o ( num@i' =last (( "#LL>?last ( tmp@tmp52linG ( last@Celse?tmp52linG ( last52linG@ NYadded at the end o' listYNlast52linG ( tmp@last ( tmp@CCNYEnd o' !reateXlist=>YNNNThis 'un!tion will add new node at the beginningvoid Fir!ularXLinGed::addatbeg=int num>?"-6E tmp@tmp ( ="-6E>new=stru!t node>@tmp52in'o ( num@tmp52linG ( last52linG@last52linG ( tmp@CNYEnd o' addatbeg=>YNNNKun!tion to add new node at any position o' the !ir!ular listvoid Fir!ularXLinGed::adda'ter=int numAint pos>?"-6E tmpAS@int i@S ( last52linG@NN'inding the position to insert a new node'or=i(0@ i < pos5&@ i33>?S ( S52linG@i' =S (( last52linG>?!out?!out@return@CS ( last52linG@i'=S52in'o (( num>?tmp ( S@last52linG ( S52linG@NNdeleting the nodedelete=tmp>@return@Cwhile=S52linG [( last>?i'=S52linG52in'o (( num> NYElement deleted in betweenYN?tmp ( S52linG@S52linG ( tmp52linG@delete=tmp>@!out=a> 6isplay IThe sta!G is emptyHStep +: Else=a> TE$. ( T-.=b> 6isplay IThe popped element T-. ] 6ATAH =c> T-. ( T-. ] "e,t =d> TE$. ] "e,t ( "#LL =e> Kree the TE$. nodeStep 0: E,itPROGRAM CO!:;in!lude@!lass SXLL?private: stru!t node?int data@ node YlinG@C@ nodeY top@ publi!: SXLL=>?top("#LL@C void push=int n>?node Ytmp@ tmp(new node@ i'=tmp(("#LL>?!out?tmp(top@ top(top52linG@ delete tmp@C CC@void main=>?!lrs!r=>@ SXLL s@ int !hAnum@ do ?!out@ !out=a> R--T("EM "-6EStep 1: Else ' =6ATA < R--T b n'o>=a> R--T ( R--T b L!hild=b> VoTo Step 1Step 7: Else ' =6ATA 2 R--T b n'o>=a> R--T ( R--T b R!hild=b> VoTo Step 1Step 8: ' =6ATA < R--T b n'o>=a> R--T b LFhild ( "EM"-6EStep 9: Else ' =6ATA 2 R--T b n'o>=a> R--T b RFhild ( "EM"-6EStep :: Else=a> 6isplay =I6#.LFATE "-6EH>=b> ETTStep 9: "EM "-6E b n'o ( 6ATAStep &0: "EM "-6E b LFhild ( "#LLStep &&: "EM "-6E b RFhild ( "#LLStep &+: ETT"ear$*in':Step &: nput the 6ATA to be sear!hed and assign the address o' the root node to R--T.Step +: ' =6ATA (( R--T b n'o>=a> 6isplay IThe 6ATA e,ist in the treeH=b> VoTo Step 8Step 0: ' =R--T (( "#LL>=a> 6isplay IThe 6ATA does not e,istH=b> VoTo Step 8Step 1: '=6ATA 2 R--Tbn'o>=a> R--T ( R--TbRFhild=b> VoTo Step +Step 7: '=6ATA < R--Tbn'o>=a> R--T ( R--TbL!hild=b> VoTo Step +Step 8: E,iteletion:"-6E is the !urrent position o' the treeA whi!h is in under !onsideration. L-F isthe pla!e where node is to be repla!ed. 6ATA is the in'ormation o' node to be deleted.Step &: Kind the lo!ation "-6E o' the 6ATA to be deleted.Step +: ' ="-6E ( "#LL>=a> 6isplay I6ATA is not in treeH=b> E,itStep 0: '="-6E b L!hild ( "#LL>=a> L-F ( "-6E=b> "-6E ( "-6E b RFhildStep 1: '="-6E b RFhild( ("#LL>=a> L-F ( "-6E=b> "-6E ( "-6E b LFhildStep 7: '=="-6E b L!hild not eSual to "#LL> UU ="-6E b R!hild not eSual to "#LL>>=a> L-F ( "-6E b RFhildStep 8: Mhile=L-F b L!hild not eSual to "#LL>=a> L-F ( L-F b L!hildStep 9: L-F b L!hild ( "-6E b L!hildStep :: L-F b RFhild( "-6E b RFhildStep 9: E,itPROGRAM CO!:;in!lude@void !aseXb="-6EA"-6E>@void !aseX!="-6EA"-6E>@void insert=int>@void del=int>@void inorder="-6E>@C@NNKun!tion to 'ind the item 'orm the treevoid %ST::'ind=int itemA"-6E YparA"-6E Ylo!>?"-6E ptrAptrsave@i'=root(("#LL> NYtree emptyYN?Ylo!("#LL@Ypar("#LL@return@Ci'=item((root52in'o> NYitem is at rootYN?Ylo!(root@Ypar("#LL@return@CNYnitiali/e ptr and ptrsaveYNi'=item?i'=par(("#LL> NNtem to be deleted is root node?root("#LL@C else?i'=lo!((par52l!hild>?par52l!hild("#LL@Celse?par52r!hild("#LL@CCCvoid %ST::!aseXb="-6E parA"-6E lo!>?"-6E !hild@ NNnitiali/e !hild i'=lo!52l!hild(("#LL> NNtem to be deleted is l!hild?!hild(lo!52l!hild@C else NNtem to be deleted is r!hild?!hild(lo!52r!hild@Ci'=par(("#LL > NYtem to be deleted is root nodeYN?root(!hild@Celse?i'= lo!((par52l!hild> NYitem is l!hild o' its parentYN?par52l!hild(!hild@Celse NYitem is r!hild o' its parentYN?par52r!hild(!hild@CCCNYEnd o' !aseXb=>YNvoid %ST::!aseX!="-6E parA"-6E lo!>?"-6E ptrAptrsaveAsu!Aparsu!@NYKind inorder su!!essor and its parentYNptrsave(lo!@ptr(lo!52r!hild@while=ptr52l!hild[("#LL>?ptrsave(ptr@ptr(ptr52l!hild@Csu!(ptr@parsu!(ptrsave@i'=su!52l!hild(("#LL UU su!52r!hild(("#LL>?!aseXa=parsu!Asu!>@Celse?!aseXb=parsu!Asu!>@Ci'=par(("#LL> NYi' item to be deleted is root node YN?root(su!@Celsei'=lo!((par52l!hild>?par52l!hild(su!@Celse?par52r!hild(su!@Csu!52l!hild(lo!52l!hild@su!52r!hild(lo!52r!hild@CNYEnd o' !aseX!=>YNNNThis 'un!tion will insert an element to the treevoid %ST::insert=int item>? "-6E tmpAparentAlo!ation@'ind=itemAUparentAUlo!ation>@i'=lo!ation[("#LL>?!out?root(tmp@Celse?i'=itemYNNNKun!tion to delete a nodevoid %ST::del=int item>?"-6E parentAlo!ation@i'=root(("#LL>?!out?i'=root(("#LL>?!out@CCvoid main=>?!lrs!r=>@int !hoi!eAnum@%ST bo@while=&>?NN$enu options!out?parent(S@ S(S52l@C else?parent(S@ S(S52r@CCC void add=int n>?int 'ound@ lea' YtAYparent@ 'indparent=nA'oundAparent>@ i'='ound((LES>?!out?in=S52l>@ !out?post=S52l>@ post=S52r>@ !out=b> Swap ( A )*A=c> .os ( i P & Step 0: Repeat the step 0 i' =Swap < A).os* and =.os 2( 0>>=a> A ).os3&* ( A ).os*=b> .os ( .os5& Step 1: A ).os 3&* ( Swap Step 7: E,it.PROGRAM CO!:;in!lude@int a)+0*AtAnAtempAD@!out?!out?int iAnADAgapAtempAa)00*@!lrs!r=>@!out 2 0 and length=right> 2 0i( 'irst=le't> e 'irst=right>append 'irst=le't> to resultle't ( rest=le't>elseappend 'irst=right> to resultright ( rest=right>else i( length=le't> 2 0append 'irst=le't> to resultle't ( rest=le't>else i( length=right> 2 0append 'irst=right> to resultright ( rest=right>end 2*ileret&rn resultPROGRAM CO!:;in!lude @int arrays)00*@int temp)00*@void $ergeSort=int arrays)*A int temp)*A int si/e>?mXsort=arraysAtempA0Asi/e5&>@Cvoid mXsort=int arrays)*A int temp)*A int le'tA int right>?int mid@ i'=right2le't>?mid(=right3le't>N+@ mXsort=arraysAtempAle'tAmid>@ mXsort=arraysAtempAmid3&Aright>@ merge=arraysAtempAle'tAmid3&Aright>@CCvoid merge=int arrays)*A int temp)*A int le'tA int midA int right>?int iAle'tXendAnumXeleAtempXpos@ le'tXend(mid5&@ tempXpos(le't@ numXele(right5le't3&@ while==le't?temp)tempXpos*(arrays)le't*@ le't(le't3&@ tempXpos(tempXpos3&@Cwhile=mid?temp)tempXpos*(arrays)mid*@ mid(mid3&@ tempXpos(tempXpos3&@C'or=i(0@i?arrays)right*(temp)right*@ right(right5&@CCvoid main=>?!lrs!r=>@int iAn@!out?path)i*)D*(in'inity@C else?path)i*)D*(adD)i*)D*@CCC 'or=G(0@G?'or=i(0@i?'or=D(0@D?path)i*)D*(minimum=path)i*)D*A=path)i*)G*3path)G*)D*>>@CCC!out KR-$ TEE E"6 -K A L"WE6 LSTStep &: ?nitiali/ationC"ode(start."e,t ?.oints to the 'irst node in the listC.revious(assign address o' start"odeXnumber(0Step +: ?Fhe!G list is empty or notC ' node("#LL-utput I#"6ERKL-MH and e,it.Step 0: ?S!an the list to !ount the number o' node in the listCRepeat while node?!out