-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_and_stack.py
More file actions
107 lines (91 loc) · 2.69 KB
/
Copy pathlist_and_stack.py
File metadata and controls
107 lines (91 loc) · 2.69 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class doubly_linked_list:
def __init__(self):
self.head = None
def __iter__(self):
current = self.head
while current is not None:
yield current
current = current.next
def __reversed__(self):
current = self.head
while current.next is not None:
current = current.next
while current is not None:
yield current
current = current.prev
def push(self, new_val):
new_node = Node(new_val)
new_node.next = self.head
if self.head is not None:
self.head.prev = new_node
self.head = new_node
def append(self, new_val):
new_node = Node(new_val)
new_node.next = None
if self.head is None:
new_node.prev = None
self.head = new_node
return
last = self.head
while last.next is not None:
last = last.next
last.next = new_node
new_node.prev = last
return
def listprint(self, node):
while node is not None:
print(node.data),
last = node
node = node.next
def remove_node(self, removed_node):
head_val = self.head
if head_val is not None:
if head_val.data == removed_node:
self.head = head_val.next
head_val = None
return
while head_val is not None:
if head_val.data == removed_node:
break
prev = head_val
head_val = head_val.next
if head_val is None:
return
prev.next = head_val.next
head_val.next.prev = head_val.prev
head_val = None
class stack:
def __init__(self):
self.head = None
def push(self, new_data):
if self.head is None:
self.head = Node(new_data)
else:
new_node = Node(new_data)
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
def pop(self):
if self.head is None:
return None
elif self.head.next is None:
temp_data = self.head.data
self.head = None
return temp_data
else:
temp_data = self.head.data
self.head = self.head.next
self.head.prev = None
return temp_data
def top(self):
return self.head.data
def isEmpty(self):
if self.head is None:
return True
else:
return False