INTRODUCTION A stackis a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop. Instead of push(), append() is used to add elements to the top of the stack while pop() removes the element in LIFO order.
4.
The functions associatedwith stack are: empty() – Returns whether the stack is empty – Time Complexity: O(1) size() – Returns the size of the stack – Time Complexity: O(1) top() / peek() – Returns a reference to the topmost element of the stack – Time Complexity: O(1) push(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity: O(1) pop() – Deletes the topmost element of the stack – Time Complexity: O(1)
IMPLEMENTATION There arevarious ways from which a stack can be implemented in Python. Stack in Python can be implemented using the following ways: list Collections.deque queue.LifoQueue
7.
stack =[] # Push elements stack.append(10) stack.append(20) stack.append(30) # Pop elements print(stack.pop()) # Output: 30 print(stack.pop()) # Output: 20 # Stack after popping print(stack) # Output: [10]