-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly_linked_list.py
160 lines (130 loc) · 4.57 KB
/
singly_linked_list.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 21 17:23:13 2020
@author: yashodhara
"""
#singly linked list implimentation with Insert - Delete - Display - Search Functions
import os # For only clearing screen after every operation.
######################### Class Block #################################
class Node:
def __init__(self,val,next=None):
self.val = val
self.next = next
################################################
def display(self):
curr = self.next
print()
while curr:
print(curr.val," => ",end=" ")
curr = curr.next
print()
return
################################################
def length(self):
curr = self.next
cnt=0
while curr:
cnt+=1
curr = curr.next
return cnt
################################################
def insert(self,n):
curr = self
while curr.next:
curr = curr.next
for i in range(n):
print("Enter value for Node {}: ".format(i+1),end=" ")
val = int(input())
curr.next = Node(val)
curr = curr.next
return
###############################################
def insert_pos(self,pos):
curr = self
cnt = 0
l = curr.length()
if pos > l:
print("-----------------Invalid position------------------")
return
val = int(input(" Enter Element to insert : "))
while curr:
n_node = curr.next
if cnt+1 == pos:
curr.next = Node(val)
curr = curr.next
curr.next = n_node
print("Element {} Inserted..".format(curr.val))
return
else:
curr = curr.next
cnt+=1
##############################################
def delete(self):
curr = self
cnt = 0
l = curr.length()
if l == 0:
print(" No nodes to delete")
else:
pos = int(input(" Enter position to delete : "))
if pos > l:
print("--------------------Invalid position--------------------")
return
else:
while curr:
if cnt+1 == pos:
dele = curr.next.val
n_node = curr.next.next
curr.next = n_node
print(" Element {} Deleted successfully...".format(dele))
return
else:
curr = curr.next
cnt+=1
#############################################
def search(self,key):
curr =self.next
cnt = 1
while curr:
if curr.val == key:
print("Element {} found at '{}'th position..".format(key,cnt))
return
else:
cnt+=1
curr = curr.next
print("-----------------Element not found in linked List-------------------")
############################### Class Block Ends ########################################
# To clear screen after every operation.
def clear():
os.system('clear')
############################### Main block starts #######################################
def main():
first = Node(0)
print("--------------Singly Linked List---------------")
# Operation performing loop
while(True):
choice = int(input("Operations available :\n 1.insert\n 2.Insert at Position\n 3.display\n 4.Delete\n 5.Search\n 6.Exit\nEnter Number to perform specified operation : "))
if choice == 1:
n = int(input("Enter number of nodes to insert : "))
first.insert(n)
elif choice == 2:
pos = int(input("Enter position to insert : "))
first.insert_pos(pos)
elif choice == 3:
first.display()
elif choice == 4:
first.delete()
elif choice == 5:
key = int(input(" Enter the key element to search : "))
first.search(key)
elif choice == 6:
break
else:
print("-----------Invalid choice-----------")
input()
clear()
############################## Main block Ends ######################################
# Main block invoking...
if __name__ == '__main__':
main()