Skip to content

Commit 528af09

Browse files
committed
Day 15 - Stack implementation using LinkedList
1 parent 58e9cef commit 528af09

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

day_14_stack_using_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def __init__(self) -> None:
77
def push(self, data):
88
if (self.top == self.maxSize-1):
99
raise Exception("Stack overflow!")
10-
self.top += 1
10+
self.top += 1
1111
self.data[self.top] = data
1212

1313
def pop(self):

day_15_stack_using_linkedList.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Node:
2+
def __init__(self, data) -> None:
3+
self.data = data
4+
self.next = None
5+
6+
class Stack:
7+
def __init__(self) -> None:
8+
self.top = None
9+
10+
def push(self, data):
11+
node = Node(data)
12+
if (self.top == None):
13+
self.top = node
14+
return
15+
16+
node.next = self.top
17+
self.top = node
18+
19+
def pop(self):
20+
if (self.top == None):
21+
raise Exception('Stack is empty!!')
22+
23+
temp = self.top
24+
self.top = self.top.next
25+
return temp
26+
27+
def print(self):
28+
temp = self.top
29+
if (temp == None):
30+
print('Stack is Empty!!!!!!, Go ahead and do some push operation first......')
31+
return
32+
while (temp):
33+
print(temp.data, end= ' ')
34+
temp = temp.next
35+
print('\n')
36+
37+
38+
39+
if __name__ == "__main__":
40+
print('Hello Developers!!!')
41+
42+
stack = Stack()
43+
stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5);
44+
stack.print()
45+
stack.pop()
46+
stack.print()
47+
stack.pop(); stack.pop(); stack.pop(); stack.pop(); #stack.pop();
48+
stack.print()

0 commit comments

Comments
 (0)