Data Structure and Algorithm Lab Manual

download Data Structure and Algorithm Lab Manual

of 34

Transcript of Data Structure and Algorithm Lab Manual

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    1/34

    Exp.No.1

    Implementation of stack using Array

    Aim:

    To write a C program to perform operations in stack using array.

    Algorithm:

    1. Declare the structure of stack with an array of size 20 to hold the stack

    and the initialize top !1

    2. Define a function push" # to add the data to the stack asCheck if the stack is full if so display $tack %ull and return.

    &therwise

    'ncrement the top (alue.$tore the data in the stack)top* and return

    +. Define a function pop" # that deletes the (alue in the top of the stack

    as'f top!1 print $tack ,mpty and return

    &therwise -rint the element at stack)top* and decrement top(alue and return

    . Define a function show that prints the elements in the stack.

    /. Define the main function as

    Display the menu to the user

    1 ! -ush2 ! -op

    + ist the stack

    ,it

    D3T3 $T45CT54,$ 36 1

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    2/34

    7et the choice from the user

    'f choice 1 read the data and add it to the stack

    using push" # function.

    'f the choice 2 delete the data from the stack usingpop " # function.

    'f the choice is + display the data in the stack.

    'f choice eit from the program

    ,lse repeat the a8o(e steps.9. $top

    Output:

    :enu

    1!;-ush2!;-op

    +!;ist stack

    !;,it,nter your choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    3/34

    +!;ist stack

    !;,it

    ,nter your choice!!!top:enu

    1!;-ush

    2!;-op+!;ist stack

    !;,it

    ,nter your choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    4/34

    RESU!:"

    The gi(en program is implemented? eecuted? tested and (erified successfully

    Implementation of #ueue using Array

    Exp.no.1$%&

    Aim:

    To write a C program to perform operations in @ueue using array.Algorithm:

    1. Declare the array to hold the @ueue elements and the (aria8les to hold

    the front and rear inde.

    2. Define functions to insert data into the rear end of @ueue as'f the rearA!$'B,!1 then

    -rint Aueue %ull

    ,lse

    4ear4ear1.$tore the sdate in the rear position

    -rint the @ueue+. Define functions to delete the data from the @ueue'f frontrear

    -rint Aueue ,mpty

    ,lsefrontfront1

    -rint the deleted element and the @ueue

    . Define function to print the elements in the @ueue./. Define main function

    'nitialize front rear !1

    Create a @ueue with elements

    Display the menu to the user1!;'nsertion

    2!;Deletion

    +!;,it7et the users choice

    'f choice 1 call the function to add element

    'f choice 2 call the function to delete the element'f choice + then ,it the program

    D3T3 $T45CT54,$ 36

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    5/34

    4epeat the step / till choice +.

    9. $top

    Output:

    o. of elements initially in the @ueue

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    6/34

    RESU!:"

    The gi(en program is implemented? eecuted? tested and (erified successfully

    SIN'( IN)E* IS!

    Ex. No : +

    AI,:"

    To write a FCG program to create a singly linked list implementation.

    A'ORI!-,:"

    1. $tart the program.

    2. 7et the choice from the user.

    +. 'f the choice is to add records? get the data from the user and add them to the

    list.

    . 'f the choice is to delete records? get the data to 8e deleted and delete it from

    the list.

    /. 'f the choice is to display num8er of records? count the items in the list and

    display.

    9. 'f the choice is to search for an item? get the item to 8e searched and respond

    yes if the item is found? otherwise no.

    =. Terminate the program

    D3T3 $T45CT54,$ 36 9

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    7/34

    OU!U!:"

    1.3dd records

    2.Delete records

    +.Display records.Count no. of items in the list

    /.$earching an item in the list

    9.,it

    ,nter your choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    8/34

    9.,it

    ,nter your choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    9/34

    *OU/( IN)E* IS!

    AI,:"

    To write a FCG program to create a Dou8ly linked list implementation.

    A'ORI!-,:"

    1. $tart the program.

    2. 7et the choice from the user.

    D3T3 $T45CT54,$ 36 K

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    10/34

    +. 'f the choice is to add records? get the data from the user and add them to the

    list.

    . 'f the choice is to delete records? get the data to 8e deleted and delete it from

    the list.

    /. 'f the choice is to display num8er of records? count the items in the list and

    display.

    9. 'f the choice is to search for an item? get the item to 8e searched and respond

    yes if the item is found? otherwise no.

    =. Terminate the program

    OU!U!:"

    1.3dd records

    2.Delete records+.Display records

    .Count no. of items in the list

    /.$earching an item in the list

    9.,it

    D3T3 $T45CT54,$ 36 10

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    11/34

    ,nter your choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    12/34

    ,nter your choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    13/34

    O(NO,IA A**I!ION

    Ex. No : 0

    AI,:"

    To write a FCG program to represent a polynomial as a linked list and write

    functions for polynomial addition

    A'ORI!-,:"

    1. $tart the program

    2. 7et the coefficients and powers for the two polynomials to 8e added.

    +. 3dd the coefficients of the respecti(e powers.

    . Display the added polynomial.

    /. Terminate the program.

    D3T3 $T45CT54,$ 36 1+

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    14/34

    OU!U!:"

    ,nrter the elements of the first polynomial < ,nter the coff and pow

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    15/34

    Ex. No : 5

    AI,:"

    To write a FCG program to implement stack and use it to con(ert infi to postfi

    epression.

    A'ORI!-,:"

    1. $tart the program

    2. $can the 'nfi string from left to right.

    +. 'nitialise an empty stack.

    . 'f the scannned character is an operand? add it to the -ostfi string. 'f the

    scanned character is an operator and if the stack is empty -ush the character to

    stack.

    'f the scanned character is an &perand and the stack is not empty?

    compare the precedence of the character with the element on top of the

    stack "top$tack#. 'f top$tack has higher precedence o(er the scanned

    character -op the stack else -ush the scanned character to stack.

    4epeat this step as long as stack is not empty and top$tack has

    precedence o(er the character.

    4epeat this step till all the characters are scanned.

    /. "3fter all characters are scanned? we ha(e to add any character that the stack

    may ha(e to the -ostfi string.# 'f stack is not empty add top$tack to -ostfi

    string and -op the stack. 4epeat this step as long as stack is not empty.

    9. 4eturn the -ostfi string.

    =. Terminate the program.

    D3T3 $T45CT54,$ 36 1/

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    16/34

    OU!U!:"

    enter the infi epression aE"sdIf#c

    the postfi epression is asdfIEc

    RESU!:"

    The gi(en program is implemented? eecuted? tested and (erified successfully.

    D3T3 $T45CT54,$ 36 19

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    17/34

    I,E,EN!A!ION O3 !REE !RA2ERSAS

    Ex. No : 6

    AI,:"

    To write a C program to implement an expression tree. Produce its pre-order,in-order, and post-order traversals.

    A'ORI!-,:"

    $tep 1< $tart the process.

    $tep 2< 'nitialize and declare (aria8les.

    $tep +< ,nter the choice. 'norder I -reorder I -ostorder.

    $tep < 'f choice is 'norder then

    o Tra(erse the left su8tree in inorder.

    o -rocess the root node.

    o Tra(erse the right su8tree in inorder.

    $tep /< 'f choice is -reorder then

    o -rocess the root node.o Tra(erse the left su8tree in preorder.

    o Tra(erse the right su8tree in preorder.

    $tep 9< 'f choice is postorder then

    o Tra(erse the left su8tree in postorder.

    o Tra(erse the right su8tree in postorder.

    o -rocess the root node.

    $tep=< -rint the 'norder I -reorder I -ostorder tra(ersal.

    $tep J< $top the process.

    D3T3 $T45CT54,$ 36 1=

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    18/34

    RESU!:"

    The gi(en program is implemented? eecuted? tested and (erified successfully

    D3T3 $T45CT54,$ 36 1J

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    19/34

    I,E,EN! /INAR( SEAR- !REE

    Ex. No : 7

    AI,:"

    To write a C program to implement binary search tree.

    A'ORI!-,:"

    $tep 1< $tart the process.

    $tep 2< 'nitialize and declare (aria8les.

    $tep +< Construct the Tree

    $tep < Data (alues are gi(en which we call a key and a 8inary search tree

    Step ! To search for the key in the gi(en 8inary search tree? start with the root node and

    Compare the key with the data (alue of the root node. 'f they match? return theroot pointer.

    Step "! 'f the key is less than the data (alue of the root node? repeat the process 8y using

    the left su8tree.

    $tep =< &therwise? repeat the same process with the right su8tree until either a match is

    found or the su8tree under consideration 8ecomes an empty tree.

    $tep J< Terminate

    D3T3 $T45CT54,$ 36 1K

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    20/34

    OU!U!:

    6'34N $,34CO T4,,

    EEEEEE EEEEEE EEEE:ain :enu

    1.Create

    2.'nsert

    +.Delete.%ind

    /.%ind:in

    9.%ind:a

    =.'norderJ.-reorder

    K.-ostorder

    10.,it

    ,nter ur choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    21/34

    K.-ostorder

    10.,it

    ,nter ur choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    22/34

    EEEEEE EEEEEE EEEE

    :ain :enu

    1.Create

    2.'nsert

    +.Delete.%ind

    /.%ind:in

    9.%ind:a=.'norder

    J.-reorder

    K.-ostorder

    10.,it,nter ur choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    23/34

    =.'norder

    J.-reorder

    K.-ostorder10.,it

    ,nter ur choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    24/34

    +.Delete

    .%ind

    /.%ind:in9.%ind:a

    =.'norder

    J.-reorderK.-ostorder

    10.,it

    ,nter ur choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    25/34

    RESU!:"The gi(en program is implemented? eecuted? tested and (erified successfully

    I,E,EN!A!ION O3 RIORI!( #UEUE USIN' -EAS

    Ex. No : 8

    AI,:"

    To implement priority @ueue using heaps.

    A'ORI!-,:"

    $tep 1< $tart the -rogram

    $tep 2< heap is a 8inary tree with two important properties! getast"#

    (alue lastode.getContent"#swap"lastode? root#

    update lastode

    return (alue

    OU!U!:

    -riority Aueue using Oeap

    ,nter the $ize of the -riority @ueue

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    27/34

    1. 'nsert

    2. Delete:in

    +. Display. ,it

    ,nter u r choice

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    28/34

    -riority Aueue Display

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    29/34

    I,E,EN!A!ION O3 *I)S!RA;S A'ORI!-,Ex. No : s Algorithm:

    E$1&is the set of the sides of the minimum genetic tree.

    E$+&is the set of the remaining sides.

    E$1&0?E$+&,Hhile E$1&contains less then n-1sides and E$+&?0 do

    D3T3 $T45CT54,$ 36 +2

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    33/34

    %rom the sides of E$+&choose one with minimum cost!!;e"iR#

    E$+&E$+&!Ye"iR# Z

    'f 2$i&?2$9&do not 8elong in the same tree then

    o unite the trees of 2$i&and 2$9&to one tree.

    end "'f#

    end "Hhile#

    ,nd &f 3lgorithm.

    )ruskal>s Algorithm:

    1 functionSruskal"G#

    2 for each(erte vin Gdo

    + Define an elementary cluster C"v# [ YvZ. 'nitialize a priority @ueue Qto contain all edges in G? using the weights as

    keys./ Define a tree T[ \ IITwill ultimately contain the edges of the :$T9 II n is total num8er of (ertices

    = @hileThas fewer than n!1 edges oJ II edge u?( is the minimum weighted route fromIto (K "u?v# [ Q.remo(e:in"#

    10 II pre(ent cycles in T. add u?( only if T does not already contain a path

    8etween u and (.

    11 II ote that the cluster contains more than one (erte only if an edgecontaining a pair of

    12 II the (ertices has 8een added to the tree.

    1+ et C"v# 8e the cluster containing v? and let C"u# 8e the cluster containing u.1 ifC"v# ] C"u# then1/ 3dd edge "v?u# to T.

    19 :erge C"v# and C"u# into one cluster? that is? union C"v# and C"u#.1= returntree T

    D3T3 $T45CT54,$ 36 ++

  • 8/13/2019 Data Structure and Algorithm Lab Manual

    34/34

    RESU!:"

    The gi(en program is implemented? eecuted? tested and (erified successfully