D3804A15 ds 3

Embed Size (px)

Citation preview

  • 8/14/2019 D3804A15 ds 3

    1/8

    HOMEWORK NO:3CAP205: Data Structure

    SUBMITTED TO: - SUBMITTED BY:-

    Lect. SANJAY SOOD SURENDRA

    MCA 3nd

    SEM

    ROLL NO- D3804A15

    REGD NO- 10806601

  • 8/14/2019 D3804A15 ds 3

    2/8

    Part-A

    1. write algorithm to delete a given node in two way link list

    Ans. Delete the given node:

    Forward[back[loc]]= forward[loc]And back[forward[loc]]=back[loc]

    Send the deleted node to the avail list

    Forward[loc]=avail and avail=loc

    Exit

    THE PROCES IS THE FOLLOWING:

    2. Discuss the Concept to insert an element in Link List at a given position And also

    implement that concept to insert ITEM=38 in the list at the location 3 where the list

    is as following:

    46,39,48,49,58

  • 8/14/2019 D3804A15 ds 3

    3/8

    Ans : In the one way list we simply assign the new node. And then we assign the link of new

    node to the next node of the location. And the link of the node of the before location to the new.

    Now the new node is inserted in between the two location.

    Set new=avail and avail=link[avail]

    Info[new]=item=38

    If loc=NULL THEN [INSERT AS FIRST NODE]

    o SET LINK[NEW]=START AND START=NEW

    LINK [NEW]=LOC AND LINK [LOC]=NEW

    EXIT

    46,39,48,49,58

    INSERTING ON THE 3RD LOCATION

    46394838 4958

    LET THE C=38

    3. Discuss the Concept to insert an element in Link List where the list is sorted . And

    also implement that concept to insert ITEM=45where the list is as following:

    33,44,55,66,88,99

  • 8/14/2019 D3804A15 ds 3

    4/8

    Ans ..

    For inserting the item in the sorted link list ,we have to find the location of the item. In the givenquestion the item is the 45. And the location is the after 44 and the before the 55.

    So if we want to insert the item in the link list then we have to find the location firstly. . the item

    will inserted between the 44 and 55.For finding the location:

    If Start=null then LOC=NULL and return

    If ITEM

  • 8/14/2019 D3804A15 ds 3

    5/8

    Array: for array queue the two varable is required front and rear that holed the index of the first

    and the last element of the queue..The memory is assign as follow:

    #define capacity 10

    Struct{

    Int front,rear;

    Int element[capacity];}queue;

    Queue q;

    From the above coding we can assign the array memory.And we can assign memory with the link list also..

    The momory is declared as follow:

    Struct f

    {

    Int info;Struct f *next;

    {node;

    Struct

    {Node *front;

    Node *front;

    }queue;

    Stack in the memory:

    Like the queue we can represent the stack usin array and link list both.

  • 8/14/2019 D3804A15 ds 3

    6/8

    It work on the concept of the first come last out.. for this we need the variable named top that

    hold the index of the top element of the stack and an array to hold the element of the stack.. in

    stack the concept will implemented as:#define max 10

    Struct

    { int top;Int element[max];

    }stack;

    Stack s;

    A stack can be represented as the linked list also.. so it is called the linked stack. The array based

    representation of stack suffers from following limitation.

    Size must be known

    5. Write algorithm to delete last node in circular linked list?

    Ans.:To delete any node in a circular linked list, you need a pointer to the Prior node, the Current

    node, and the Next node. If the list is not doubly linked, you don't need the pointer to the

    Prior node. You can get Prior and Next from Current (Current.prior and Current.next) or youcan walk the list looking for a node that points to Current.

    Then, set Prior.next = Next, Next.prior = Prior, and delete Current.

    If you have a single pointer to one of the nodes, and that node is being deleted, you will also

    need to update that pointer accordingly

    Part-B

    6. Write algorithm to convert infix expression to prefix expression?

    Ans. We will represent simplification as a list of the rules ,much like the rules for student and

    eliza. But since each simplification rule is an algebraic equation ,we will storeeach one as an exp

    rather than a rule.

    We have choice as to how general we wat our infix notation to be. Consider:(((a*(x2))+(b*x))+c)

    (a*x2+b*x+c)

    (a x 2 + b x+c)A x2 +b*x+c

    The first is fully parenthesized infix, the seci=on makes use of the operator precedence and the

    third makes use of imlicit multiplication as well as operator precedence. The fourth requires alexical analyzer to break lisp symbol into pieces..

    7. Write advantages and disadvantages of recursion.write various application and

    problem solving example that require the use of recursion?

    Ans.

    Advantage of the recursion:

  • 8/14/2019 D3804A15 ds 3

    7/8

    The code may be much easier to write.

    Some situations are naturally recursive

    Disadvantage of recursion:

    There is shallow recursion and there is deep recursion.

    Shallow recursion will not overflow the stack, but it may

    take an excessively long time to execute.

    Deep recursion is generally much faster, but it may

    overflow the stack.

    Application in the factorial:

    function factorial( x )

    {

    if x equals 0 then

    return 1

    endif

    return x * factorial( x-1 )

    }

    8. Write procedure to delete nth element from queue ?

    Ans:-

    A data structure in which objects are added to one end, called the tail, and removed from the

    other, called the head (- a FIFO queue). The term can also refer to a LIFO queue orstackwherethese ends coincide.

    Algorithm:-

    This procedure deletes an element from a queue and assigns it to the variable ITEM.

    1. If FRONT :=NULL,then:Write: UNDERFLOW and return.2. Set ITEM:=QUEUE[FRONT]

    3. If FRONT:=REAR,then ;

    Set FRONT:=NULL and REAR:=NULLElse if

    FRONT:=N,then;

    Set FRONT:=1.

    Else :FRONT:=FRONT+1.

    4.Return.

    http://en.wiktionary.org/wiki/data_structurehttp://en.wiktionary.org/wiki/tailhttp://en.wiktionary.org/wiki/headhttp://en.wiktionary.org/wiki/FIFOhttp://en.wiktionary.org/wiki/LIFOhttp://en.wiktionary.org/wiki/stackhttp://en.wiktionary.org/wiki/data_structurehttp://en.wiktionary.org/wiki/tailhttp://en.wiktionary.org/wiki/headhttp://en.wiktionary.org/wiki/FIFOhttp://en.wiktionary.org/wiki/LIFOhttp://en.wiktionary.org/wiki/stack
  • 8/14/2019 D3804A15 ds 3

    8/8