It is a very important data structure to  Store a particular data format in it by only store an integer or characters or string. Stack also have a particular principle, the principal is last in first out .so let's see the algorithm of stack using array.


For C program : click here

**Algorithm for stack using array**

Data Structure Definition: -

                                         1.          An array stack [MAX], where MAX is the maximum size of the array.

                                         2.          Initialization top = -1

___________________________________________________________________

Algorithm for push operation: -

 

Input:   1. Stack element

              2. Element should be inserted into the top of the stack

Output:  stack after push operation

 

                       

Step 1: Increment top;

                       top=top+1

Step 2: if top=MAX then

              a.   print “Stack overflow”

b.       return

Step 3: else

a.      stack[top]=element

           (End of if condition)

Step 4: Return

______________________________________________________________________

Algorithm for pop operation: -

 

Input:   1.   Stack element

2.      Element should be delete from top of the stack

Output:  stack after pop operation

 

                       

Step 1: if (top = -1) then

a.      Print “Stack Underflow”  

Step 2: else

             a.   popelement = stack[top]

             b.  top = top-1

             c.  return popelement

           (End of if condition)

______________________________________________________________________

Algorithm for display the value: -

 

Input:   None

Output:  stack after traversal

_____________________________________________________________________

Step 1: if (top == -1)

a.      print "Stack is empty”

Step 2: else

a.      print “Stack element:”

b.      i =top

c.      while (i >=0)

1.  print the value of stack arr [i])

                    2.  i =i -1

________________________________________________________________

 

 

 

(End of Algorithm)

0 comments:

Post a Comment