Sei sulla pagina 1di 8

Raja College of Engineering and Technology, Madurai-20.

Department of Computer Science and Engineering a! Manual


"200#-0$ -%&

Su!.Code Su!.+ame
Syllabus:

' (()2 ' Data Structure, a!

Sem ' *%% Cla,, ' -E - CSE

(. .rray implementation of i,t .!,tract Data Type ".DT& 2. in/ed li,t implementation of i,t .DT 0. .rray implementation, of Stac/ .DT 1. in/ed li,t implementation, of Stac/ .DT ). 2rogram for 3-alanced 2aranthe,i,4 5. 2rogram for 3E6aluating 2o,tfi7 E7pre,,ion,4 8. 9ueue .DT #. Search Tree .DT - -inary Search Tree $. %n,ertion Sort (0. -u!!le Sort ((. :eap Sort (2. 9uic/ Sort

1. Array implementation of List Algorithm (. Create a ,tructure to ,tore ,tudent name,mar/(,mar/2,mar/0,total,a6g and grade. 2. Create an array of a!o6e ,tructure. 0. Do in,ertion, Deletion and Di,play operation. Insertion: (. Chec/ ;hether the li,t i, full or not. 2. %f not full locate the ;rite po,ition to in,ert the ne; element. 0. %& %f that po,ition % i, ,tart of li,t then mo6e all the li,t element, !y one po,ition !ac/, in,ert the element a, fir,t one. ii& %f that po,ition % i, in !et;een the li,t then mo6e the li,t element, !y one po,ition !ac/ from la,t element to %, %n,ert the element a, ith element. iii& <ther;i,e in,ert the element a, la,t element. Deletion: (. Chec/ for a6aila!ility of i,t. 2. %f the element a6aila!le get the po,ition of it. 0. %& %f that po,ition % i, ,tart of the li,t then delete it and ,hift all remaining element, ( po,ition for;ard. ii& %f that po,ition % i, in !et;een the li,t delete the element and ,hift all element from % to end, !y one po,ition for;ard. Display: (. Di,play all the element, from ,tart to end of the li,t.

po,ition

2. Linked List implementation of list Algorithm (. Create a ,tructure to ,tore employee name and employee id. 2. Create Start "header&,current, temp a, pointer to the a!o6e ,tructure. Creation' (. .llocate memory for the ne; record and a,,ign it to temp pointer. 2. %f :eader i, null ,et data and ,et it, ne7t pointer a, +ull. 0. <ther;i,e di,play error. Addition: (. .llocate memory for the ne; record and a,,ign it to temp pointer. 2. Set data. 0. .,,ign temp pointer to current pointer, ne7t. 1. Set temp a, current pointer. Deletion: (. =et the po,ition of the node to !e deleted. 2. Tra6er,e the li,t up to re>uired po,ition u,ing dummy pointer. 0. %f the po,ition i, ,tart then ma/e the header to point out the ,econd Record. 1. if the po,ition i, la,t then ,et the pre6iou, node, ne7t pointer a, +ull. ). %f po,ition i, not ,tart or la,t then ma/e the pre6iou, node, ne7t pointer a, Current node, ne7t pointer. 5. Delete the dummy pointer. Insertion: (. .llocate memory for the ne; record and a,,ign it to temp pointer. 2. %dentify the po,ition to in,ert. 0. Tra6er,e the li,t upto re>uired po,ition, u,ing dummy pointer. 1. Set the ne; node, ne7t a, current node, ne7t. ). .dju,t the current node, ne7t pointer ? header to point out ne; node. 5. Delete the dummy pointer. 3. Stack Implementation. Algorithm (. %nitiali@e a ,tac/. 2. %nitiali@e a pointer to the top of ,tac/. 0. Do 2u,h and 2op operation. !S": (. =et the data to !e pu,hed. 2. Create a ne; element and ,et the data. 0. Set current top record a, ne;ly created element, ,ucce,,or. 1. Apdate pointer to top of ,tac/. # : (. =et pointer to the top of ,tac/. 2. Retrie6e data from top. 0. Re,et the top pointer to it, ,ucce,,or

$. Stack Implementation. Algorithm (. %nitiali@e a ,tac/. 2. %nitiali@e a pointer to the top of ,tac/. 0. Do 2u,h and 2op operation. !S": (. =et the data to !e pu,hed. 2. Create a ne; element and ,et the data. 0. Set current top record a, ne;ly created element, ,ucce,,or. 1. Apdate pointer to top of ,tac/. # : (. =et pointer to the top of ,tac/. 2. Retrie6e data from top. 0. Re,et the top pointer to it, ,ucce,,or %. rogram for &'alanced aranthesis( Algorithm (. Scan the e7pre,,ion into a ,tring B find it, ength. 2. Read Each Character B pu,h it on to a ,tac/ if it i, a 3 " 3. 0. %f it i, 3 & 3 ,earch for it, corre,ponding left !rac/et in thi, ,earch if Stac/ i, empty then e7pre,,ion i, in6alid. .t the end of loop , if any left !rac/et, are left o6er in the ,tac/ then al,o in6alid other;i,e 6alid. ). rogram for &*+aluating ostfi, *,pressions( Algorithm (. %f an operand i, found pu,h it into the ,tac/. 2. %f operator, i, found then pop the top 2 element, and perform the !inary operation corre,ponding to the operator. 0. 2u,h the re,ult into the ,tac/, Chich mu,t replace the 0 entitie, in6ol6ed in the operation. 1. Repeat the ,tep, (,2,0 until the end of e7pre,,ion. ). The top of the ,tac/ indicate, the re,ult. -. .!*!* AD/ Algorithm (. %nitiali@e front a, @ero and rear a, D(. *0.!*!*: (. =et the element to !e in,erted. 2. %ncrement rear !y (. 0. %n,ert the data at the po,ition of rear. D*.!*!*: (. Di,play the >ueue i, empty if rear E front. 2. %f >ueue e7i,t, ,ho; and remo6e the element a6aila!le at po,ition front.

0. %ncrement front !y (. 1. 'inary Search /ree. Algorithm (. Create a ,tructure ;ith left, right pointer B /ey element. 2. Searching' Chec/ the a6aila!ility of Tree. =et the Fey *alue of the root node B compare ;ith the target 6alue. %f target 6alue e7i,t di,play it. %f target 6alue le,, than the target 6alue do the ,earch on left ,u! tree. %f target 6alue greater than the target 6alue do the ,earch on right ,u! tree. Insertion : (. %f no tree e7i,t declare it a, root node. 2. if /ey 6alue of ne; node le,, than ne; node, /ey 6alue then repeat in,ertion on left ,u! tree. 0. if /ey 6alue of ne; node greater than ne; node, /ey 6alue then repeat in,ertion on right ,u! tree. Deletion: (. Chen the node to !e deleted i, a leaf node replace the lin/ to the node !y +A . 2. Chen the node to !e deleted ha, only one ,u! tree .dju,t the lin/ from the parent node to it, ,u! tree. 0. Chen the node to !e deleted ha, !oth ,u! tree, .ttach the left ,u! Tree to an appropriate node of the right ,u! tree. .ttach the right ,u! tree of the node to it, parent. 2. I0S*3/I#0 S#3/: %t i, called in,ertion ,ort, !ecau,e on the ith pa,,, ;e in,ert the ith element .G%H into it, rightful place among .G(HI..Gi-(H ;hich i, pre6iou,ly ,orted. Algorithm .G0H J - K for "iJ2K i EJnK iLL& M j J iK ;hile ".GjH E .Gj-(H& M ,;ap ".GjH, .Gj-(H&K j J j-(K N N eg& gi6en that (2, ), #, (, (), (0 ."(& I.. ."n& initiali@e ."0& J - - - - - (2 ) ) ( ) (2 # ) # # (2 # ( ( ( (2 () () () () (0 (0 (0 (0 %nitial iJ2 iJ0 iJ1 Si7th pa,, get, the ,orted li,t. - ( ) # (2 () (0 iJ) - ( ) # (2 (0 () iJ5

14. 'ubble Sort :ere the record, ;ith the lo; /ey 6alue, are OlightP and !u!!le up to the top. Ce do repeated pa,,e, o6er the array, from !ottom to top. The effect of thi, operation i, that, on the fir,t pa,, the Olighte,tP record ie., the record ;ith the lo;e,t /ey ari,e, to the top. <n 2 nd pa,,, the 2nd lo;e,t /ey ri,e, to the ,econd po,ition. Algorithm for "i J (K i EJn-(K iLL& for "j J nK j EJ%L(K j--& M if aGjH E aGj-(H %nterchange .GjH and .Gj-(H N E7ample' ) 2 5 0 (

Sorting through !u!!le ,ort ha, the pa,,e, gi6en !elo; ) ( ( ( ( 2 ) 2 2 2 5 2 ) 0 0 0 5 0 ) ) ( 0 5 5 5 %nitial iJ( iJ2 iJ0 %J1 11. "eap sort Algorithm The other name for priority >ueue i, heap. i/e !inary ,earch tree, heap, ha6e t;o propertie,, namely ,tructure property and heap order property. Structure 2roperty . heap i, a !inary tree ie., completely filled ;ith the po,,i!le e7ception of the !ottom le6el, ;hich i, filled from left to right. Such a tree i, called a complete !inary tree. :eap <rder 2roperty Thi, property allo;, operation to !e performed >uic/ly, in the ,en,e it ma/e, the ,malle,t element ;ill !e in the root. -y applying thi, logic heap order property i, o!tained. %n a heap for e6ery node 7, the /ey in the parent of 7 i, ,maller than the /ey in the 7. -y thi, property the minimum element can !e found al;ay, at the root i, called min heap. Similarly if the ma7 element i, in the root, it i, called a, ma7 heap. 12. .uick sort Algorithm The di6ide and con>uer approach can !e u,ed to arri6e at an efficient ,orting method different from merge ,ort. difference !et;een >uic/ and merge ,ort Q %n merge ,ort, the file aG(InH ;a, di6ided at it, midpoint into t;o ,u!array,, ;hich ;ere independently ,orted and later merged. Q %n >uic/ ,ort the di6i,ion into t;o ,u! array, i, made, ,o that the ,orted ,u!array, do not need to !e merged later. Thi, i, accompli,hed !y rearranging the element, in aG('nH ,uch that aGiH EJaGjH for all i !et;een ( and m, and all j !et;een mL( and n for ,ome m, ( m n. Thu, the element, in aG(..mH B aGmL(.. nH can !e independently ,orted.

+o merge i, needed.

14. .lgorithm :eap,ort "a, n& ?? Rearrange in a,cending order M :eapify "a,n&K for iJn to 2 ,tep D ( do M t J aGiHK aGiH J aG(HK aG(H J tK .dju,t "a, (, i-(&K N N :eapify Tran,form, the array into heap. ie, ma7imum at node. .lgorithm heapify "a,n& M for "i J n?2K i (K i--& .dju,t "a, i, n&K N .dju,t %t ;ill rearrange element, of aGH. The tree rooted at i i, ma7 heap !y comparing ;ith ,u!tree 2i and 2iL(. %f root i, greater, it ;ill !rea/. .lgorithm .dju,t "a, i, n& Mj J 2iK item J aGiHK ;hile "j n& do if "jEn& BB "aGCjH& E aGjL(H then jLLK ??compare left B right child, et j !e larger child. if "item aGjH& then !rea/K aGj?2H J aGjH K j J 2j N a"j?2& J itemK N :eapify ta/e, o"n& operation, and adju,t ta/e, o"logn& 11..lgorithm :eap,ort "a, n& ?? Rearrange in a,cending order M :eapify "a,n&K for iJn to 2 ,tep D ( do M t J aGiHK aGiH J aG(HK aG(H J tK .dju,t "a, (, i-(&K N N :eapify Tran,form, the array into heap. ie, ma7imum at node. .lgorithm heapify "a,n& M for "i J n?2K i (K i--& .dju,t "a, i, n&K N .dju,t %t ;ill rearrange element, of aGH. The tree rooted at i i, ma7 heap !y comparing ;ith ,u!tree 2i and 2iL(. %f root i, greater, it ;ill !rea/. .lgorithm .dju,t "a, i, n& Mj J 2iK item J aGiHK ;hile "j n& do if "jEn& BB "aGCjH& E aGjL(H then jLLK ??compare left B right child, et j !e larger child.

if "item aGjH& then !rea/K aGj?2H J aGjH K j J 2j N a"j?2& J itemK N :eapify ta/e, o"n& operation, and adju,t ta/e, o"logn& 12. Algorithm 5uicksort : 6oid >uic/,ort "int p, int >& M if "pE>& M??if there are more than one element, di6ide p into t;o ,u!pro!lem,?? int j J partition "a, p, >L(&K ?? j i, the po,ition of the partitioning element. ?? ,ol6e the ,u!pro!lem, 9uic/ ,ort "p, j-(&K 9uic/ ,ort "jL(, >&K ??Therei, no need for com!ining ,olution, N N Thi, algorithm ,ort, the element, aGpH,I. aG>H ;hich re,ide, in the glo!al array aG(..nH into a,cending order. aGnL(H i, con,idered to !e defined and mu,t !e greater than or e>ual to all the element, in aG('nH. .lgorithm 2artition ' int partition "Type aGH, int m, int p& M Type 6J aGmHK int iJmK jJpK do M do iLLK ;hile "a"i& E 6&K do j--K ;hile "a"j& R 0&K if "i E j& %nterchange "a, i, j&K N ;hile "i E j& aGmH J aGjH K aGjH J 6K return "j&K N 6oid interchange "Type aG H, int %, int j& ??E7change a"i& ;ith a"j& M Type p J a"i&K a"i& J a"j& K a"j& J pK N

Potrebbero piacerti anche