-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_ll.py
More file actions
executable file
·36 lines (28 loc) · 957 Bytes
/
Copy pathstack_ll.py
File metadata and controls
executable file
·36 lines (28 loc) · 957 Bytes
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
class Node:
def __init__(self,value):
self.value = value
self.next = None
class Stack:
def __init__(self):
self.head = None
self.num_elements = 0
def push(self, value):
new_node = Node(value)
# if stack is empty
if self.head is None:
self.head = new_node
else:
new_node.next = self.head # place the new node at the head (top) of the linked list
self.head = new_node
self.num_elements += 1
def pop(self):
if self.is_empty():
return
value = self.head.value # copy data to a local variable
self.head = self.head.next # move head pointer to point to next node (top is removed by doing so)
self.num_elements -= 1
return value
def size(self):
return self.num_elements
def is_empty(self):
return self.num_elements == 0