Data Structures — Stack
Stack is a linear data structure. Which allows you to insert and retrieve the elements in LIFO(Last in first out) order.The element added at last will be retrieved first, you can also consider stack as CD Holders in which we add CD’s on top of each other and we can retrieve the CD starting from the top i.e the last CD inserted. Common operations on stack are Push, pop, peek.
Push — Insert an element in stack
Pop — Remove the top element
Get Lokesh’s stories in your inbox
Join Medium for free to get updates from this writer.
Peek — See the top element
Python Implementation
We are now going to implement the stack in python,
In the above code we created a class called stack and then in the init method we initialized an empty list in which we will be adding the elements. Then we created the methods push,pop,peek and getStack. Push will append the new element in the existing stack, Pop will remove the last element from the stack and peek will print the last element, getStack will be used to print the stack. After this we created an object for the stack class and performed all the operations.









