The concept of a stack is easy to understand. An example of a stack in the real world would be a stack of books or stack of papers etc. The idea of a stack in data structures is identical. There is a top and everything else is below the top. Creating a stack using a linked list is a good implementation because it allows for a stack of any size as long as resources such as memory permit. The main ideas of a stack are
Top,
Push and
Pop.
Top references the top of the stack. This is implemented using a pointer that will always be at the top of the stack.
Push is refering to adding and item onto the stack. After each P
ush is completed the newest item added would then become the
Top. Pop is used as a deletion method just as Push is used as an insertion. After each
Pop is called the
Top is removed from the list and the next node in that list becomes the new
Top.
This code example shows the push and pop methonds.