Stacks, as you already know, are based on Last-In First-Out (LIFO) technique. The last element added in a stack is the first one to be deleted. Addition of an element at top or end of a stack is called Push operation. Deletion of an element from top or end of a stack is called Pop operation. Stacks are used in a number of applications. In this post we will understand two ways of implementing Python Stacks.
Using Python List as a Stack
List is the compound datatype predefined in Python. It has two relevant methods that allow you to implement a list as a Stack. These two methods are append() and pop(). Append() is used to add a new value at the end of the list. Pop() function removes and returns the last value stored in the list. You don’t need to maintain the TOP variable to represent top element of the stack in this implementation. Append and Pop functions automatically work on last added elements.
Here is the code to demonstrate how a Python List can be implemented as a Stack.
pyStack= option='Y' while (option=='Y' or option=='y'): pushpop=int(input("Enter 1 for push and 2 for pop")) if (pushpop==1): value=input("Enter the value to be pushed in stack") pyStack.append(value) elif (pushpop==2): print("The popped value is",pyStack.pop()) else: print("Wrong option") continue option=str(input("do you want to do more operations(Y/N)")) print("Stack is ",pyStack)
Using a User Defined Linear Linked List Class as Python Stacks
In previous post we learned how to create a linear linked list user defined class. Here we will create a stack by using the concept of Linear Linked List. This implementation is better than using Python List. For real-life problems you may need to declare a node as a class to define more than one data member and multiple member functions. Managing linked list created with node as stack is easier. You can have the TOP pointer to access the topmost element of the stack.
Push operation is implemented by adding elements at the beginning in the linked list implemented as a stack.
Pop operation is implemented by storing data part of TOP node in item variable and updating TOP with address of next element of stack. Pop operation returns the popped node’s data part.
Here is the Python Stack using Linear Linked user defined class.
#user defined class for nodes class Node: def __init__(self, data=None, link=None): self.data=data self.link=link def __str__(self): return str(self.data) #user defined class for Stack class Stack: #constructor for Stack Class def __init__(self, top=None,nodecount=0): self.top=top self.nodecount=nodecount #stores number of nodes in stack def push(self, value=None):#Push elements node=Node(value) node.link=self.top self.top=node self.nodecount=self.nodecount+1 def pops(self):#Pop elements item=self.top.data self.top=self.top.link self.nodecount=self.nodecount-1 #Update count of stack elements return item def printList(self):#traverse and display stack elements ptr=self.top while ptr: print(ptr) ptr=ptr.link print() #Create blank Stack stk=Stack() numele= input('How many elements to be added in the Stack??') #add a few elements to initialise stack for cnt in range(int(numele)): ele= input('Enter a value for Stack ') stk.push(ele) cnt=cnt+1 #Print Stack before pushing of popping elements print("Stack Initially") stk.printList() #As user whether to push an element or pop an element. option='Y' while (option=='Y' or option=='y'): pushpop=int(input("Enter 1 for push and 2 for pop")) if (pushpop==1): value=input("Enter the value to be pushed in stack") stk.push(value) elif (pushpop==2): print("The popped value is",stk.pops()) else: print("Wrong option") continue option=str(input("do you want to do more operations(Y/N)")) #Print Stack again after operations print("Stack Finally") stk.printList()