-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLL.py
148 lines (118 loc) · 4.22 KB
/
SLL.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
# Program to do Singly Linked List Operations
import os
class Node:
def __init__(self, value = None) -> None:
self.value = value
self.next = None
class SLL:
def __init__(self) -> None:
self.head = None
self.tail = None
def __iter__(self):
node = self.head
while node:
yield node
node = node.next
def insert(self, value, location = -1):
'''Insert in Singly Linked List'''
newNode = Node(value)
if self.head is None:
'''Empty List'''
self.head = newNode
self.tail = newNode
elif location == 0:
'''Insertion in the beginning'''
newNode.next = self.head
self.head = newNode
elif location == -1:
'''Insertion at the End'''
self.tail.next = newNode
self.tail = newNode
else:
'''Insertion in custom location'''
tempNode = self.head
for index in range(1, location):
if tempNode.next is None:
print('Index out of Range!!!')
return
tempNode = tempNode.next
if tempNode is self.tail:
'''if last node is reached'''
self.tail.next = newNode
self.tail = newNode
else:
nextNode = tempNode.next
tempNode.next = newNode
newNode.next = nextNode
def delete(self, location):
'''Delete from Singly Linked List'''
if self.head is None:
print('Linked List is Empty!!! Aborting...')
return
elif location == 0:
'''Deletion in the beginning'''
current = self.head
if current.next is None:
'''only 1 element in list'''
self.head = None
self.tail = None
else:
'''more than 1 elements present'''
self.head = current.next
elif location == -1:
'''Deletion in the end'''
current = self.head
if current.next is None:
'''only 1 element in list'''
self.head = None
self.tail = None
else:
'''list has multiple elements'''
while current.next is not None:
'''find the second last element'''
previous = current
current = current.next
previous.next = None
self.tail = previous
else:
'''Deletion from custom location'''
current = self.head
if current.next is None:
'''only 1 element in list'''
print('Index out Range!!!')
else:
'''multiple elements in list'''
for index in range(1, location):
current = current.next
if current.next is None:
print('Index out of Range!!!')
return
if current.next is self.tail:
'''if last node is to be deleted'''
current.next = None
self.tail = current
else:
nextNode = current.next
current.next = nextNode.next
sll = SLL()
while True:
print(f'Singly Linked List: {[node.value for node in sll]}')
print('1. Insertion')
print('2. Deletion')
choice = input('Enter your choice: ')
if choice == '1':
while choice in ('1', 'Y', 'y'):
value = int(input('Value: '))
pos = int(input('Position: '))
sll.insert(value, pos)
print(f'Singly Linked List: {[node.value for node in sll]}')
choice = input('Continue? (Y/N) ')
elif choice == '2':
while choice in ('2', 'Y', 'y'):
pos = int(input('Position: '))
sll.delete(pos)
print(f'Singly Linked List: {[node.value for node in sll]}')
choice = input('Continue? (Y/N) ')
else:
break
os.system('cls' if os.name == 'nt' else 'clear')