-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListParticipation.py
More file actions
168 lines (147 loc) · 4.39 KB
/
LinkedListParticipation.py
File metadata and controls
168 lines (147 loc) · 4.39 KB
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
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return "Node({})".format(self.value)
__repr__ = __str__
class LinkedList:
def __init__(self):
self.head=None
self.tail=None
def __str__(self):
temp=self.head
out=[]
while temp:
out.append(str(temp.value))
temp=temp.next
out=' -> '.join(out)
return 'Head:{}\nTail:{}\nList: {}'.format(self.head,self.tail,out)
__repr__=__str__
def replace(self, new, a, b=-2):
'''
>>> lst1 = LinkedList()
>>> lst1.add(5)
>>> lst1.add(6)
>>> lst1.add(9)
>>> lst1.add(10)
>>> lst1.add(0)
>>> lst1.add(8)
>>> lst2 = LinkedList()
>>> lst2.add('a')
>>> lst2.add('b')
>>> lst2.add('c')
>>> lst1
Head:Node(8)
Tail:Node(5)
List: 8 -> 0 -> 10 -> 9 -> 6 -> 5
>>> lst2
Head:Node(c)
Tail:Node(a)
List: c -> b -> a
>>> lst1.replace(lst2, 2, 4)
>>> lst1
Head:Node(8)
Tail:Node(5)
List: 8 -> 0 -> c -> b -> a -> 6 -> 5
'''
#Keeps track of where to add new nodes
connectStart = connectEnd = None
temp = self.head
i = 0 #Index to keep track of where one is
while temp:
#If the node is right before the connection point
if (i == (a-1)):
connectStart = temp
if (i == b):
connectEnd = temp
i += 1
temp = temp.next
#Splicing the two lists together
connectStart.next = new.head
temp = self.head
while temp.next:
temp = temp.next
temp.next = connectEnd
#Updating the tail
while temp.next:
temp = temp.next
self.tail = temp
def isEmpty(self):
return self.head==None
def __len__(self):
current=self.head
count=0
while current is not None:
count += 1
current = current.next
return count
def add(self, value):
newNode=Node(value)
if self.isEmpty():
self.head=newNode
self.tail=newNode
else:
newNode.next=self.head
self.head=newNode
def __contains__(self,value):
current=self.head
while current is not None:
if current.value==value:
return True
else:
current=current.next
return False
def __delitem__(self,position):
if self.isEmpty():
print('List is empty')
return None
if len(self)>=position:
current=self.head
previous=None
count=1
while count<position:
previous=current
current=current.next
count+=1
if previous is None:
self.head=current.next
current.next=None
elif current.next is None:
previous.next=None
self.tail=previous
else:
previous.next=current.next
current.next=None
def append(self, value):
newNode = Node(value)
if self.isEmpty():
self.head=newNode
self.tail=newNode
else:
self.tail.next = newNode
self.tail = newNode
def __getitem__(self,value):
current=self.head
while current is not None:
if current.value==value:
return current
else:
current=current.next
return None
def pop(self):
if self.isEmpty():
print('List is empty')
return None
else:
removed = self.tail.value
if len(self) == 1:
self.head = None
self.tail = None
else:
current = self.head
while current.next.next is not None:
current = current.next
current.next = None
self.tail = current
return removed