-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
74 lines (46 loc) · 1.32 KB
/
Stack.py
File metadata and controls
74 lines (46 loc) · 1.32 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Node:
def __init__(self, value):
self.data = value
self.next = None
class Stack:
def __init__(self):
self.top = None
self.n = 0
def __len__(self):
return self.n
def push(self, value):
new_node = Node(value)
new_node.next = self.top
self.top = new_node
self.n += 1
def isempty(self):
return self.top == None
def __str__(self):
curr = self.top
while curr != None:
print(curr.data)
curr = curr.next
return ''
def peak(self):
if self.top == None:
print("Stack is empty.")
return
else:
return self.top.data
def pop(self):
if self.top == None:
print("Stack is empty")
return
else:
self.top = self.top.next
self.n -= 1
if __name__ == "__main__":
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.pop()
print(stack.isempty())
print(stack.peak())
print(stack)