-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.py
169 lines (143 loc) · 4.02 KB
/
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class node:
def __init__(self,data,nexts = None):
self.data = data
self.nexts = nexts
def __str__(self):
return str(self.data)
class List:
def __init__(self,data = None):
self.head = self.tail = node(data)
self.size = 0 if data == None else 1
def __str__(self):
for i in range(self.size):
print(self.get(i),end=' ')
return ''
def isEmpty(self):
return self.size == 0
def append(self,data):
p = node(data)
if self.head.data == None:
self.head = p
self.tail = self.head
self.tail.nexts = p
self.tail = p
self.size += 1
def appendRecursion(self,dataList):
if len(dataList) == 1:
self.append(dataList.pop(0))
else:
self.append(dataList.pop(0))
self.appendRecursion(dataList)
def addHead(self,data):
p = node(data,self.head)
self.head = p
self.size +=1
def isIn(self,data):
t = self.head
while t.nexts != None:
if t.data == data:
return True
t = t.nexts
if t.data == data:
return True
return False
def before(self,data):
before = None
i = self.head
while i.nexts != None:
if i.data == data:
return before
before = i
i = i.nexts
if i.data == data:
return before
return None
def remove(self,index):
currentNode = self.get(index-1)
currentNode.nexts= self.get(index+1)
def removeTail(self):
t = self.before(self.tail.data)
t.nexts = None
self.tail = t
def removeHead(self):
self.head = self.head.nexts
def get(self,index):
if index > self.size:
raise IndexError
t = self.head
for i in range(index):
t = t.nexts
if t != None:
return t
def insert(self,node,index):
currentNode = self.get(index)
nextNode = currentNode.nexts
currentNode.nexts = node
node.nexts = nextNode
class CircularList:
def __init__(self,data = None):
self.head = self.tail = node(data)
self.size = 0 if data == None else 1
def __str__(self):
p = self.head
while p.nexts != None:
print(p.data,end=' ')
p = p.nexts
print(p.data)
return ''
def isEmpty(self):
return self.size == 0
def append(self,data):
p = node(data)
if self.head.data == None:
self.head = p
self.tail = self.head
self.tail.nexts = p
self.tail = p
self.size += 1
def addHead(self,data):
p = node(data,self.head)
self.head = p
self.size +=1
def isIn(self,data):
t = self.head
while t.nexts != None:
if t.data == data:
return True
t = t.nexts
if t.data == data:
return True
return False
def before(self,data):
before = None
i = self.head
while i.nexts != None:
if i.data == data:
return before
before = i
i = i.nexts
if i.data == data:
return before
return None
def remove(self,index):
currentNode = self.get(index-1)
currentNode.nexts= self.get(index+1)
def removeTail(self):
t = self.before(self.tail.data)
t.nexts = None
self.tail = t
def removeHead(self):
self.head = self.head.nexts
def get(self,index):
if index >= self.size:
index = index % self.size
t = self.head
for i in range(index):
t = t.nexts
if t != None:
return t
def insert(self,node,index):
currentNode = self.get(index)
nextNode = currentNode.nexts
currentNode.nexts = node
node.nexts = nextNode