Stack

 What is a Stack?

Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. In a stack, adding and removing of elements are performed at a single position which is known as "top". That means, a new element is added at top of the stack and an element is removed from the top of the stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) principle.

 


In a stack, the insertion operation is performed using a function called "push" and deletion operation is performed using a function called "pop".
In the figure, PUSH and POP operations are performed at a top position in the stack. That means, both the insertion and deletion operations are performed at one end (i.e., at Top).
 
A stack data structure can be defined as follows...

                    Stack is a linear data structure in which the operations are performed based on LIFO principle.

Stack can also be defined as

                    "A Collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle".

 

Working of Stack
 
Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full.



Operations on a Stack

The following are some common operations implemented on the stack:
  • push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs.
  • pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an underflow state.
  • isEmpty(): It determines whether the stack is empty or not.
  • isFull(): It determines whether the stack is full or not.'
  • peek(): It returns the element at the given position.
  • count(): It returns the total number of elements available in a stack.
  • change(): It changes the element at the given position.
  • display(): It prints all the elements available in the stack.
PUSH operation

The steps involved in the PUSH operation is given below:

  • Before inserting an element in a stack, we check whether the stack is full.
  • If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
  • When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
  • When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1, and the element will be placed at the new position of the top.
  • The elements will be inserted until we reach the max size of the stack.




POP operation

The steps involved in the POP operation is given below:

  • Before deleting the element from the stack, we check whether the stack is empty.
  • If we try to delete the element from the empty stack, then the underflow condition occurs.
  • If the stack is not empty, we first access the element which is pointed by the top
  • Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.

 


 

Stack data structure can be implemented in two ways. They are as follows...

  1. Using Array
  2. Using Linked List

When a stack is implemented using an array, that stack can organize an only limited number of elements. When a stack is implemented using a linked list, that stack can organize an unlimited number of elements.

 

 
 

No comments:

Post a Comment