First of all, we must aware with the Stack - the stack is a linear data structure that works on LIFO mechanism i.e. Last In First Out (that means Last inserted item will be removed (popped) first).
Thus, to implement a stack, basically we have to do two things:
- Inserting (PUSH) elements at the end of the list
- Removing (POP) elements from the end of the list
i.e. both operations should be done from one end.
In Python, we can implement a stack by using list methods as they have the capability to insert or remove/pop elements from the end of the list.
Method that will be used:
- append(x) : Appends x at the end of the list
- pop() : Removes last elements of the list
Program to use list stack
Output
need an explanation for this answer? contact us directly to get an explanation for this answer