-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathSinglyLinkedList.py
112 lines (94 loc) · 2.87 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
class Node:
# constructor
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
# insertion method
def insertNode(self, ele):
newNode = Node(ele)
if(self.head):
current = self.head
while(current.next):
current = current.next
current.next = newNode
else:
self.head = newNode
# deletion method
def deleteNode(self, ele):
p1 = self.head
# if head node is the element to be deleted
if p1 is not None:
if p1.data == ele:
self.head = p1.next
p1 = None
print("!!! ELEMENT DELETED !!!")
return
# search ele in list
while p1 is not None:
if p1.data == ele:
break
p2 = p1
p1 = p1.next
# if element not present in list
if p1 == None:
print("*** ELEMENT NOT FOUND ***")
return
# delete the node
p2.next = p1.next
p1 = None
# search method
def searchNode(self, ele):
p1 = self.head
# head element to be searched
if p1.data == ele:
print("!!! ELEMENT FOUND !!!")
return
while p1 is not None:
if p1.data == ele:
print("!!! ELEMENT FOUND !!!")
return
p1 = p1.next
print("*** ELEMENT NOT FOUND ***")
return
# print method
def printList(self):
current = self.head
while(current):
print(current.data, end=" ")
current = current.next
def main():
List = LinkedList()
ch = 'y'
# input code
while ch == 'y':
print("Choose an operation :\n1. Insertion\n2. Deletion\n3. Search\n4. Print\n\tMAKE YOUR CHOICE : ", end="")
c = int(input())
# insertion
if c == 1:
print("\nINSERTION\nEnter List Size : ", end="")
n = int(input())
print("\nEnter List Elements : ", end="")
eleList = [int(i) for i in input().split()][:n]
for i in eleList:
List.insertNode(i)
# deletion
elif c == 2:
print("DELETION\nEnter element for deletion : ", end="")
ele = int(input())
List.deleteNode(ele)
# search
elif c == 3:
print("SEARCHING\nEnter element to be searched : ", end="")
ele = int(input())
List.searchNode(ele)
# print
elif c == 4:
print("List Elements : ", end="")
List.printList()
else:
print("\n\t*** INVALID INPUT ***")
ch = input("\n\tTry Again ? (y/n) : ")
main()