Tutorials

Stack


Stacks Basic


A stack is a non-primitive linear data structure in which element can be inserted or deleted from only one end.

The end that allows addition and deletion operation is known as top of stack or TOS.

 

As addition and deletion is done only from the top of the stack so the last added element is the one which would first be removed. This makes stack a Last-in-first-out (LIFO) type of list data structure.

 

Example of stack

  • Plates in party
  • Coin stacker

              stack by tutorialsinhand

 

In parties we always see plates kept for guests dinners. Refer the diagram above.

Those plates are arranged such that we can only pick one up from the top. 

And new plates are also added from the top end only. So we can see both deletion (pick up) and insertion of plates is done from the top end only.

The plate kept last would be the first to be picked. This is the basic concept on which stack works. 

 

Operations on Stacks

From the above example we saw to operations.

  1. New plates are added on the top. This is known as push operation.
  2. Plate at the top are picked up. This is known as pop operation.

When new element is added at the top of the stack then this process is known as PUSH operation.

When element is removed or deleted from the top of the stack then this process is known as POP operation.

Stack Underflow

If we keep removing element from the stack continuously then a situation will arise when there will be no element left in the stack. This situation is known as stack empty or underflow.

At this scenario, top of the stack is equals to bottom of the stack.

 

Stack Overflow

If we keep adding element into the stack continuously then a situation will arise that no more element can be added into it. This situation is known as stack overflow.

At this point top of the size reaches the maximum size of the stack.

 



Please Share this page
Views : 46
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!