Introduction to Stacks in Data Structures

Introduction of Stacks in DSA

Stack in Data Structures

Stack is one of the basic linear Data structure, that we use for storing our data. Data in a stack is stored in a serialized manner. One important thing about using a Stack is that the data first entered in the stack will be at the last of the stack. This is one of the reason why we also called Stack a LIFO Data Structure, i.e; Last in First Out. We’ll be discussing more about this feature later in this article.

Basic Terminology of Stack

Before we move on further with understanding the Stack Data Structure, we need to learn about the basic terminology that is associated with this data structure so that understanding stack will be a little easy for us.

The basic terminology that we will be using in Stack is :

  • Top – This refers to the topmost element of the stack, or in other words the element last entered in the stack.
  • Push () – This is one of the operation that we can perform on stack, for inserting data in this data structure.
  • Pop () – This operation deals with deleting the data from the stack. It deletes the top-most data from the stack.
  • Peek () – This operation helps us in looking at the topmost element of the data without removing it from the stack.

A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space for push operation, the stack is then considered to be in an overflow state.

Below is simple representation of a stack with push and pop operations –

Operations on stacks

Time Complexity
For Stack

Best

O(1)

Average

O(n)

Worst

O(n)

Space Complexity

O(1)

Average Comparisons

(n+1)/2

Applications of Stacks in Data Structures

There a lot of places where we use stack without even realizing it. Let’s see some of the most common uses of stack data structure.

Real life examples of Stack

  1. Stacking up dishes after washing them.
  2. Deck of cards
  3. Stacking a pile of boxes in our store rooms.
  4. Libraries stack pile of books and articles.

Some Coding/Programming Applications of Stack

  1. Recursion
  2. Backtrackking
  3. Memory managament
  4. Process Managament

Advantages of Using Stack over other data structures

  • Manages the data in a Last In First Out(LIFO) method which is not possible with Linked list and array.
  • When a function is called the local variables are stored in a stack, and it is automatically destroyed once returned.
  • A stack is used when a variable is not used outside that function.
  • It allows you to control how memory is allocated and deallocated.
  • Stack automatically cleans up the object.
  • Not easily corrupted
  • Variables cannot be resized.

Disadvantages of Using Stack over other data structures

  • Stack memory is very limited.
  • Creating too many objects on the stack can increase the risk of stack overflow.
  • Random access is not possible.
  • Variable storage will be overwritten, which sometimes leads to undefined behavior of the function or program.
  • The stack will fall outside of the memory area, which might lead to an abnormal termination.

Learn more about Stacks

Here we have already gained the basic information on stacks , click on the below button to learn about more operations that can be performed on stack