-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.PY.py
142 lines (116 loc) · 3.64 KB
/
1.PY.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
"""
Single Linked List.
Starting point is called as the Head.
Last point is called as the tail.
Allows moving forward.
"""
class Node:
def __init__(self, data):
self.data = data
self.link = None
class LinkedList:
def __init__(self):
self.head = None
def insertBegin(self, data):
new_node = Node(data)
new_node.link = self.head
self.head = new_node
def insertEnd(self, data):
new_node = Node(data)
# check whether the list is empty
if(self.head is None):
new_node.link = self.head
self.head = new_node
else:
n = self.head
while(n.link is not None):
n = n.link
n.link = new_node
def insertInBetween(self, data, idx):
new_node = Node(data)
# check whether the list is empty
if (self.head is None):
new_node.link = self.head
self.head = new_node
else:
n = self.head
for i in range(idx-1):
n = n.link
new_node.link = n.link
n.link = new_node
def removeBegin(self):
# case 1: empty list
# case 2: one or many nodes
if (self.head is None):
print("No Node Present")
else:
self.head = self.head.link
def removeEnd(self):
#case 1: empty list.
#case 2: one or many nodes are present.
if(self.head is None):
print("No Node Present")
else:
n = self.head
while(n.link.link is not None):
n = n.link
n.link = None
def removeInBetween(self, idx):
# case 1: empty list
# case 2: one or many nodes
if(self.head is None):
print("Empty List. No node is present.")
else:
n = self.head
for i in range(idx-1):
n = n.link
n.link = n.link.link
def traversal(self):
if(self.head == None):
print("Linked List is Empty")
else:
n = self.head
while(n is not None):
print(n.data)
n = n.link
ll = LinkedList()
while True:
print("Perform the following Operations in Single Linked List: \n"
"\t1. Add\n"
"\t2. Remove\n"
"\t3. Traversal\n")
choice = int(input("Enter your choice: "))
if(choice == 1):
print("Perform the following Add Operations: \n"
"\t1. Begin\n"
"\t2. end\n"
"\t3. Inbetween\n"
)
subChoice = int(input("Enter your sub-choice: "))
data = int(input("Enter the data: "))
if(subChoice == 1):
ll.insertBegin(data)
elif(subChoice == 2):
ll.insertEnd(data)
elif(subChoice == 3):
idx = int(input("Enter the index value: "))
ll.insertInBetween(data, idx)
elif(choice == 2):
print("Perform the following Remove Operations: \n"
"\t1. Begin\n"
"\t2. end\n"
"\t3. Inbetween\n"
)
subChoice = int(input("Enter your sub-choice: "))
if (subChoice == 1):
ll.removeBegin()
elif (subChoice == 2):
ll.removeEnd()
elif (subChoice == 3):
idx = int(input("Enter the index value: "))
ll.removeInBetween(idx)
elif(choice == 3):
ll.traversal()
else:
print("Entered the wrong choice")
break