the art of
Algorithm
Notes on Analysis and Design



Stack using Linked List

Implementing stack using Linked List in python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Node:
	def __init__(self,data):
		self.data=data
		self.next=None
class Stack:
	def __init__(self):
		self.root=None
	def push(self,data):
		newnode=self.Node(data)
		newnode.next=self.root
		self.root=newnode
	def pop(self):
		if (self.isempty()):
			return "-1"
		temp=self.root
		self.root=self.root.next
		popped=temp.data
		return popped
	def isempty(self):
		return True if self.root is None else False
#driver program to test above function
stack=Stack()
stack.push(10)
stack.push(20)
stack.push(30)
print stack.pop()