-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
153 lines (127 loc) · 3.88 KB
/
LinkedList.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __str__(self):
return f"{self.data}"
class LinkedList:
def __init__(self):
self.head = None
self.length = 0
def traverse(self):
if self.head is None:
print("LinkedList is empty")
else:
head = self.head
while head is not None:
print(head, end=" ")
head = head.next
print("\n")
def insert_at_beginning(self, new_node):
new_node.next = self.head
self.head = new_node
self.length += 1
def insert_at_end(self, new_node):
if self.head is None:
self.head = new_node
else:
head = self.head
last_node = None
while head is not None:
last_node = head
head = head.next
last_node.next = new_node
self.length += 1
def insert_at_specific_position(self, new_node, position):
if self.head is None and position > 0:
print(f"Linked list is empty. Position {position} is not available in the LinkedList.")
elif self.head is None and position == 0:
self.head = new_node
elif position > self.length:
print(f"Can not insert at index {position} for a LinkedList with {self.length} size.")
else:
current_position = 0
head = self.head
if position == 0:
new_node.next = head
self.head = new_node
self.length += 1
return
while head is not None:
current_position += 1
if current_position == position:
break
else:
head = head.next
new_node.next = head.next
head.next = new_node
self.length += 1
def delete_at_beginning(self):
if self.head is not None:
self.head = self.head.next
self.length -= 1
def delete_from_end(self):
if self.head is None:
print("LinkedList is empty. Nothing is available to delete.")
# traverse till end
head = self.head
prev = None
while head.next is not None:
prev = head
head = head.next
prev.next = None
self.length -= 1
def delete_from_specific_position(self, position):
if position == 0:
self.delete_at_beginning()
elif position == self.length:
self.delete_from_end()
elif position > self.length:
print(f"Position {position} is greater than length {self.length} of the LinkedList.")
else:
head = self.head
prev = None
count = 0
while head is not None:
prev = head
head = head.next
count += 1
if position == count:
break
prev.next = head.next
if __name__ == "__main__":
n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5)
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
ll = LinkedList()
ll.head = n1
ll.traverse()
# insert at beginning
ll.insert_at_beginning(Node(0))
ll.insert_at_beginning(Node(-1))
ll.traverse()
# insert at the end
ll.insert_at_end(Node(6))
ll.insert_at_end(Node(7))
ll.traverse()
# insert at position 2
ll.insert_at_specific_position(Node(20), 2)
ll.traverse()
# insert at position 0
ll.insert_at_specific_position(Node(10), 0)
ll.traverse()
# delete at the beginning
ll.delete_at_beginning()
ll.traverse()
# delete from the end
ll.delete_from_end()
ll.traverse()
# delete from position 2
ll.delete_from_specific_position(2)
ll.traverse()