Skip to content

Commit 5438853

Browse files
authored
Merge pull request kelvins#336 from MrWeast/feature/unordered-linked-list-python
create unordered list in python
2 parents 5531625 + b98c692 commit 5438853

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,8 +2230,8 @@ In order to achieve greater coverage and encourage more people to contribute to
22302230
</a>
22312231
</td>
22322232
<td> <!-- Python -->
2233-
<a href="./CONTRIBUTING.md">
2234-
<img align="center" height="25" src="./logos/github.svg" />
2233+
<a href="./src/python/unordered_linked_list.py">
2234+
<img align="center" height="25" src="./logos/python.svg" />
22352235
</a>
22362236
</td>
22372237
<td> <!-- Go -->

src/python/unordered_linked_list.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
class Node:
2+
def __init__(self, value, rptr=None) -> None:
3+
self.value = value
4+
self.rptr = rptr
5+
6+
@property
7+
def get_value(self):
8+
return self.value
9+
10+
@property
11+
def next(self):
12+
return self.rptr
13+
14+
@next.setter
15+
def next(self, ptr):
16+
self.rptr = ptr
17+
18+
19+
class Unordered_Linked_List:
20+
def __init__(self) -> None:
21+
self.first_node = None
22+
23+
def insert(self, value):
24+
# inserts a new node with the given value
25+
if self.first_node is None:
26+
self.first_node = Node(value)
27+
else:
28+
tmp = Node(value)
29+
tmp.next = self.first_node
30+
self.first_node = tmp
31+
32+
def find(self, value):
33+
# returns true if the specified value is in your list
34+
ptr = self.first_node
35+
while ptr is not None:
36+
if ptr.value == value:
37+
print(f"{value} is in your list")
38+
return True
39+
else:
40+
ptr = ptr.next
41+
42+
print(f"{value} is not in your list")
43+
return False
44+
45+
def size(self): # returns size of the list
46+
ptr = self.first_node
47+
i = 0
48+
while ptr is not None:
49+
ptr = ptr.next
50+
i += 1
51+
print(f"Your list is of size {i}")
52+
return i
53+
54+
def remove(self, value):
55+
# removes all instances of a given value
56+
ptr = self.first_node
57+
prev = None
58+
while ptr is not None:
59+
if ptr.value == value:
60+
if ptr == self.first_node:
61+
tmp = ptr.next
62+
self.first_node = tmp
63+
else:
64+
prev.next = ptr.next
65+
66+
prev = ptr
67+
ptr = ptr.next
68+
69+
def show(self):
70+
ptr = self.first_node
71+
val = []
72+
while ptr is not None:
73+
val.append(ptr.get_value)
74+
ptr = ptr.next
75+
76+
print(val)
77+
78+
79+
def main():
80+
list = Unordered_Linked_List()
81+
82+
list.insert(1)
83+
list.insert(3)
84+
list.insert(5)
85+
list.insert(2)
86+
87+
list.size()
88+
list.show()
89+
90+
list.find(3)
91+
list.find(9)
92+
93+
list.remove(1)
94+
list.remove(3)
95+
list.remove(5)
96+
list.remove(2)
97+
98+
list.show()
99+
100+
list.insert(1)
101+
list.insert(3)
102+
list.insert(5)
103+
list.insert(3)
104+
105+
list.show()
106+
107+
list.remove(3)
108+
109+
list.show()
110+
111+
112+
if __name__ == "__main__":
113+
main()

0 commit comments

Comments
 (0)