-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab assignment 2.py
245 lines (224 loc) · 6.19 KB
/
Lab assignment 2.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class Node:
def __init__(self, e, n):
self.element = e
self.next = n
class MyList:
def __init__(self, a):
self.head = None
tail = None
for i in a:
newNode = Node(i, None)
if self.head is None:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
self.tail = newNode
########################################################################
def showlist(self):
n = self.head
while(n is not None):
print(n.element, end=" ")
n = n.next
print()
def isEmpty(self):
if self.head is None:
return True
return False
def clear(self):
self.head = None
def countnode(self):
c = 0
n = self.head
while(n is not None):
c += 1
n = n.next
return c
def nodeAt(self, index):
size = self.countnode()
if (index < 0 or index >= size):
return None
n = self.head
i = 0
while(i < index):
n = n.next
i += 1
return n
def insert(self, newElement, index=0):
if index == 0:
n = self.head
pred = self.head
ok = 1
while n is not None:
if n.element == newElement.element:
print("key already exists and doesnot insert the key")
ok = 0
break
pred = n
n = n.next
if ok:
tail = pred
tail.next = newElement
newElement.next = None
tail = newElement
else:
size = self.countnode()
if(index < 0 or index >= size):
print("invalid index")
return
n = self.head
while(n is not None):
if(n.element == newElement):
print("key already exists and doesnot insert the key")
return
n = n.next
newnode = Node(newElement, None)
pred = self.nodeAt(index-1)
newnode.next = pred.next
pred.next = newnode
def remove(self, deletekey):
n = self.head
index = 0
removeNode = None
while(n is not None):
if(n.element == deletekey):
if(index == 0):
removeNode = self.head
head = head.next
else:
pred = self.nodeAt(index-1)
removeNode = pred.next
pred.next = removeNode.next
n = n.next
index += 1
return removeNode
##############################################################
def EvenChecker(self):
n = self.head
while n is not None:
if n.element % 2 == 0:
print(n.element, end=" ")
n = n.next
def search_ele(self, number):
n = self.head
while n is not None:
if n.element == number:
break
n = n.next
if n is None:
print("False")
else:
print("True")
def reverse(self):
prev = None
n = self.head
while n is not None:
tem = n.next
n.next = prev
prev = n
n = tem
self.head = prev
def find_elem(self, number):
n = self.head
while n is not None:
if n.element == number:
break
n = n.next
if n is None:
print("False")
else:
print("True")
def reverse(self):
prev = None
n = self.head
while n is not None:
tem = n.next
n.next = prev
prev = n
n = tem
self.head = prev
def sorting(self):
end = None
while end != self.head.next:
n = self.head
while n.next is not end:
m = n.next
if n.element > m.element:
n.element, m.element = m.element, n.element
n = n.next
end = n
def sumOfelements(self):
n = self.head
overall = 0
while n is not None:
overall += n.element
n = n.next
return (overall)
def rotate_byplace(self, side, k):
if side == "Right":
if k == None:
return
prev = None
prev2 = None
p = self.head
q = self.head
length = 0
c1 = 0
while q is not None:
prev = q
q = q.next
length += 1
q = prev
c = length-k
while p is not None and c1 < c:
prev2 = p
p = p.next
c1 += 1
p = prev2
q.next = self.head
self.head = p.next
p.next = None
elif side == "Left":
p = self.head
q = self.head
prev = None
count = 0
while p is not None and count < k:
prev = p
p = p.next
count += 1
p = prev
while q is not None:
prev = q
q = q.next
q = prev
q.next = self.head
self.head = p.next
p.next = None
#####################################################################
x = [1, 2, 3, 4, 5]
c = MyList(x)
c.showlist()
x = Node(20, None)
c.insert(x)
c.showlist()
c.insert(10, 2)
c.showlist()
x = Node(4, 10)
c.insert(x)
c.showlist()
c.remove(2)
c.showlist()
c.find_elem(7)
c.showlist()
c.sorting()
c.showlist()
c.reverse()
c.showlist()
c.EvenChecker()
c.showlist()
c.rotate_byplace("Left", 2)
c.showlist()
print(c.sumOfelements())
c.clear()
print(c.isEmpty())