Understanding Stack Data Structure: Concepts, Examples, and Real-World Applications
Ahmad Raza
Undergrad SoftwareEngineer | CGPA 3.83/4.00 | Global Nominee @NASA?? | 15X Inte'l Hackathons & Hackathon Finalist | LeetCode 350+ | 1X Hackathon Mentor | IELTS & AI Instructor | MERN Stack & Gen AI developer | C++ Python
Today, w'll discuss about stack. Basically, stack is a Linear Data Structure and follows the principle of LIFO (Last in First Out) or you can say FILO (First in Last Out), both are same to say. Now, what does this mean, it means that when we insert any element in stack, it appends that element on its top, and continues doing this, until the memory ends the or space by which we initialize it, ends. And when we want to reterieve an element, it gives us the last inserted element first and then give the second last element inserted and continues this sequence until the stack becomes empty. So, we are getting the element which we inserted first, at the last, and which we inserted last, at the first.
Lecture On Stack Data Structure : You can view my lecture on Stack by Visiting this.
Examples
Implementation
We can implement stack in 2 ways :
Here, we will discuss static method, using arrays. The operations we mostly use are pop, top, IsEmpty, IsFull and push.
Push Operation
领英推荐
Pop Operation
Top Function:
Here, no need to decrement you top variable, just use index property to get the element present at the index == top.
Similarly, you can make isEmpty and isFull functions. Below I am showing the diagram of these functions, take help fom that.
Practical Applications of Stack
Here are two practical applications of stacks:
GitHub Repository
Differeent LeetCode Questions : Visit this
Stack Implementation in C++ (Both With Array & LinkedLists) : Visit this