-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
183 lines (115 loc) · 3.71 KB
/
LinkedList.py
File metadata and controls
183 lines (115 loc) · 3.71 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class Node:
def __init__(self, value):
self.data = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.n = 0
def __len__(self):
return self.n
def __str__(self):
result = ''
curr = self.head
while curr != None:
result = result + str(curr.data) + '->'
curr = curr.next
return result[:-2]
def insert_head(self, value):
new_node = Node(value)
new_node.next = self.head
self.head = new_node
self.n += 1
def insert_middle(self, after, value):
new_node = Node(value)
curr = self.head
while curr.next != None:
if curr.data == after:
break
curr = curr.next
if curr.next == None:
print("Item not found.")
else:
new_node.next = curr.next
curr.next = new_node
self.n += 1
def insert_tail(self, value):
new_node = Node(value)
if self.head == None:
self.head = new_node
self.n += 1
return
curr = self.head
while curr.next != None:
curr = curr.next
curr.next = new_node
self.n += 1
def delete_head(self):
if self.head == None:
print("Empty linked list.")
return
self.head = self.head.next
self.n -= 1
def delete_middle(self, value):
if self.head == None:
print("Empty linked list.")
return
if self.head.data == value:
self.delete_head()
return
curr = self.head
while curr.next != None:
if curr.next.data == value:
break
curr = curr.next
if curr.next != None:
curr.next = curr.next.next
else:
print("Item not found")
def delete_tail(self):
if self.head == None:
print("Empty linked list.")
return
if self.head.next == None:
self.delete_head()
return
curr = self.head
while curr.next.next != None:
curr = curr.next
curr.next = None
self.n -= 1
def search_by_value(self, value):
index = 0
curr = self.head
while curr != None:
if curr.data == value:
return index
curr = curr.next
index += 1
return "Item not found"
def search_by_index(self, index):
pos = 0
curr = self.head
while curr != None:
if pos == index:
return curr.data
curr = curr.next
pos += 1
return "Index not found."
if __name__ == "__main__":
ll = LinkedList()
ll.insert_head(1)
ll.insert_head(2)
ll.insert_head(3)
ll.insert_tail(4)
ll.insert_tail(5)
ll.insert_middle(1,6)
ll.insert_middle(2,8)
ll.insert_middle(6,9)
# ll.delete_head()
# ll.delete_tail()
# ll.delete_middle(6)
print(len(ll))
print(ll)
print(ll.search_by_value(8))
print(ll.search_by_index(5))