You are on page 1of 14

(1) Algorithm for PUSH Operation. Push(S, Top, X) This algorithm insert an element X to the top of the stack.

The Stack is represented by vector S which contains N elements. TOP is a pointer which points to the top element of the Stack 1. [Check for stack overflow] If TOP >= N then Write (Stack Overflow) Return 2. [Increment TOP] TOP = TOP + 1 3. [Insert Element] S [TOP] = X 4. [Finished] Return (2) Algorithm for POP Operation. POP(S, TOP) This algorithm removes an element from the Top of the Stack. The Stack is represented by vector S which contains N elements. TOP is a pointer which points to the top element of the Stack 1. [Check for the Underflow on the Stack] If TOP = 0 then Write (STACK UNDERFLOW ON POP) Exit 2. [Decrement Pointer] TOP = TOP - 1 3. [Return former top element of the stack] Return(S [TOP + 1]) (3) Algorithm for PEEP Operation. PEEP(S, TOP, I) This algorithm returns the value of the i th element from the Top of the Stack. The Stack is represented by vector S which contains N elements. TOP is a pointer which points to the top element of the Stack 1. [Check for the Underflow on the Stack] If TOP I + 1 <= 0 then Write (STACK UNDERFLOW ON PEEP) Exit 2. [Return the ith element from the TOP of the Stack] Return(S [TOP I + 1]) (4) Algorithm for CHANGE Operation.

CHANGE(S, TOP, X, I) This algorithm changes the value of the I th element from the Top of the Stack by X The Stack is represented by vector S which contains N elements. TOP is a pointer which points to the top element of the Stack 1. [Check for the Underflow on the Stack] If (TOP I + 1 ) <= 0 then Write (STACK UNDERFLOW ON CHANGE) Exit 2. [Change ith element from the TOP of the Stack] S [TOP I + 1] X 3. [finished] Return

Linked implementation of stack


(1) Algorithm for push operation PUSH(TOP,X) TOP is a pointer which points to the first node in the stack X is the element which we want to insert in to stack. 1. [Check for Availability stack underflow] If AVAIL = NULL then Write Availability stack underflow Return 2. [Check for the stack element and insert element] If TOP = NULL then NEW AVAIL AVAILLINK (AVAIL) INFO (NEW) X LINK (NEW) NULL TOPNEW Else NEWAVAIL AVAILLINK (AVAIL) INFO (NEW) X LINK (NEW) TOP TOPNEW 3. [Finished] Return (2) Algorithm for pop operation POP(TOP) TOP is a pointer which points to the first node in the stack 1. [Check for empty stack] If TOP = NULL then Write Stack is empty

Return 2. [Check for the element in the stack and delete it] If LINK (TOP) = NULL then YINFO (TOP) TOPNULL Else (Assign the address pointed by TOP pointer to TEMP pointer) TEMPTOP YINFO (TEMP) (Assign the address of next node to TOP pointer) TOPLINK (TEMP) 3. [Finished] Return (1) Algorithm to insert an element in queue QINSERT (Q, F, R, N, Y) This function insert an element into the queue The Queue is represented by vector Q which contains N elements. F is a pointer which points to the front end R is a pointer which points to the rear end Y is the element to be inserted. (1) [Overflow?] If RN then Write (Overflow) (2) [Increment rear pointer ] RR+1 (3) [Insert element] Q[R]Y (4) [Is front pointer properly set?] If F=0 then F1 Return (2) Algorithm to delete an element from the queue QDELETE (Q, F, R) The Queue is represented by vector Q which contains N elements. F is a pointer which points to the front end R is a pointer which points to the rear end (1) [Underflow?] If F = 0 then Write (UNDERFLOW) Return (0) (2) [Delete element] YQ [F] (3) [Queue empty?] If F =R

Then FR0 Else FF+1 (4) [Return element] Return (y)

Linked implementation of queue


(1) Algorithm to insert an element in queue. QINSERT(FRONT,REAR,X) FIRST is a pointer which points to the first element in the queue. REAR is a pointer which points to the last element in the queue. X is an element which we want to insert in to queue. 1. [Check for availability stack underflow] If AVAIL = NULL then Write Availability stack underflow Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) X LINK (NEW) NULL 5. [If queue is empty?] If REAR = NULL then REARNEW FRONTNEW 6. [initialize search for last node] SAVEREAR 7. [Search end of the list] Repeat while LINK (SAVE) NULL SAVELINK (SAVE) 8. [Set LINK field of last node to NEW ] LINK (SAVE) NEW 9. [set REAR pointer to point to the LAST element] REARNEW 10. [Finished] Return (2) Algorithm to delete an element from queue. QDELETE(FRONT,REAR) FIRST is a pointer which points to the first element in the queue. REAR is a pointer which points to the last element in the queue. 1. [Check for empty Queue] If FRONT = NULL then Write Queue is empty

Return 2. [Check for elements in Queue and delete element] If LINK (FRONT) = NULL then YINFO (FRONT) FRONTNULL REARNULL Else (Assign the address pointed by FRONT pointer to TEMP pointer) TEMPFRONT YINFO (TEMP) (Assign the address of next node to FRONT pointer) FRONTLINK (TEMP) 3. [Finished] Return (1) Algorithm to insert element in circular queue. CQINSERT(Q,F,R,N,Y) This function inserts an element in to circular queue. The Queue is represented by vector Q which contains N elements. F is a pointer which points to the front end R is a pointer which points to the rear end Y is the element to be inserted. (1) [Reset rear pointer?] If R = N then R1 Else RR + 1 (2) [Overflow?] If F = R then Write (queue overflow) (3) [Insert element] Q[R]Y (4) [Is front pointer properly set?] If F = 0 then F1 Return (2) Algorithm to delete element from circular queue. CQDELETE(Q,F,R) This function deletes an element from circular queue The Queue is represented by vector Q which contains N elements. F is a pointer which points to the front end R is a pointer which points to the rear end (1) [Underflow] If F = 0 then

Write (Underflow) (2) [Delete element] Return (Q[F]) (3) [Queue empty?] If F = R then FR0 (4) [Increment front pointer] If F = N then F1 Else FF+1

Algorithms for Dequeue


(1) Algorithm for Insert an element in to Dequeue DQINSERT(DQ,F,R,N,X) This function inserts an element in to Dequeue. The Dequeue is represented by vector DQ which contains N elements. F is a pointer which points to the front end R is a pointer which points to the rear end X is the element to be inserted. 1. [Check overflow condition] If R >= N and F = 0 then Write Dequeue overflow Return 2. [Check front pointer value] If F > 0 then FF-1 Else Return 3. [Insert element at the front end] DQ [F] X 4. [Check rear pointer value] If R < N then RR+1 Else Return 5. [Insert element at the rear end] DQ [R] X 6. [Finished] Return (2) Algorithm for Delete an element from Dequeue. DQDELETE(DQ,F,R) This function deletes an element from Dequeue The Queue is represented by vector DQ which contains N elements.

F is a pointer which points to the front end R is a pointer which points to the rear end [Check for underflow] If F = 0 and R = 0 then Write Dequeue underflow Return [Delete element from front end] If F > 0 then Return (DQ [F]) [Check queue for empty] If F = R then FR0 Else FF+1 [Delete element from rear end] If R > 0 then Return (DQ [R]) [Check for queue empty] If F = R then FR0 Else RR-1 [Finished] Return 1. Algorithm to insert new node at beginning of the linked list. INSERT(X,FIRST) This function inserts a new element X at the beginning of the linked list. FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write Availability stack underflow Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [Is the list is empty?] If FIRST = NULL then INFO (NEW) X LINK (NEW) NULL Else INFO (NEW) X LINK (NEW) FIRST 5. [Assign the address of the Temporary node to the First Node ] FIRSTNEW

6. [Finished] Return (FIRST) 2. Algorithm to insert new node at end of the linked list. INSERT(X,FIRST) This function inserts a new element X at the end of the linked list. FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write Availability stack underflow Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) X LINK (NEW) NULL 5. [If list is empty?] If FIRST = NULL then FIRSTNEW 6. [initialize search for last node] SAVEFIRST 7. [Search end of the list] Repeat while LINK (SAVE) NULL SAVELINK (SAVE) 8. [Set LINK field of last node to NEW ] LINK (SAVE) NEW 9. [Finished] Return (FIRST) 3. Algorithm to insert new node at specific location. INSPOS(X,FIRST,N) This function inserts a new element N into the linked list specified by address X. FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write Availability stack underflow Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) N 5. [If list is empty?]

If FIRST = NULL then LINK (NEW) NULL FIRSTNEW 6. [Search the list until desired address found] SAVEFIRST Repeat while LINK (SAVE) NULL and SAVE X PREDSAVE SAVELINK (SAVE) 7. [Node found] LINK (PRED) NEW 8. [Finished] Return (FIRST) 4 Algorithm to insert new node in ordered linked list. INSORD(X,FIRST) This function inserts a new element X into the ordered linked list. FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write Availability stack underflow Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) X 5. [If list is empty?] If FIRST = NULL then LINK (NEW) NULL FIRSTNEW 6. [Does the new node precede all nodes in the list?] If INFO (NEW) INFO (FIRST) then LINK (NEW) FIRST FIRSTNEW 7. [Initialize search pointer] SAVEFIRST 8. [Search for predecessor of new node] Repeat while LINK (SAVE) NULL and INFO (LINK (SAVE)) INFO (NEW) SAVELINK (SAVE) 9. [Set LINK field of new node and its predecessor ] LINK (NEW) LINK (SAVE) LINK (SAVE) NEW 10. [Finished] Return (FIRST)

5. Algorithm to delete first node of linked list DELFIRST(FIRST) This function deletes a first node from the list. FIRST is a pointer which contains address of first node in the list. 1. [Check for empty list] If FIRST = NULL then Write List is empty Return 2. [Check for the element in the list and delete it] If LINK (FIRST) = NULL then YINFO (FIRST) FIRSTNULL Else (Assign the address pointed by FIRST pointer to TEMP pointer) TEMPFIRST YINFO (TEMP) (Assign the address of next node to FIRST pointer) FIRSTLINK (TEMP) 3. [Finished] Return (FIRST) 6. Algorithm to delete last node of linked list DELLAST(FIRST) This function deletes a last node from the list. FIRST is a pointer which contains address of first node in the list. 1. [Check for empty list] If FIRST = NULL then Write List is empty Return 2. [Check for the element in the list and delete it] If LINK (FIRST) = NULL then YINFO (FIRST) FIRSTNULL Else (Assign the address pointed by FIRST pointer to TEMP pointer) TEMPFIRST Repeat while LINK (TEMP) NULL PREDTEMP TEMPLINK (TEMP) 3. [Delete Last Node] YINFO (TEMP) LINK (PRED) NULL 4. [Finished] Return (FIRST)

7. Algorithm to delete specific node from linked list. DELPOS(FIRST,X) This function deletes a node from specific location from the list. FIRST is a pointer which contains address of first node in the list. 1. [Check for empty list] If FIRST = NULL then Write List is empty Return 2. [If there is only one node?] If LINK (FIRST) = NULL then YINFO (FIRST) FIRSTNULL 3. [If list contains more than one node?] (Assign the address pointed by FIRST pointer to TEMP pointer) TEMPFIRST Repeat while LINK (TEMP) NULL and TEMP X PREDTEMP TEMPLINK (TEMP) 4. [Node found?] If TEMP X then Write Node not found Else YINFO (TEMP) LINK (PRED) LINK (TEMP) 5. [Finished] Return (FIRST) 8. Count number of nodes in linked list COUNT(FIRST) This function counts number of nodes in the list. FIRST is a pointer which contains address of first node in the list. [Empty List?] If FIRST = NULL then Write List is empty [Initialize] Count1 SAVEFIRST [process the list until end of the list] Repeat while LINK (SAVE) NULL CountCount + 1 SAVELINK (SAVE) [Finished] Return (Count) 9. Algorithm to copy linked list

COPY(FIRST) This function copy one list into another list. FIRST is a pointer which contains address of first node in the list. BEGIN is a pointer which points to the address of first node in the list. 1 [Empty List?] If FIRST = NULL then BEGINNULL 2 [Copy First Node] If AVAIL = NULL then Write Availability stack underflow Else NEWAVAIL AVAILLINK (AVAIL) FIELD (NEW) INFO (FIRST) BEGINNEW 3 [Initialize traversal] SAVEFIRST 4 [Move to next node if not at end of the list] Repeat thru step 6 while LINK (SAVE) NULL 5 [Update predecessor and save pointers] PREDNEW SAVELINK (SAVE) 6 [Copy node] If AVAIL = NULL then Write availability stack underflow Else NEWAVAIL AVAILLINK (AVAIL) FIELD (NEW) INFO (SAVE) PTR (PRED) NEW 7 [Set link of last node and return] PTR (NEW) NULL 8 [Finished] Return

Algorithms for Doubly linked linier list


1. Algorithm to insert new node in doubly linked list DOUBLEINS(L, R, M, X) This function inserts an element into double linked list. L is a pointer which contains address of left most node in the list. R is a pointer which contains address of right most node in the list. X contains the value to be inserted in to the list 1 [Create new node]

If AVAIL = NULL then Write Availability stack underflow Else NEWAVAIL AVAILLINK (AVAIL) 2 [Copy information field] INFO (NEW) X 3 [insertion into an empty list] If R = NULL then LPTR (NEW)RPTR (NEW) NULL LRNEW 4 [left most insertion] If M = L then LPTR (NEW)NULL RPTR (NEW)M LPTR (M) NEW LNEW 5 [insert in middle] LPTR (NEW)LPTR (M) RPTR (NEW)M LPTR (M) NEW RPTR (LPTR (NEW))NEW 6 [Finished] 2. Algorithm to delete node from doubly linked list. DOUBLEDEL(L, R,OLD) This function inserts an element into double linked list. L is a pointer which contains address of left most node in the list. R is a pointer which contains address of right most node in the list. OLD contains address of node which we want to delete. 1 [Underflow] If R = NULL then Write Underflow 2 [Delete Node] If L = R then LRNULL Else if OLD = L then LRPTR (L) LPTR (L) NULL Else if OLD = R then RLPTR(R) RPTR(R) NULL Else RPTR (LPTR (OLD)) RPTR (OLD) LPTR (RPTR (OLD)) LPTR (OLD)

3 [Return Delete node] Restore (OLD) Return

You might also like