0% found this document useful (0 votes)
2 views

python

The document contains a Python implementation of a singly linked list with various functionalities such as appending, prepending, inserting, and deleting nodes at specific positions. It also includes a user interface for interacting with the linked list, allowing users to add or remove nodes based on their choices. The code demonstrates basic linked list operations and user input handling in a loop until the user decides to exit.

Uploaded by

Wahab Tanveer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python

The document contains a Python implementation of a singly linked list with various functionalities such as appending, prepending, inserting, and deleting nodes at specific positions. It also includes a user interface for interacting with the linked list, allowing users to add or remove nodes based on their choices. The code demonstrates basic linked list operations and user input handling in a loop until the user decides to exit.

Uploaded by

Wahab Tanveer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

class Node:

def __init__(self, data):


self.data = data
self.next = None
class LinkedList:

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)

i2=int(input('enter 1 to display list: '))


if(i2==1):
list1.printl()
elif(m==1):
print('Entering Values at front: ')
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.prepend(i)

i2=int(input('enter 1 to display list: '))


if(i2==1):
list1.printl()

elif(m==3):

print('Entering Values at index: ')


a=int(input('enter the index:'))
b=int(input('enter the value:'))
list1.insert(b,a)

i2=int(input('enter 1 to display list: '))


if(i2==1):
list1.printl()
print('if you want to exit press \'yes\'')
c=input()
if(c=='yes'):
break
print('Exiting.....')

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.....')

You might also like