-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha04.py
201 lines (149 loc) · 4.32 KB
/
a04.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class Node:
def __init__(self, val=None):
self.val = val
self.next = None
class Ring:
def __init__(self):
self.head = None
def __str__(self):
ret = '['
temp = self.head
while temp:
ret += str(temp.val) + ', '
temp = temp.next
if temp == self.head:
break
ret = ret.rstrip(', ')
ret += ']'
return ret
def _get_last(self):
if self.head is None:
return
temp = self.head.next
while temp.next != self.head:
temp = temp.next
return temp
def insert(self, index, val):
new_node = Node(val)
last = self._get_last()
if self.head is None:
self.head = new_node
new_node.next = self.head
return
if index == 0:
new_node.next = self.head
self.head = new_node
last.next = self.head
return
temp = self.head
counter = 0
while counter < index:
prev = temp
temp = temp.next
counter += 1
if new_node.next == self.head:
self.head = new_node
else:
new_node.next = temp
prev.next = new_node
def remove(self,val):
if self.head is None:
return
last = self._get_last()
temp = self.head
if temp.val == val:
if last == self.head:
self.head = None
else:
temp = temp.next
self.head = temp
last.next = self.head
return
prev = temp
temp = temp.next
while temp != self.head:
if temp.val == val:
break
prev = temp
temp = temp.next
if temp == self.head:
return
prev.next = temp.next
def remove_at(self, index):
if self.head is None:
return
last = self._get_last()
temp = self.head
if index == 0:
if last == self.head:
self.head = None
else:
temp = temp.next
self.head = temp
last.next = self.head
return
temp = self.head
count = 0
while count < index:
prev = temp
temp = temp.next
count += 1
if temp == self.head:
prev.next = temp.next
self.head = temp.next
prev.next = temp.next
def len(self):
last = self._get_last()
if self.head is None:
return 0
temp = self.head
count = 0
while temp is not None:
temp = temp.next
count += 1
if temp == self.head:
break
return count
def get(self, index):
if self.head is None:
raise IndexError('List is empty')
if index == 0:
return self.head.val
temp = self.head
count = 0
while count < index:
temp = temp.next
count += 1
return temp.val
def push(self,val):
if self.head is None:
self.insert(0,val)
return
else:
length = self.len()
count = 0
temp = self.head
while count < length:
temp = temp.next
count += 1
self.insert(count,val)
def pop(self):
if self.head is None:
return
else:
length = self.len()
temp = self.head
count = 0
while count < length:
temp = temp.next
count += 1
print(self.get(length-1))
self.remove_at(count-1)
if __name__ == '__main__':
r = Ring()
r.insert(1, 1)
r.insert(0, 1)
r.insert(0, 2)
r.insert(1, 3)
r.insert(7, 5) # different behavrior since it's a ring!
print(r)