Unlike an array structure, which allows random access at all the positions, a stack limits the inserting and removing operation to only one side of the data sequence. A stack follows the last in, first out (LIFO) principle.
In Python, stack can be implemented using a list. To follow the LIFO principle, inserting and removing operations both occur at the tail of the list.Python implementation of a stack
pop
in a stack is taking out the top item in the stack.
Stacks are used extensively in a lot of places.
Compilers and Parsers – Expression evaluation is done by stacks by postfix or prefix using stacks in compilers.
Activation Records – An activation record is data that keeps track of the procedure activities during the runtime of a program.
When the function is called, an activation record is created for it and keeps track of parameters and information like local variables, return address, static and dynamic links, and the return value.
This activation record is the fundamental part of programming languages and is implemented using a stack.
Web Browsers – Web Browsers use a stack to keep track of URLs that you have visited previously. When you visit a new page, it is added to the stack and when you hit the back button, the stack is popped and the previous URL is accessed.
To implement other Data Structures – Stacks are used to implement searches in Graphs and Trees, which are other complex data structures.
Stack Methods
There are various functions that are associated with a stack. They are,
stack.isEmpty()
The stack.isEmpty()
method returns True
if the stack is empty. Else, returns False
Time Complexity - O(1)
stack.length()
The stack.length()
method returns the length of the stack.
Time Complexity - O(1)
stack.top()
The stack.top()
method returns a pointer/reference to the top element in the stack.
Time Complexity - O(1)
stack.push(x)
The stack.push()
method inserts the element, x
to the top of the stack.
Time Complexity - O(1)
stack.pop()
The stack.pop()
method removes the top element of the stack and returns it.
Time Complexity - O(1)
In Python, we can implement the stack by various methods. We are going to dive into two of the methods - the common method and the efficient method.
We use the list methods append
and pop
to implement a Stack.
Python collections
are container classes that are used for data collection storage. They are highly optimized, are really fast, and have lots of methods built-in.
Deque
is one such python collection that is used for inserting and removing items. We can use it to create a faster implementation of a stack.
Once you are done with understanding the stack and the basic implementation, practice the following problems and problem-sets in order to get a strong grasp on stacks.
Infix to Postfix - GeeksForGeeks
Next Greater Element - GeeksForGeeks
Postfix to Prefix - GeeksForGeeks
Reverse a String using Stack - GeeksForGeeks
Mini Parser - LeetCode
Simplify Path - LeetCode
More Stack Problems - LeetCode
Stack Problem Set - HackerRank
We have learned the implementation, importance, and application of stacks. This is one of the most important data structures to know and it is extensively asked in the computer science industry. It is important to have strong knowledge on this topic as it would give you an edge.
push
in a stack is putting an item on top of the stack.
A great analogy we can use is stacking a pile of books. We always keep a new book on top and remove the topmost book. Stacks are similar to queues in that they are linear collections of items, but they differ in the order in which they are accessed. Stacks are used in a variety of areas from Operating System Software, in Compilers and Language Parsing, and to implement other complex Data Structures like Trees and Graphs.