Stack Data Structure

 

Stack Data Structure


You will learn about the stack data structure and how it is implemented in Python, Java, and C/C++ in this course.




Stacks are linear data structures that adhere to the Last In First Out rule (LIFO). This implies that the last piece to be added to the stack gets eliminated first.


The stack data structure can be compared to a stack of plates stacked one on top of the other.

Stack Representation similar to a pile of plate


As you can see:

Stack a fresh dish on top.

Taking off the top plate

Additionally, you must first remove all of the plates on top if you want the plate at the bottom. Exactly like this, the stack data structure functions.


LIFO Principle of Stack

Programmers use the terms push and pop to describe the actions of adding and deleting items from stacks.

Stack push and pop operation


Even though item 3 was maintained last in the above image, it was removed first. The LIFO (Last In First Out) Principle operates in just this manner.

Any programming language, whether C, C++, Java, Python, or C#, can be used to implement a stack because the specification is essentially the same.


Basic operation of Stack


We may carry out various activities on a stack using a few fundamental operations.

  • Push: Place a component on top of a stack.
  • Pop: Remove an element from the top of a stack
  • isEmpty: Check if the stack is empty
  • isFull: Verify that the stack is full.
  • Peek: Gain access to the top element's value without removing it


Working of stack data structure


  • The top element in the stack is tracked by a pointer named TOP.
  • We initialise the stack with a value of -1 so that TOP == -1 can be used to determine whether the stack is empty.
  • When an element is pushed, TOP's value is increased and the new element is positioned in the area that TOP is pointing to.
  • When an element is popped, we return the element that TOP pointed to and lower its value.
  • We determine whether the stack is already full before pushing.
  • We determine whether the stack is already empty before bursting.

Working of stack data structure


Stack implementation in C++


// Stack implementation in C++ #include<iostream> using namespace std; #define MAX 10 int size = 0; // Creating a stack struct stack { int items[MAX]; int top; }; typedef struct stack st; void createEmptyStack(st *s) { s->top = -1; } // Check if the stack is full int isfull(st *s) { if (s->top == MAX - 1) return 1; else return 0; } // Check if the stack is empty int isempty(st *s) { if (s->top == -1) return 1; else return 0; } // Add elements into stack void push(st *s, int newitem) { if (isfull(s)) { cout << "STACK FULL"; } else { s->top++; s->items[s->top] = newitem; } size++; } // Remove element from stack void pop(st *s) { if (isempty(s)) { cout << "\n STACK EMPTY \n"; } else { cout << "Item popped= " << s->items[s->top]; s->top--; } size--; cout << endl; } // Print elements of stack void printStack(st *s) { printf("Stack: "); for (int i = 0; i < size; i++) { cout << s->items[i] << " "; } cout << endl; } // Driver code int main() { int ch; st *s = (st *)malloc(sizeof(st)); createEmptyStack(s); push(s, 1); push(s, 2); push(s, 3); push(s, 4); printStack(s); pop(s); cout << "\nAfter popping out\n"; printStack(s); }


Time Complexity


For the array-based implementation of a stack, the push and pop operations take constant time, i.e. O(1).



Application of stack data structure


Although stack is a straightforward data structure to use, it is incredibly effective. A stack is most frequently used for the following purposes:

  • To reserve a word - Stack all the letters together, then remove them. You'll receive the letters in reverse order due to the stack's LIFO sequence.
  • In browser - All of the URLs you've previously viewed are saved in a stack when you use the back button on a browser. A fresh page is added on top of the stack each time you view one. The previous URL is retrieved when you use the back button, which also removes the current URL from the stack.


Post a Comment

Previous Post Next Post