Stack Array Representation



Array Representation of Stack in C Programming :

We have already discussed What is Stack ?.
[box]Stack is used as Linear data structure which cane be accessed from only one end .[/box]
Stack can be represented as “Array” . For Representing Stack we have to declare the following data structure –

typedef struct stack
{
  int data[MAX];
  int top;
}stack;

Explanation :

  1. 1-D array is used to hold the element of the stack.
  2. Variable “top” keeps track of “Topmost” Element in the stack.
  3. “MAX” is used as Constant which gives maximum size of Stack.

Values of Stack and Top :

Operation Explanation
top = -1 -1 indicated Empty Stack
top = top + 1 After push operation value of top is incremented by integer 1
top = top – 1 After pop operation value of top is decremented by 1

Important Note :

[box]MAX : Any number which can represent the size of stack[/box]