Skip to content

Commit c5732e4

Browse files
authored
Update Stack.md
1 parent c1c2808 commit c5732e4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Stack.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,27 @@ void Display()
141141
}
142142
}
143143
```
144+
145+
## Working of Stack
146+
Initially, we set a pointer Peek/Top to keep the track of the topmost item in the stack.
147+
148+
Initialize the stack to -1. Then, we check whether the stack is empty through the comparison of Peek to -1 i.e. Top == -1
149+
150+
As we add the elements to the stack, the position of the Peek element keeps updating every time.
151+
152+
As soon as we pop or delete an item from the set of inputs, the top-most element gets deleted and thus the value of Peek/Top gets reduced.
153+
154+
## Time Complexity Stack
155+
As mentioned above, only a single element can be accessed at a time in Stacks.
156+
157+
While performing push() and pop() operations on the stack, it takes O(1) time.
158+
159+
## Application of Stacks
160+
Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:
161+
162+
* **To reverse a word** - Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the letters in reverse order.
163+
* **In compilers** - Compilers use the stack to calculate the value of expressions like 2 + 4 / 5 * (7 - 9) by converting the expression to prefix or postfix form.
164+
* **In browsers** - The back button in a browser saves all the URLs you have visited previously in a stack. Each time you visit a new page, it is added on top of the stack. When you press the back button, the current URL is removed from the stack, and the previous URL is accessed.
165+
166+
## Conclusion
167+
I have added .c file of complete implementation of stack.

0 commit comments

Comments
 (0)