implement stack using linked list in python

Python program for implement stack using linked list. Here problem description and explanation.

#  Python 3 program for
#  Implementation stack using linked list

#  Stack Node
class StackNode :
	def __init__(self, data, top) :
		self.data = data
		self.next = top
	

class MyStack :
	def __init__(self) :
		self.top = None
		self.count = 0
	
	#  Returns the number of element in stack
	def size(self) :
		return self.count
	
	def isEmpty(self) :
		if (self.size() > 0) :
			return False
		else :
			return True
		
	
	#  Add a new element in stack
	def push(self, data) :
		#  Make a new stack node
		#  And set as top
		self.top = StackNode(data, self.top)
		#  Increase node value
		self.count += 1
	
	#  Add a top element in stack
	def pop(self) :
		temp = 0
		if (self.isEmpty() == False) :
			#  Get remove top value
			temp = self.top.data
			self.top = self.top.next
			#  Reduce size
			self.count -= 1
		
		return temp
	
	#  Used to get top element of stack
	def peek(self) :
		if (not self.isEmpty()) :
			return self.top.data
		else :
			return 0
		
def main() :
	#  Create new stack 
	s = MyStack()
	print("\n Is empty : ", s.isEmpty())
	#  Add element
	s.push(15)
	s.push(14)
	s.push(31)
	s.push(21)
	s.push(10)
	print("\n Top  : ", s.peek())
	print(" Size : ", s.size())
	print("\n Is empty : ", s.isEmpty())
	#  Delete Stack Element
	data = s.pop()
	print("\n Pop element ", data)
	print(" Size : ", s.size())
	data = s.pop()
	print("\n Pop element ", data)
	print(" Size : ", s.size())

if __name__ == "__main__": main()

Output

 Is empty :  True

 Top  :  10
 Size :  5

 Is empty :  False

 Pop element  10
 Size :  4

 Pop element  21
 Size :  3


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved