forked from amespi22/code_rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
executable file
·98 lines (88 loc) · 2.34 KB
/
linked_list.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
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
#! /usr/bin/env python
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextnode = None
self.prevnode = None
class ll:
def __init__(self):
self.headnode = None
self.tailnode = None
self.length = 0
def push(self, val):
self.length += 1
if self.headnode == None:
nn = Node(val)
self.headnode = nn
self.tailnode = nn
else:
nn = Node(val)
tailnode = self.tailnode
tailnode.nextnode = nn
nn.prevnode = tailnode
self.tailnode = nn
def push_list(self, val):
for v in val:
self.push(v)
def dequeue(self):
hn = self.headnode
if hn == None:
return None
else:
self.length -= 1
self.headnode = hn.nextnode
if hn.nextnode != None:
hn.nextnode.prevnode = None
else:
self.tailnode = None
return hn.dataval
def print(self):
cn = self.headnode
if cn == None:
return
while cn.nextnode != None:
print(cn.dataval)
cn = cn.nextnode
print(cn.dataval)
def main():
linked_list = ll()
linked_list.push(4)
linked_list.push("5")
linked_list.push("this34")
print(linked_list.length)
print("linked list values")
linked_list.print()
node = linked_list.dequeue()
linked_list.push("this")
print(f"Removed {node.dataval}")
print("new linked list")
linked_list.print()
node = linked_list.dequeue()
linked_list.print()
node = linked_list.dequeue()
linked_list.print()
node = linked_list.dequeue()
linked_list.print()
print(linked_list.headnode)
print(linked_list.tailnode)
print(linked_list.length)
"""
#get all types of ctx
#use to find functions or conditional statements
def find_ctx(tree, ctx):
q = linked_list.ll()
#get first set of children
q.push_list(list(tree.getChildren()))
#go through all children breadth first
r = []
while q.length > 0:
e = q.dequeue()
t = type(e)
if str(t) == ctx:
r.append(e)
if e.getChildCount() != 0:
q.push_list(list(e.getChildren()))
return r
"""
if __name__ == "__main__":
main()