Home Theory What is a Stack?

# What is a Stack?

0 comment

A stack is a simple data structure for storing data (similar to Linked List). In stack, the order in which the data arrives is important. The pile of plates at a cafeteria is a good example of stacks. The plates are added to the stack as they are cleaned. They are placed on top of the previous plates. When we require a plate, the plate at the TOP of the pile is taken and then put into use. This means that the plate that was placed in the pile at the starting will be the last one to be used.

Definition:  A stack is an ordered list in which insertion and deletion are done at one end, where the end is called as top. the last element inserted is the first one to be deleted. Hence, it is called LAST IN FIRST OUT (LIFO) or FIRST IN LAST OUT (FILO) list.

Special names are given to the two operations that can be made to a stack. Two type of operations are available in a stack:-

• PUSH:- In this operation, we add an element on top of the previous element of the stack such that the new element is now the TOP element.
• POP:- This is the deletion operation, when this operation is called, we simply return the TOP element from the stack and delete that element from the list.

In some cases, the stack available may be of a limited length. In those cases, if we try to PUSH elements in a stack beyond the set limit, it is known as stack overflow. If the stack is empty and we try to POP an element, it is known as stack underflow.

Here is a visualization of the stack in two operations PUSH and POP.

#### EXAMPLES OF STACK IN REAL LIFE

Consider a working day in the office. Let us assume a developer is working on a long-term project. The manager then gives the developer a new task, which is more important. The developer places the long term project aside and begins working on the new task. The phone then rings and there is an urgent requirement. The developer then PUSHES the current task in the pending tray and begins the urgent task. When this urgent task is finished, he POPS the task from pending tray and completes the new task that was assigned to him. Once that is completed, he POPS the task from pending tray again and resumes his long-term project.

#### MAIN OPERATIONS ON A STACK:-

• push( int ) :- Inserts a data into the stack
• pop :- Returns the topmost data from the stack

#### AUXILIARY OPERATIONS ON A STACK

• int Top( ) :- returns the topmost element on the stack
• int Size( ) :- returns the total size of the stack
• int isStackEmpty( ) :- returns true if the stack is empty
0 comment

#### You may also like

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More