DS_Doc

download DS_Doc

of 14

Transcript of DS_Doc

  • 8/7/2019 DS_Doc

    1/14

    LINEAR SEARCH

    START LOCATION= -1READ THE ARRAY A[MAX]

    ENTER THE VALUE TO BE SEARCHED, M

    I= 1DO WHILE I

  • 8/7/2019 DS_Doc

    2/14

    INSERTION SORT

    FOR I =1 TO N-1TEMP = A[I] //FIRST ELEMENT OF UNSORTED PART

    J = I-1 // LAST EMEMENT OF SORTED PART

    WHILE (TEMP=0)A[J+1] = A[J]

    J=J-1

    END WHILEA[J+1] = TEMP

    END FOR

    PRINT THE LIST

    BUBBLE SORTC = 1

    DO WHILE (C

  • 8/7/2019 DS_Doc

    3/14

    QUICK SORT

    Q_SORT(ARR, FIRST, LAST)LOW=FIRST,HIGH = LAST, PIVOT = ARR[(LOW+HIGH)/2]

    DO WHILE (LOW PIVOT)

    LOW = LOW+1

    END DO

    DO WHILE (ARR[HIGH] > PIVOT)

    HIGH = HIGH 1END DO

    IF LOW

  • 8/7/2019 DS_Doc

    4/14

    IF (A>B)

    FOR(D = C TO HIGH ), DOFIND[B] = LIST[D]

    B = B +1

    END FORELSE

    FOR(D = A TO MID ), DO

    FIND[B] = LIST[D]B = B+1

    END FOR

    END IF

    SHELL SORT

    S_SORT( ARR, SIZE)

    ARR -> REPRESENTS THE LIST OF ELEMENTSSIZE-> RWPRESENTS THE SIZE OF THE LIST

    INITIALIZE THE GAP = GAP/2DO WHILE GAP = GAP/2

    SWAP = 0

    DO WHILE (SWAP)FOR I = 1 TO SIZE GAP, DO

    IF(ARR[I] > ARR[I+1])

    TEMP = ARR[I]

    ARR[I] = ARR[I+1]ARR[I+1] = TEMP

    SWAP = 1END IF

    END FOR

    END OF DO

    END OF DO

    RADIX SORT

    Step 1 : larg = Large (rec)

    m = num (larg)

    Step 2: Repeat through step 5 for j = 0,1,2,..,m

    Step 3: Repeat for I = 0,1,2,,9

    pocket [i] = NULLpocket1[i] = NULL

    Step 4: r = recrepeat while r NULL

  • 8/7/2019 DS_Doc

    5/14

    dig = digit (link [r], j)

    Update (dig, r)

    nex = link [r]r = nex

    if (r NULL)

    dig = digit (link[r], j)Update(dig, r)

    Step 5: poc = 0Repeat while pocket1[poc] = NULL

    poc = poc + 1

    rec = Make_link (poc, rec)

    Step 6: Return

    ALGORITHM TO IMPLEMENT A STACK USING SINGLY LINKED LIST.

    Procedure: Push an element into the stack.

    1. [Check overflow condition]

    If Stk = NULL

    Output Overflow and exit.

    2. [Remove first node from stk lit]

    Set new = Stk

    Stk = link[Stk]

    3. [Copy item into new node]

    Set info[new] = item

    4. [New node points to the original top node in the stack]Set link[new] = top

    5. [Reset top to point to the new node at the top of the stack]

    Set top = new

    6. Exit.

    Procedure: Pop an element from the stack.

    1. [Checks underflow condition]

    If top = NULL

    Output Underflow and return.

    2. [Copy the top elementof stack into item]

    Set item = info[top]

    3. [Assign old top value to temp and reset top pointer]

    temp = top

    top = link[top]

    4. [Return deleted node to the Stk list]

  • 8/7/2019 DS_Doc

    6/14

    Set link[temp] = Stk

    Stk = temp

    5. Return.

    ALGORITHM TO IMPLEMENT A QUEUE USING

    ARRAYS

    Procedure : Insert an element in the queue

    if (rear >= size) // [check overflow condition]

    print OVERFLOW and exitelse

    rear = rear + 1

    Q[rear = value] // insert an element

    If (front = 0) //set front pointerFront = 1

    End if

    Return

    Procedure : Delete an element from the queue

    If (front = 0) // check underflow condition

    Print UNDERFLOW and exit

    ElseValue = Q[front]

    If (front = rear) //check for empty queue

    Front = 0Rear = 0Else

    Front = front +1

    End ifEnd if

    Return

    DequeALGORITHM TO IMPLEMENT A QUEUE USING ARRAYS.

    Procedure: Insert an element in the queue.

    1. [Check overflow condition]

    If rear >= size

    Output Overflow and exit.

    2. [Increment rear pointer]

    rear = rear+1

    3. [Insert an element]

  • 8/7/2019 DS_Doc

    7/14

    Q[rear] = value

    4. [Set the front pointer]

    If front = 0

    front=1

    Return.

    Procedure: Delete an element from the queue.

    1. [Check underflow condition]

    If front = 0

    Output Underflow and return.

    2. [Remove an element]

    value = Q[front]

    3. [Check for empty queue]

    If front = rear

    front = 0

    rear = 0

    ElseFront = front+1

    4. Return.

    IMPLEMENT SINGLE LINKED LIST

    //Add an element to the head of the linkded listprocedure AddTohead (element)

    head = new Node (element, head)

    if (not the Tail)

    Tail = head

    End if

    End procedure

    // Add an element to the tail of the linked listprocedure AddToTail (element)

    if (this is the Tail)

    Tail -> next = new Node (element)

    Tail = Tail-> nextEnd if

    Else head = Tail = new Node (element)

    End procedure

    // Remove an element from the head of the linked listprocedure RemoveFromHead () : integer

    if (this is the Head)

    element = head ->info

    temp = head

    if (this is the last item in the list)

    Break pointer connection between head and tail to terminate list

    Else

    Set = head->next = next item in the listEnd if

    Release temp from memory pool

  • 8/7/2019 DS_Doc

    8/14

    Return element

    Else

    Return 0

    End if

    End procedure

    // Remove an element from the tail of the linked listprocedure RemoveFromTail () : integerif (this is the Tail)

    element = Tail->info

    if (this is the last item in the Tail)

    Break pointer connection between head and Tail to terminate list

    Release head from memory pool

    Else

    Traverse list until we reach the Tail

    Release Tail from the listSet Tail pointer to previous element if previous element exist

    Set Tail pointer to next to nullEnd if

    Return element

    Else

    Return 0

    End if

    End procedure

    ALGORITHM :IMPLEMENT SINGLY LINKED LIST

    //Add an element to the start of the linked list

    procedure AddToHead(element)

    head = new Node(element, head)

    if(not the Tail)

    tail = head

    end if

    end procedure

    // Add an element to the tail of the linked list

    procedure AddToTail(element)

    if(this is the Tail)

    tail->next = new Node(el)

    tail = tail->next

    endif

    else head = tail = new Node(el)

    end procedure

    // Remove an element from the head of the linked list

    procedure RemoveFromHead():integer

    if(this is the Head)

    element = head->info

    temp = head

    if(this is the last item in the list)

    Break pointer connection between head and tail to

    terminate list

    else

  • 8/7/2019 DS_Doc

    9/14

    set head.next to equal next item in list

    end if

    release temp from from memory pool

    return element

    else

    return 0

    end if

    end procedure

    // Remove an element from the tail of the linked list

    procedure RemoveFromTail():integer

    if(this is the Tail)

    element = tail->info

    if(this is the last item in the list)

    Break pointer connection between head and tail to terminate

    list release head from memory pool.

    else

    Traverse list until you reach the tail release tail from

    memory pool

    set tail pointer to previous element if one exists settail's pointer to next element to null.

    end if

    return element

    else

    return 0

    end if

    end procedure

    ALGORITHM TO IMPLEMENT A QUEUE USING SINGLY LINKED LIST

    Procedure: Insert an element into the queue.

    1. [Check overflow condition]If Q = NULL

    Output Overflow and return

    2. [Remove first node from Q list]

    Set new= Q

    Q = link[Q]

    3. [Copy item into new node]

    info[nwew] = item

    link[new] = NULL

    4. [If Q is empty then item is the first element in the queue Q]

    If (front = NULL)

    Front = rear = new

    Else

    Set link[rear] = new

    rear = new

    5. Exit.

    Procedure: Delete an element from the queue.

  • 8/7/2019 DS_Doc

    10/14

    1. [Check underflow condition]

    If (front = NULL)

    Output Underflow and return.

    2. [If Q nonempty, copy front in temp]

    Set temp = front

    item=info[temp]

    3. [Reset front to point to the next element in the Q]

    front = link[temp]

    4. [Return the deleted node temp to the list Q]

    link[temp] = Q

    Q = temp

    5. Exit.

    IMPLEMENT DOUBLY LIST

    //Add an element after specified position in the listprocedure insertAfter (List list, Node node, Node newNode)

    newNode->prev = node

    newNode->next = node->next

    if (node->next = NULL)list->lastNode

    else

    node->next = newNodenode->next = newNode

    //Add an element before specified position in the linked list

    procedure insertBefore (List list, Node node, Node newNode)

    newNode->prev = node->prev

    newNode->next = nodeif (node->prev = NULL)

    list->firstNode = newNode

    elsenode->prev->next = newNode

    node->prev = newNode

    //Add an element to the start of the linked list

    procedure insetrtBeginning (List list, Node node, Node newNode)if (list->firstNode = NULL)

    list->firstNode = newNode

    list->lastNode = newNode

    newNode->prev = NULLnewNode->next = NULL

    else

  • 8/7/2019 DS_Doc

    11/14

    insert (list, list->firstNode, newNode)

    //add an element to the end of the linked list

    procedure insertEnd (List list, Node newNode)

    if (list->lastNode = NULL)insertBeginning (list, newNode)

    else

    insertAfter (list, list->lastNode, newNode)

    ALGORITHM : IMPLEMENT DOUBLY LINKED LIST

    //Add an element after specified position in the linked list

    procedure insertAfter(List list, Node node, Node newNode)

    newNode.prev := node

    newNode.next := node.next

    if node.next = nulllist.lastNode := newNode

    else

    node.next.prev := newNode

    node.next := newNode

    //Add an element before specified position in the linked list

    procedure insertBefore(List list, Node node, Node newNode)

    newNode.prev := node.prev

    newNode.next := node

    if node.prev is null

    list.firstNode := newNode

    else

    node.prev.next := newNode

    node.prev := newNode

    //Add an element to the start of the linked list

    procedure insertBeginning(List list, Node newNode)

    if list.firstNode = null

    list.firstNode := newNode

    list.lastNode := newNode

    newNode.prev := null

    newNode.next := null

    else

    insertBefore(list, list.firstNode, newNode)

    //Add an element to the end of the linked list

    procedure insertEnd(List list, Node newNode)

    if list.lastNode = null

    insertBeginning(list, newNode)

    else

    insertAfter(list, list.lastNode, newNode)

  • 8/7/2019 DS_Doc

    12/14

    // Remove an element from the linked list

    procedure remove(List list, Node node)

    if node.prev = null

    list.firstNode := node.next

    else

    node.prev.next := node.next

    if node.next = null

    list.lastNode := node.prev

    else

    node.next.prev := node.prev

    destroy node

    ALGORITHM TO CONVERT INFIX EXPRESSION TO POSTFIX FORM.

    1. Initialize the stack.

    2. Scan the leftmost symbol in the given infix expression and

    denote it as current input symbol.

    3. Repeat through step 6 while infix expression.

    4. Remove and output all stack symbols whose precedence values

    are greater than or equal to the precedence of the current input

    symbol.

    5. Push the current input symbols onto the stack.

    6. Scan the leftmost symbol in the infix expression and let it be

    the current input symbol.

    ALGORITHM TO CONVERT INFIX EXPRESSION TO POSTFIX FORM

    1. Initialize the stack

    2. Scan the leftmost symbol in the given infix expression and denote it as

    current input symbol.

    3. Repeat through step 6 while infix expression.

    4. Remove and output all stack symbols whose precedence values are

    greater than or equal to the precedence of the current input symbol.

    5. Push the current input symbols onto the stack.

    6. Scan the leftmost symbol in the infix expression and let it be the

    current input symbol.

    ALGORITHM TO EVALUATE THE POSTFIX EXPRESSION.

    1. Add a right paranthesis ) at the end of P.

    2. Scan P from left to right and repeat steps 3 and 4 for each element

    of P until the sentinel ) is encountered.

  • 8/7/2019 DS_Doc

    13/14

    3. If an operand is encountered, put it on STACK.

    4. If an operator x is encountered , then:

    a) Remove the two top elements of STACK, where A is the top

    element and B is the next-to-top element.

    b) Evaluate B x A.

    5. Place the result of (b) back on Stack.

    6. Set Value equal to the top element on STACK.

    7. Exit.

    ALGORITHM TO EVALUATE THE POSTFIX EXPRESSION

    1. Add a right parenthesis ) at the end of P

    2. Scan P from left to right and repeat steps 3 and 4 for each element of P untilthe sentinel ) is encountered.

    3. If an operand is encountered, put it on STACK.

    4. If an operator X is encountered, then :remove the two top elements of stack and perform the operation X

    5. Place the evaluated result back on the STACK

    6. Set value equal to the top element on STACK

    7. Exit

    BFS

    Input :V, the starting vertexOutput : A list VISIT giving the order of visit of vertices during traversal

    Data Structure : linked structure of graph . gptr is the pointer to a Graph.

    Steps :

    If gptr = NULL thenPrint graph is empty and exit

    Else

    u = v // starts from v

    open.ENQUENE (u) enter the starting vertex in openqwhile (openq.STATUS != EMPTY) // till the openq is not empty

    u = openq.DEQUENE ()

    if (SEARCH_SL_(VISIT, u) = FALSE) then //if u is not VISIT

    // then VISIT itINSERT_SL_END (VISIT, u) // store u in VISIT

    ptr = GPTR[u]while (ptr.link!= NULL) do // to store all the adjacent

    //vertices of V in openq

    vptr = ptr.linkopenq.ENQUENE (vptr.LABEL)

    end while

  • 8/7/2019 DS_Doc

    14/14

    end if

    end while

    end ifreturn (VISIT)

    Stop

    DFS

    Input : v, the starting vertex

    Output : A list VISIT giving the order of visit of vertices during Traversal

    Data Structure : linked structure of graph. GPTR is the pointer to a Graph

    Steps:

    1 if GPTR = NULL

    1 print GRAPH IS EMPTY

    2 EXIT2 end if

    3 u = v //start from V4 open.push (u)//push the starting vertex in OPEN

    5 while (Open.Top!= NULL) //till the stack is not empty

    1 u = Open.pop () //pop the top element from Open

    2 if (SEARCH_SL_END (VISIT, u) = FALSE) then //if u is not visited

    1 INSERT_SL_END (VISIT , u) //Store u in VISIT

    2 Ptr = GPTR [u] //To push all adjacent vertices of u into

    stack

    3 While (ptr.link!=NULL) dovptr = ptr.linkOpen.push (vptr.LABLE)

    4 end while

    3 end if 6 End while

    7 Return (VISIT)

    8 STOP