python
python
def __init__(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
else:
newnode = Node(data)
curr = self.head
while curr.next:
curr = curr.next
curr.next = newnode
def printl(self):
curr = self.head
while curr:
print(curr.data,end='->')
curr = curr.next
def prepend(self, data):
newnode = Node(data)
newnode.next = self.head
self.head=newnode
def printmenu(self):
print('press 1 to add at front')
print('press 2 to add at last')
print('press 3 to add at a particular index')
def printmenu_d(self):
print('press 1 to delete at front')
print('press 2 to delete at last')
print('press 3 to delete at a particular index')
def insert(self, data, k):
pos=0
if k == pos:
newnode = Node(data)
newnode.next = self.head
self.head = newnode
else:
newnode = Node(data)
curr = self.head
while(curr is not None and pos+1 !=k):
pos=pos+1
curr = curr.next
if curr is not None:
newnode.next = curr.next
curr.next=newnode
else:
print("Index out of range")
def del_at_start(self):
if not self.head:
print("List is empty")
else:
temp=self.head.next
self.head=None
self.head = temp
def del_at_end(self):
if not self.head:
print("List is empty")
else:
curr = self.head
while curr.next.next:
curr = curr.next
curr.next = None
def del_at_pos(self, k):
if not self.head:
print("List is empty")
else:
pos = 0
if k == pos:
self.head = self.head.next
else:
curr = self.head
while(curr is not None and pos+1 !=k):
pos=pos+1
curr = curr.next
if curr is not None:
curr.next = curr.next.next
else:
print("Index out of range")
list1 = LinkedList()
a=0
i=0
w=0
m=0
while True:
g=int(input('if you want to add node press 1 or 0 to delete the node:'))
if(g==1):
while True:
print("where you want to inert values: ")
list1.printmenu()
m=int(input())
if(m==2):
print('Entering Values at last: ')
a=int(input('How many values you want to enter:'))
for w in range(a):
i=int(input('Enter value to add in list: '))
list1.append(i)
elif(m==3):
elif(g==0):
list1.printmenu_d()
a=int(input('enter your choice:'))
if(a==1):
print('deleting from front:')
list1.del_at_start()
list1.printl()
elif(a==2):
print('deleting at end:')
list1.del_at_end()
list1.printl()
elif(a==3):
print('deleting at index:')
n=int(input('enter index:'))
list1.del_at_pos(n)
list1.printl()
print('Exiting.....')