A simple implementation of a stack – C language

A stack is a last in, first out (LIFO) abstract data type and linear data structure.

It  is characterized by two fundamental operations, called push and pop. The push operation adds a new item to the top of the stack, while the  pop operation removes an item from the top of the stack.

The stack data type can be used in several applications:

  • Towers of Hanoi
  • Expression evaluation and syntax parsing
  • Rearranging railroad cars
  • Backtracking
  • Quicksort
  • Runtime memory management

and many more

Now let's write few lines of code:

First of all the data structure:



then we are going to write the prototypes:




The isEmpty function will check if the stack is empty:



the popItem will extract the last inserted item 



the pushItem will add a new item at the beginning of the srtucture:



Last we are going to write a simple test program, it will push into the stack 10 items and then it will pop 10 items and print back the results:


That's all.


Releated Post

Leave a Reply

Your email address will not be published. Required fields are marked *