This is an algorithm for stack using linked list.


DATA STRUCTURE DEFINITION

___________________________________________________________________________________

 

NODE: Data {To store data  in  the Node}

Next {To store link of  the next Node}

_____________________________________________________________________

Algorithm for Operations in a Stack

Algorithm for push operation

_______________________________________________________________________________

INPUT : A value to be inserted

OUTPUT : The data value inserted at the end of the list ___________________________________________________________________________________

Step 1: Input v

Step 2: new node = createnode(creates a new node)

Step 3: new node->data = v

Step 4: new node->next = NULL

Step 5: if head = NULL then,

A.                   head=new node

Step 6: else

A. New node->next = head

B. Head = new node

(End of if...else)

Step 7: top++

Algorithm for pop operation _________________________________________________________________________________

INPUT : A list of data

OUTPUT : The data value to be deleted from beginning of the list

___________________________________________________________________________________

Step 1: Input p

Step 2: If top > = 1 then

A.                   tmp = head

B.                   Head = head ->next

C.                   v = tmp->data

D.                   Free tmp

E.                    Top --

Step 3:else

A.                   v= -999

B.                   Return v

___________________________________________________________________________________


0 comments:

Post a Comment