-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_queue.py
More file actions
66 lines (50 loc) · 1.55 KB
/
Copy pathstack_queue.py
File metadata and controls
66 lines (50 loc) · 1.55 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
class Stack():
def __init__(self, head=None):
self.head = head
def push(self, data):
self.head = StackNode(data=data, next_node=self.head)
def pop(self):
node = self.head
self.head = node.next_node
return node.data
def print_stack(self):
current_node = self.head
arr = []
while current_node:
arr.append(current_node.data)
current_node = current_node.next_node
return arr
class StackNode():
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node
class Queue():
def __init__(self, head=None):
self.head = head
self.tail = self.head
def push(self, data=None):
new_node = QueueNode(data=data, next_node=self.head, prev_node=None)
if self.head:
self.head.prev_node = new_node
else:
self.tail = new_node
self.head = new_node
def pop(self):
if self.tail:
data = self.tail.data
self.tail.prev_node.next_node = None
self.tail = self.tail.prev_node
return data
return None
def print_queue(self):
current_node = self.head
arr = []
while current_node:
arr.append(current_node.data)
current_node = current_node.next_node
return arr
class QueueNode():
def __init__(self, data=None, next_node=None, prev_node=None):
self.data = data
self.next_node = next_node
self.prev_node = prev_node