-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackLinkedList.py
65 lines (48 loc) · 1.5 KB
/
stackLinkedList.py
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
# Stack implementation using LinkedList
# Simply, the idea is to insert a node to the head of LinkedList
# each time we push an element to the Stack
# All Time and Space complexity O(1)
class Node:
def __init__(self, value = None):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def __iter__(self):
current_node = self.head
while current_node:
yield current_node
current_node = current_node.next
class Stack:
def __init__(self):
self.linked_list = LinkedList()
def __str__(self):
values = [str(v.value) for v in self.linked_list]
return '\n'.join(values)
def isEmpty(self):
if self.linked_list.head == None:
return True
else:
return False
# add node to the head/first
def push(self, value):
node = Node(value)
node.next = self.linked_list.head
self.linked_list.head = node
def peek(self):
if self.isEmpty():
return "Stack is empty"
else:
node_value = self.linked_list.head.value
return node_value
# get the head/first node
def pop(self):
if self.isEmpty():
return "Stack is empty"
else:
node_value = self.linked_list.head.value
self.linked_list.head = self.linked_list.head.next
return node_value
def delete(self):
self.linked_list.head = None