-
Notifications
You must be signed in to change notification settings - Fork 1
/
clinkedlist.py
117 lines (104 loc) · 3.29 KB
/
clinkedlist.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
class Node:
def __init__(self, dataval):
self.dataval = dataval
self.nextval = None
self.preval = None
class CLinkedList:
def __init__(self):
self.headval = None
def AtBeginning(self, newdata):
if self.headval == None:
NewNode = Node(newdata)
self.headval = NewNode
else:
NewNode = Node(newdata)
temp = self.headval
while temp.nextval is not None:
if temp.nextval == self.headval:
break
temp = temp.nextval
NewNode.preval = temp
NewNode.nextval = self.headval
self.headval.preval = NewNode
self.headval = NewNode
temp.nextval = self.headval
def AtEnd(self, newdata):
NewNode = Node(newdata)
temp = self.headval
while temp.nextval is not None:
if temp.nextval == self.headval:
break
temp = temp.nextval
temp.nextval = NewNode
NewNode.preval = temp
NewNode.nextval = self.headval
self.headval.preval = NewNode
def InBetween(self, middle_node, newdata):
temp = self.headval
while temp is not None:
if temp.dataval == middle_node:
break
temp = temp.nextval
else:
print("Could not find node")
return
NewNode = Node(newdata)
NewNode.nextval = temp.nextval
NewNode.preval = temp
temp.nextval = NewNode
def removeStart(self):
temp = self.headval
while temp.nextval is not None:
if temp.nextval == self.headval:
break
temp = temp.nextval
temp.nextval = self.headval.nextval
self.headval.nextval.preval = temp
self.headval = self.headval.nextval
def removeEnd(self):
temp = self.headval
if temp == None:
return
else:
while temp.nextval is not None:
if temp.nextval.nextval == self.headval:
break
temp = temp.nextval
temp.nextval = self.headval
self.headval.preval = temp
def listprint(self):
temp = self.headval
if temp == None:
return
else:
while temp.nextval is not None:
print(temp.dataval)
if temp.nextval == self.headval:
break
temp = temp.nextval
def listcount(self):
temp = self.headval
if temp == None:
return 0
count = 0
while temp.nextval is not None:
count += 1
if temp.nextval == self.headval:
break
temp = temp.nextval
return count
def removeMiddle(self, removeKey):
temp = self.headval
while temp is not None:
if temp.nextval.dataval == removeKey:
break
temp = temp.nextval
before = temp
key = self.headval
while key is not None:
if key.dataval == removeKey:
break
key = key.nextval
after = key.nextval
before.nextval = after
after.preval = before