-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMenu_Driven_Code_for_Linear_LinkedList.py
149 lines (124 loc) · 3.99 KB
/
Menu_Driven_Code_for_Linear_LinkedList.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
# -*- coding: utf-8 -*-
"""
Author: Himanshu Agarwal
Github: himanshu-03 (http://github.com/himanshu-03)
LinkedIn: agarwal-himanshu (https://linkedin.com/in/agarwal-himanshu)
# Menu Driven Code for Linear Linked List
"""
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.root = None
def insertLeft(self, data):
n = Node(data)
if self.root == None:
self.root = n
else:
n.next = self.root
self.root = n
def deleteLeft(self):
if self.root == None:
print('\nLinked List is empty..!!')
else:
temp = self.root
self.root = self.root.next
print('\nDeleted element: ', temp.data)
def insertRight(self, data):
n = Node(data)
if self.root == None:
self.root = n
else:
temp = self.root
while temp.next != None:
temp = temp.next
temp.next = n
def deleteRight(self):
if self.root == None:
print('\nLinked List is empty..!!')
else:
temp = self.root
temp2 = self.root
while temp.next != None:
temp2 = temp
temp = temp.next
temp2.next = None
if temp == self.root:
self.root = None
print('\nDeleted element: ', temp.data)
def printList(self):
if self.root == None:
print('\nLinked List is empty..!!')
temp = self.root
print('\nElements in Linked List are: ')
while temp != None:
print('|', temp.data, '| -> ', end='')
temp = temp.next
print('None')
print()
def searchList(self, data):
if self.root == None:
print('\nLinked List is empty..!!')
else:
count = 0
temp = self.root
while temp != None:
if temp.data == data:
print('\nElement', data, 'found at node: ', count)
return
temp = temp.next
count += 1
return ('\nElement not found')
def deleteElement(self, data):
if self.root == None:
print('\nLinked List is empty..!!')
else:
count = 0
temp = self.root
temp2 = self.root
while temp != None and temp.data != data:
temp2 = temp
temp = temp.next
count += 1
if temp != None:
if temp == self.root:
self.root = self.root.next
elif temp.next == None:
temp2.next = None
else:
temp2.next = temp.next
print('\nDeleted Element:', temp.data,
'from position: ', count)
else:
print(data, 'not found in Linked List')
o = LinkedList()
while True:
print('----------------------')
print('\n1. Insert from Left\n2. Insert from Right\n3. Delete from Left\n4. Delete from Right\n5. Delete Element x\n6. Print Linked List\n7. Search Element x\n0. Exit')
print('----------------------')
ch = int(input('\nEnter your choice: '))
if ch == 1:
data = int(input('\nEnter value to be inserted in left: '))
o.insertLeft(data)
elif ch == 2:
data = int(input('\nEnter value to be inserted in right: '))
o.insertRight(data)
elif ch == 3:
o.deleteLeft()
elif ch == 4:
o.deleteRight()
elif ch == 5:
x = int(input('\nEnter the value of Element x: '))
o.deleteElement(x)
elif ch == 6:
o.printList()
elif ch == 7:
data = int(input('Enter the value of Element x: '))
o.searchList(data)
elif ch == 0:
print('You are out of the program..!!')
break
else:
print('\nWrong Input..\nEnter the correct choice..!!\n')