2. Stack using Linked List
In [1]:
Copied!
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return "<Node: %s>" % self.data
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return "" % self.data
In [2]:
Copied!
Node(10)
Node(10)
Out[2]:
<Node: 10>
In [3]:
Copied!
class StackUsingLinkedList:
def __init__(self):
self.__top = -1
self.head = None
def __repr__(self):
current = self.head
nodes = []
while current != None:
if current == self.head:
nodes.append("[Top: %s]" % current.data)
else:
nodes.append("[%s]" % current.data)
current = current.next
return ", ".join(nodes)
def __iter__(self):
current = self.head
while current:
yield current.data
current = current.next
def __len__(self):
return self.__top + 1
def is_empty(self):
return self.head == None and self.__top == -1
def peek(self):
if self.is_empty():
print("Stack is empty, nothing to peek()")
else:
print("Element at top is: {}".format(self.head))
# return "<Element: %s>" % self.head.data
return self.head
def push(self, data):
node = Node(data)
if self.head == None:
print("Inserting first element: {}".format(data))
self.head = node
else:
print("Inserting another element: {}".format(data))
node.next = self.head
self.head = node
self.__top += 1
def pop(self):
if self.__top == -1 or self.head == None:
print("Stack is empty, nothing to pop()")
return
current = self.head
if current.next == None:
print("at last element")
self.head = None
else:
self.head = current.next
data = current.data
del current
self.__top -= 1
return data
class StackUsingLinkedList:
def __init__(self):
self.__top = -1
self.head = None
def __repr__(self):
current = self.head
nodes = []
while current != None:
if current == self.head:
nodes.append("[Top: %s]" % current.data)
else:
nodes.append("[%s]" % current.data)
current = current.next
return ", ".join(nodes)
def __iter__(self):
current = self.head
while current:
yield current.data
current = current.next
def __len__(self):
return self.__top + 1
def is_empty(self):
return self.head == None and self.__top == -1
def peek(self):
if self.is_empty():
print("Stack is empty, nothing to peek()")
else:
print("Element at top is: {}".format(self.head))
# return "" % self.head.data
return self.head
def push(self, data):
node = Node(data)
if self.head == None:
print("Inserting first element: {}".format(data))
self.head = node
else:
print("Inserting another element: {}".format(data))
node.next = self.head
self.head = node
self.__top += 1
def pop(self):
if self.__top == -1 or self.head == None:
print("Stack is empty, nothing to pop()")
return
current = self.head
if current.next == None:
print("at last element")
self.head = None
else:
self.head = current.next
data = current.data
del current
self.__top -= 1
return data
In [4]:
Copied!
stack = StackUsingLinkedList()
stack = StackUsingLinkedList()
In [5]:
Copied!
for i in range(0, 6):
stack.push(i)
for i in range(0, 6):
stack.push(i)
Inserting first element: 0 Inserting another element: 1 Inserting another element: 2 Inserting another element: 3 Inserting another element: 4 Inserting another element: 5
In [6]:
Copied!
stack
stack
Out[6]:
[Top: 5], [4], [3], [2], [1], [0]
In [7]:
Copied!
len(stack)
len(stack)
Out[7]:
6
In [8]:
Copied!
for i in stack:
print(i)
for i in stack:
print(i)
5 4 3 2 1 0
In [9]:
Copied!
stack.is_empty()
stack.is_empty()
Out[9]:
False
In [10]:
Copied!
stack.peek()
stack.peek()
Element at top is: <Node: 5>
Out[10]:
<Node: 5>
In [11]:
Copied!
for _ in range(0, 7):
stack.pop()
for _ in range(0, 7):
stack.pop()
at last element Stack is empty, nothing to pop()
In [12]:
Copied!
stack
stack
Out[12]:
In [13]:
Copied!
stack.pop()
stack.pop()
Stack is empty, nothing to pop()
In [ ]:
Copied!