Understanding Stack Data Structure: Concepts, Examples, and Real-World Applications

Understanding Stack Data Structure: Concepts, Examples, and Real-World Applications

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.


Pictorial Representation of Stack

Examples

  • Considering the pile of book, to access the first book, you have to firstly remove the last book, which is only the book you can access, after you remove it, you can now access second last book, which is now last book, and doing the same thing in loop, you can reach to first book.
  • Similarly, consider the stack of coins in vertical layout, we can see the coin which is at the top of the stack, means which we put at the last of all coins, and to see next coin, you have to first remove last one.

Implementation

We can implement stack in 2 ways :

  • One is using arrays ( Static and dynamic ), but here the size will be defined and thus fixed, so you can't insert the values more than the size specified during initialization.
  • Second way is to use LinkedLists, as I already wrote an article on LinkedLists , and we know that here the size doesn't matter and we can insert as much as we can until the memory of compuetr exhausted.

Here, we will discuss static method, using arrays. The operations we mostly use are pop, top, IsEmpty, IsFull and push.


Push Operation

  • Check, the stack reaches its space limit or not, means stack is full or not.
  • If yes, no operation will perform
  • Else, increment your top variable by one step.
  • Add the new element in your stack at top (as an index).


Pop Operation

  • Check the stack has some values or not. Means if the stack is not empty.
  • If stack is empty, no operation will executed.
  • else, get the element present on the index equals to top.
  • Then decrement the top variable by one.


Push, Pop and classes structure and implementation

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.


IsFull, IsEmpty and top functions

Practical Applications of Stack

Here are two practical applications of stacks:

  1. Text Editing (Undo Feature): When typing, each action (such as adding or deleting text) is pushed onto a stack. The undo feature uses this stack to revert the most recent action, following the Last In, First Out (LIFO) principle, ensuring the last operation is undone first.
  2. Web Browsing (Back Button): Browsers use stacks to manage page history. Each visited webpage is pushed onto a stack. When the user clicks the back button, the most recent page (the last one visited) is popped from the stack, taking the user to the previous page.

GitHub Repository

Differeent LeetCode Questions : Visit this

Stack Implementation in C++ (Both With Array & LinkedLists) : Visit this

要查看或添加评论,请登录

Ahmad Raza的更多文章

社区洞察

其他会员也浏览了