All That You Need To Know About Stack

# All That You Need To Know About Stack

clickHere
Updated on Nov 20, 2022 21:33 IST

Author: Kanika Joshi

A stack is a linear data structure that works upon the LIFO (Last in First out) or FILO(First) approach, this means the element which is inserted at last in the stack will be the first one to be removed from the stack. Stack is an Abstract Data Type, and it behaves similar to a real-world stack, like a pile of plates kept one over another, a deck of cards, etc.

In this article, we are going to discuss the stack, types of the stack, operations performed over the stack, working of the stack, etc.

The stack data structure can be visualized as a pile of plates where one plate is placed over another plate.

From the above image, we can understand the stack data structure easily, as if we have to place a new plate in the pile, we will keep it at the top of the stack, whereas if we have to remove the plate, the plate at the top of the pile will be removed first. So, this is working on the principle of LIFO i.e. Last in First Out.

You can also explore – All You Need to Know About Algorithms and Data Structures

## Principle of Stack:

In stack, there are two fundamental operations, PUSH and POP. When an item is inserted into the stack the PUSH operation is performed whereas when an item is removed from the stack the POP operation is performed. In a stack, these operations are used while following the LIFO (Last in First out) or FILO (First In Last Out) principle.

Let’s Understand with the help of an example:

In the above image, A is the first item to be pushed or inserted into the stack, then we insert B and at last C. The C was the last item to be inserted and when the POP operation was applied, C was the first to be removed from the top of the stack.

Note: In stack, all the operations are performed at TOP of the stack.

## Operations on Stack:

The basic operations that are performed over a stack are as follows:

### Push:

To insert an item into the top of the stack. If the stack is full, so no item can be inserted into the stack, then such a condition is known as Overflow.

Algorithm:

1. Start
2. Check whether the stack is full.
3. If Full, then return.
4. Else, increment top.
5. Insert a new element at the position pointed by the top.
6. End

PseudoCode:

` `
`start if (stack== full) return(); else top++; stack[top]= value;endCopy code`

### Pop:

To remove an item from the top of the stack. So items are removed in reverse order as they were pushed. If the stack is empty, no items can be popped out of the stack, so such a condition is known as Underflow.

Algorithm:

1. Start
2. Check whether the stack is empty.
3. If empty, then return.
4. Else, access the topmost item.
5. Decrement the top
6. End

PseudoCode:

` `
`start if (stack== empty) return(); else t= top; top--; return(t);endCopy code`

### Peek:

Return the top of the stack without removing it.

Algorithm:

` `
`start return stack(top);EndCopy code`

### IsEmpty:

Checking whether the stack is empty or not.

Algorithm:

` `
`start if (top<1) return true; else return false;endCopy code`

### IsFull:

Checking whether the stack is full or not.

Algorithm:

` `
`start if (top==n) // n is the capacity of the stack return true; else return false;endCopy code`

## Working of Stack:

The working of basic operations (PUSH and POP) is as follows:

1. The TOP of the stack is a pointer that is used to keep track of the top item of the stack.
2. When a stack is initialized, Top’s value is -1, which means Top<1, hence the stack is empty.
3. Whenever an item is inserted Top’s value is incremented by 1, and the item is placed at the place where pointer Top is Pointing.
4. Whenever an item is Popped out of the stack, the value of that item is stored in some variable, and Top’s value is decremented by 1.
5. Before performing the PUSH operation, we always check whether the stack is full or not by using the isFull operation.
6. Before performing a POP operation, we always check whether the stack is empty or not by using the isEmpty operation.

## Types of Stack:

Mainly, stacks are of two types:

Register Stack: Register stack is commonly a memory element and can manage a small amount of data. Its height is always limited and less than the memory.

Memory Stack: A memory stack can manage a large size of data, the capacity of the memory stack is flexible, and can have large amounts of memory.

Must explore – 8 Most Important Data Structures Every Programmer Must Know

## Implementation of Stack:

The stack can be implemented using:

1. Array

Implementation with arrays is quite easy and fast, but arrays are limited in size. Whereas, implementation with a linked list has an overhead of allocating, linking, unlinking, and deallocating, linked lists are not limited in size.

### Implementation using Array:

The stack will be implemented using an array, and all the operations of the stack will be performed using an array.

Let’s understand this by an example, consider the following data item: 10, 20, 30, 40 Push them into the stack and Pop 40, 30 from the stack.

The stack will be implemented using a linked list, and all the operations of the stack will be performed using a linked list. While implementing a stack using a linked list, the top will point to every newly inserted element, and while removing any element top must point to that element

Let’s take the same example, consider the following data item: 10, 20, 30, 40 Push them into the stack and Pop 40, 30 from the stack.

## Applications of Stack:

The stack data structure has numerous applications, some of them are as follows:

1. It is used in Infix to Postfix or Infix to Prefix conversion.
2. It is used in Balancing symbols.
3. Forward and Backward feature of the Web browser.
4. Undo-Redo feature.
5. Used in algorithms like Tower of Hanoi.
6. Used in Backtracking
7. For String reversal
8. For memory management

## Time Complexity of Stack

The Time Complexity of different operations of the stack is as follows:

Push Operation:

• Best Case: O(1)
• Average Case: O(1)
• Worst Case: O(N), where N is the number of items in the stack
• Space Complexity: O(1)

Pop Operation:

• Best Case: O(1)
• Average Case: O(1)
• Worst Case: O(1)
• Space Complexity: O(1)

Peek Operation:

• Best Case: O(1)
• Average Case: O(1)
• Worst Case: O(1)
• Space Complexity: O(1)

isFull operation:

• Best Case: O(1)
• Average Case: O(1)
• Worst Case: O(1)
• Space Complexity: O(1)

isEmpty operation:

• Best Case: O(1)
• Average Case: O(1)
• Worst Case: O(1)
• Space Complexity: O(1)