implement stack using array in c. A stack is a data structure where the addition of new items and the removal of existing items always takes place at the top. This is also known as LIFO ( last in first out ). The operation supported by stack is (1) push (2) pop (3) top (4) isEmpty. The push adds an item to the top of the stack, top returns the topmost item,pop removes the item from the top and isEmpty informs whether the stack is empty or not. 

implement stack using array in c

Array-based implementation

To implement stack using array we have to maintain following properties.

  • An array of default size (>=1).
  • Capacity of stack which is array size.
  • A variable top , initialized with -1 and range up to Capacity -1.
  • When top == -1 , this shows empty stack.
  • When top = Capacity -1, this shows stack is full.

we need to keep track of the array contents and a top index and size. if we put these two into struct we can  track them easily.


Related Contents to follow