Skip to content

Commit f3647b5

Browse files
committed
Create stack_using_linkedlist.py
1 parent 862e263 commit f3647b5

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

stack/stack_using_linkedlist.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Node:
2+
def __int__(self, value=None):
3+
self.value = value
4+
self.next = next
5+
6+
7+
class LinkedList:
8+
def __init__(self):
9+
self.head = None
10+
11+
def __iter__(self):
12+
currnode = self.head
13+
while currnode:
14+
yield currnode
15+
currnode = currnode.next
16+
17+
18+
class Stack:
19+
def __init__(self):
20+
self.LinkedList = LinkedList()
21+
22+
def __str__(self):
23+
values = [str(x.value) for x in self.LinkedList]
24+
return '\n'.join(values)
25+
26+
def isEmpty(self):
27+
if self.LinkedList.head is None:
28+
return True
29+
else:
30+
return False
31+
32+
def push(self, value):
33+
node = Node()
34+
node.value = value
35+
node.next = self.LinkedList.head
36+
self.LinkedList.head = node
37+
38+
def pop(self):
39+
if self.isEmpty():
40+
print("Stack underflow")
41+
else:
42+
node = self.LinkedList.head.value
43+
self.LinkedList.head = self.LinkedList.head.next
44+
return node
45+
46+
def peak(self):
47+
if self.isEmpty():
48+
print("Stack underflow")
49+
else:
50+
node = self.LinkedList.head.value
51+
return node
52+
53+
def delete(self):
54+
self.LinkedList.head = None
55+
56+
57+
customStack = Stack()
58+
print(customStack.isEmpty())
59+
customStack.push(1)
60+
customStack.push(2)
61+
customStack.push(3)
62+
print(customStack)
63+
print("_________")
64+
print(customStack.pop())
65+
print("_________")
66+
print(customStack.peak())

0 commit comments

Comments
 (0)