-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_2.py
More file actions
142 lines (90 loc) · 2.42 KB
/
05_2.py
File metadata and controls
142 lines (90 loc) · 2.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'''
Haris Khan
Linked-List
Traversing Forward Nodes
'''
class Node:
# Initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
#Initialize the linked list object
def __init__(self):
self.head = None
def contentlist(self):
tmp = self.head
while(tmp):
print(tmp.data)
tmp = tmp.next
if __name__ == "__main__":
l_list = LinkedList()
l_list.head = Node(1)
second = Node(2)
third = Node(3)
l_list.head.next = second
second.next = third
l_list.contentlist()
'''
Stack Data Structure
LIFO / FIFO
Last In First Out / First In First Out
'''
stk = []
stk.append('1')
stk.append('2')
stk.append('3')
# ['1', '2', '3']
stk.pop() # '3' goes out first
stk.pop() # '2' goes out next
stk.pop() # '1' goes out last
print(stk)
'''
Queue --> First In - First Out (FIFO)
Similar to Stack DS
'''
queue = []
queue.append('g')
queue.append('f')
queue.append('g')
print(queue)
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print(queue)
'''
Priority Queue: Queue with an importance of value i.e.
one is a higher priority then the other that exits first
If 2 elements have the same priority level, then they are dequeued based on their order of entrance.
'''
class PriorityQ(object):
def __init__(self):
self.queue = []
def __str__(self):
return ' '.join([str(i) for i in self.queue])
def empty(self):
return len(self.queue) == 0
def insert(self, data):
self.queue.append(data)
def delete(self):
try:
max = 0
for i in range(len(self.queue)):
if self.queue[i] > self.queue[max]:
max = i
item = self.queue[max]
del self.queue[max]
return item
except IndexError as e:
print("Check error: " + e)
exit()
if __name__ == "__main__":
myQueue = PriorityQ()
myQueue.insert(10)
myQueue.insert(0)
myQueue.insert(14)
myQueue.insert(7)
print(myQueue)
while not myQueue.empty():
print(myQueue.delete())