forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.py
180 lines (166 loc) · 4.92 KB
/
SinglyLinkedList.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
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
# Python program for all basic functionalities of Singly Linked List
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Constructor to initialize head
def __init__(self):
self.head = None
def insertNodeAtFront(self, ndata):
nnode = Node(ndata)
nnode.next = self.head
self.head = nnode
def insertNodeAtEnd(self, ndata):
nnode = Node(ndata)
if self.head == None:
self.head = nnode
else:
last = self.head
while last.next != None:
last = last.next
last.next = nnode
def insertNodeAtPosition(self, index, ndata):
if index == 1:
insertNodeAtFront(self, ndata)
else:
cur = 1
pos = self.head
while cur < index-1 and pos != None:
pos = pos.next
cur += 1
if pos == None:
insertNodeAtEnd(self, ndata)
else:
nnode = Node(ndata)
nnode.next = pos.next
pos.next = nnode
def deleteNodeAtFront(self):
if self.head == None:
print("Empty list, nothing to delete!")
else:
self.head = self.head.next
def deleteNodeAtEnd(self):
if self.head == None:
print("Empty list, nothing to delete!")
elif self.head.next == None:
self.head=None
else:
pos = self.head
while pos.next.next != None:
pos = pos.next
pos.next = None
def deleteNodeAtPosition(self, index):
if self.head == None:
print("Empty list, nothing to delete!")
return
pos = self.head
if index == 1:
self.head = pos.next
temp = None
return
for ind in range(1,index-1):
pos = pos.next
if pos == None:
break
if pos == None or pos.next == None:
print("Index more than number of nodes!")
else:
next = pos.next.next
pos.next = None
pos.next = next
def traverseList(self):
if self.head == None:
print("Empty list, nothing to print!")
else:
n = self.head
while n != None:
print(n.data)
n = n.next
def countNodes(self):
cnt = 0
ind = self.head
while ind != None:
cnt += 1
ind = ind.next
print(cnt)
if __name__=='__main__':
newLinkedList = LinkedList()
print("SINGLY LINKED LIST")
print("1. Insert Node at Front")
print("2. Insert Node at End")
print("3. Insert Node at Position")
print("4. Delete Node at Front")
print("5. Delete Node at End")
print("6. Delete Node at Position")
print("7. Print the List")
print("8. Count nodes in the List")
print("9. Exit")
print("--Use 1-based indexing--")
while(True):
ch = int(input("Enter your choice: "))
if ch == 1:
ndata = int(input("Enter the element to insert: "))
newLinkedList.insertNodeAtFront(ndata)
elif ch == 2:
ndata = int(input("Enter the element to insert: "))
newLinkedList.insertNodeAtEnd(ndata)
elif ch == 3:
ndata = int(input("Enter the element to insert: "))
ind = int(input("Enter index to insert this element: "))
newLinkedList.insertNodeAtPosition(ind,ndata)
elif ch == 4:
newLinkedList.deleteNodeAtFront()
elif ch == 5:
newLinkedList.deleteNodeAtEnd()
elif ch == 6:
ind = int(input("Enter index to delete element: "))
newLinkedList.deleteNodeAtPosition(ind)
elif ch == 7:
newLinkedList.traverseList()
elif ch == 8:
newLinkedList.countNodes()
elif ch == 9:
break
else:
print("Enter a valid choice (1-9)")
# --- EXAMPLE ---
# SINGLY LINKED LIST
# 1. Insert Node at Front
# 2. Insert Node at End
# 3. Insert Node at Position
# 4. Delete Node at Front
# 5. Delete Node at End
# 6. Delete Node at Position
# 7. Print the List
# 8. Count nodes in the List
# 9. Exit
# --Use 1-based indexing--
# Enter your choice: 1
# Enter the element to insert: 10
# Enter your choice: 2
# Enter the element to insert: 5
# Enter your choice: 3
# Enter the element to insert: 3
# Enter index to insert this element: 2
# Enter your choice: 7
# 10
# 3
# 5
# Enter your choice: 6
# Enter index to delete element: 2
# Enter your choice: 7
# 10
# 5
# Enter your choice: 8
# 2
# Enter your choice: 4
# Enter your choice: 7
# 5
# Enter your choice: 5
# Enter your choice: 7
# Empty list, nothing to print!
# Enter your choice: 10
# Enter a valid choice (1-9)
# Enter your choice: 9