-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.py
172 lines (136 loc) · 4.18 KB
/
linklist.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def listprint(self):
printval=self.head
while printval is not None:
print(printval.data)
printval=printval.next
def search(self):
num=int(input("Enter num to be searched:"))
ref=self.head
flag=0
while ref is not None:
if ref.data==num:
print("num is found")
flag=1
break
ref=ref.next
if flag==0:
print("Num is not found")
def insertatbeg(self):
num=int(input("Enter number to be inserted"))
Newnode=Node(num)
#list is empty
if self.head==None:
print("list is empty")
#General Case
Newnode.next=self.head
self.head=Newnode
def insertatend(self):
num=int(input("Enter num to be inserted"))
Newnode=Node(num)
#list is empty
if self.head==None:
print("list is empty")
Newnode.next=self.head
self.head=Newnode
#General Case
else:
ref=self.head
while ref.next:
ref=ref.next
ref.next=Newnode
def insertatmid(self):
num=int(input("Enter num to be inserted"))
Newnode=Node(num)
loc=int(input("Enter num after which you want to insert"))
#list is empty
if self.head==None:
print("list is empty")
Newnode.next=None
self.head=Newnode
return
#general case
ref=self.head
while ref is not None:
prev=ref
if ref.data==loc:
Newnode.next=ref.next
ref.next=Newnode
return
ref=ref.next
#if num is not in list it should be added at the end
if ref==None:
print("Num is not found")
Newnode.next=None
prev.next=Newnode
def delete(self):
num=int(input("Enter num to delete"))
ref=self.head
prev = self.head
if ref==None:
print("list is empty")
return
if self.head.data==num:
self.head = self.head.next
return
while ref is not None:
if ref.data==num:
break
prev=ref
ref=ref.next
if ref!=None:
prev.next=ref.next
else:
print("Num is not found")
def options(self):
print("What do you want to do")
print("1. Print the list")
print("2. Search")
print("3. Insert at the beginning")
print("4. Insert at the end")
print("5. Insert at the mid")
print("6. Delete")
print("7. Exit")
option=int(input("Enter your option"))
if option==1:
llist.listprint()
llist.options()
if option==2:
llist.search()
llist.options()
if option==3:
llist.insertatbeg()
llist.listprint()
llist.options()
if option==4:
llist.insertatend()
llist.listprint()
llist.options()
if option==5:
llist.insertatmid()
llist.listprint()
llist.options()
if option==6:
llist.delete()
llist.listprint()
llist.options()
if option==7:
exit
# Code execution starts here
if __name__=='__main__':
# Start with the empty list
llist = LinkedList()
#llist.head = Node(1)
#second = Node(2)
#third = Node(3)
#fourth=Node(4)
#llist.head.next = second; # Link first node with second
#second.next = third; # Link second node with the third node
#third.next=fourth
llist.options()