How To Use Stack Data-Structure In Real-life

shivendra k jha
6 min readApr 3, 2021

--

Before diving into this blog, you should be of Role of Data-Structures-Algorithms For Software Engineer!. Let's start with this now.

We are software engineers and programmers as role holders for this, it is our responsibility to solve any kind of complicated problems with a programming approach, that is whatever we do in real life we want to computerize that thing in a generic form so that everybody can see it straightforwardly.

So let’s start with some real-world examples and then see how that uses the stack data structure.

Suppose you got a chance of working with Tesla company and they gave you a task to design an automated Car parking system where you have to fit more cars in less space i.e you need to utilize space efficiently.

What you will do?? Yes for sure in that situation stack car parking will be the best solution. As you can see in the above image car is parked in a stacked manner, there is a lift to get the first-row car up then again we got new space for parking so this way the car which kept, at last, will be left from its place first(LIFO). In some places and countries, the space is limited you may see the whole building where the car is parked this way, Check out this stack parking at its finest.

The first and foremost thing which comes to our mind after listening to the word stack is LIFO (Last-In-First-Out). As you saw above example car which will be parked last will take out first. Furthermore, these are some more examples for Stack:

  • Pile of trays.
  • Molded chairs.
  • The tennis balls in their container.
  • Stack of coins.
  • Computer algorithm (Undoing or backtracking an action).
  • Java compiler translates the program into postfix notation.
  • Java virtual machine uses the stack.
  • The sadder example of a stack is the slogan “Last hired, First fired” which is typically utilized when a company reduces its workforces.

So by keeping all the above examples in our mind we can make a definition of STACK?

Stack is a collection of elements that follows the Discipline Last-In-First-Out (LIFO), and basically, we store stack in these two physical data structures.

  1. Array
  2. Linked-List

When we say stack we imagine it as a vertical Array, Let’s see the array part. What are all the things required for implementing a stack using?

  • An array with a fixed size.
  • A TOP pointer with integer data-type, which will show the index of the stack. With help of the TOP pointer, we can perform all operations on the stack.

Structure of Stack:

class Stack {
private: // Structure for stack!
int size; // size of the stack
int top; // this will decide the situation of the stack
int* S; // integer type pointer
}

Insertion and Deletion can be performed from the TOP, (if top== -1) it means the stack is empty, and when (top==size-1) it shows stack is full.

#include <iostream>
using namespace std;
class Stack {
private: // Structure for stack!
int size; // size of the stack
int top; // this will decide the situation of the stack
int* S; // stack Pointer to create dynamically
public:
Stack(int size); // Creating a parametrized constructor by class name by passing the size of the stack
~Stack(); // Destructor to remove from memory
void push(int x); // delaclartion of push function i.e for inserting an element at the top of stack
int pop(); // declaration for the pop i.e deleting the topmost element
int peek(int index); // declaration of peek, knowing the element at particular index
int isFull(); // declaration of isfull, this will check either the stack is full or not
int isEmpty(); // declaration of isempty this will check either stack is empty or not
void display(); // declaration of Display for displaying the stack
int stackTop(); // declaration of the stacktop for checking the topmost element present in the stack
};
Stack::Stack(int size) { // this will create the size of the stack
this->size = size; // this is used bcz we have same name parameter so assigning this function size to the private class size
top = -1; // intially set top of the stack as -1;
S = new int[size]; // dynamically creating the size of the stack in heap memory
}
Stack::~Stack() {
delete[] S; // after using free the memory from heap
}
void Stack::push(int x) { // this function is for inserting element at the top of stack
if (isFull()) { // checking the condition if stack is full then we can't insert further any elements
cout << "Stack Overflow!" << endl; // so print a message that stack is full
}
else { // if stack is not full the
top++; // increment top
S[top] = x; // and push the element to stack
}
}
int Stack::pop() {// deleting the element from the stack
int x = 1; // initially setting x as one
if (isEmpty()) { // checking the stack either it is empty if empty there is no meaning for deletion of the element
cout << "Stack Underflow!" << endl;
}
else {
x = S[top]; // take out the element and then decrement the size of the stack
top--; // decreament the size of the stack
}
return x;
}
int Stack::peek(int index) { // taking out the element from a particular index
int x = -1;
if (top - index + 1 < 0 || top - index + 1 == size) { // 1st of all check either the stack index is valid or not; it should be greater then 0 and less the size
cout << "Invalid position!" << endl; // if is does not shows a proper index then print invalid index
}
else {
x = S[top - index + 1]; // and if the index is valid then take out the element
}
return x; // and return it
}
int Stack::isFull() { // checking either a stack is full
if (top == size - 1) { // if the top
return 1;
}
return 0;
}
int Stack::isEmpty() {
if (top == -1) {
return 1;
}
return 0;
}
void Stack::display() { // since stack is lifo consider it as verticle cane of ball and taking elements at time
for (int i = top; i >= 0; i--) {
cout << S[i] << " | " << flush;
}
cout << endl;
}
int Stack::stackTop() {
if (isEmpty()) {
return -1;
}
return S[top];
}
int main() { int A[] = { 1, 3, 5, 7, 9 };
// inside main function we will create a object of stack
Stack stk(sizeof(A) / sizeof(A[0]));
// Populate stack with array elements
for (int i = 0; i < sizeof(A) / sizeof(A[0]); i++) {
stk.push(A[i]);
}
stk.push(11);
cout << "Stack full: " << stk.isFull() << endl; // Display stack;
cout << "Stack: " << flush;
stk.display();
// Peek
cout << "Peek at 0th: " << stk.peek(0) << endl;
cout << "Peek at 3rd: " << stk.peek(3) << endl;
cout << "Peek at 10th: " << stk.peek(10) << endl;
// Top element
cout << "Top element: " << stk.stackTop() << endl;
// Pop out elements from stack
cout << "Popped out elements: " << flush;
for (int i = 0; i < sizeof(A) / sizeof(A[0]); i++) {
cout << stk.pop() << ", " << flush;
}
cout << endl;
stk.pop();
cout << "Stack empty: " << stk.isEmpty() << endl; return 0;
}

If any one wants to Learn DSA and from scratch you can visit my Github Repository

--

--

shivendra k jha
shivendra k jha

Written by shivendra k jha

I am a consistent learner and driven. I thrive on challenges and constantly set goals for myself. Technology always excites me.

No responses yet