0% found this document useful (0 votes)
5 views3 pages

Linked List Operations in 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Linked List Operations in 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

class Node:

def __init__(self, data):


[Link] = data
[Link] = None
class LinkedList:

def __init__(self):
[Link] = None
def append(self, data):
if not [Link]:
[Link] = Node(data)
else:
newnode = Node(data)
curr = [Link]
while [Link]:

curr = [Link]
[Link] = newnode
def printl(self):
curr = [Link]
while curr:
print([Link],end='->')
curr = [Link]
def prepend(self, data):
newnode = Node(data)
[Link] = [Link]
[Link]=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)
[Link] = [Link]
[Link] = newnode

else:
newnode = Node(data)
curr = [Link]
while(curr is not None and pos+1 !=k):

pos=pos+1
curr = [Link]
if curr is not None:

[Link] = [Link]
[Link]=newnode
else:
print("Index out of range")
def del_at_start(self):
if not [Link]:
print("List is empty")
else:
temp=[Link]
[Link]=None
[Link] = temp
def del_at_end(self):
if not [Link]:
print("List is empty")

else:
curr = [Link]
while [Link]:
curr = [Link]
[Link] = None
def del_at_pos(self, k):

if not [Link]:

print("List is empty")
else:
pos = 0
if k == pos:
[Link] = [Link]
else:
curr = [Link]
while(curr is not None and pos+1 !=k):
pos=pos+1
curr = [Link]
if curr is not None:
[Link] = [Link]
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: ")
[Link]()
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: '))
[Link](i)

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


if(i2==1):
[Link]()
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: '))
[Link](i)

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


if(i2==1):
[Link]()

elif(m==3):

print('Entering Values at index: ')


a=int(input('enter the index:'))
b=int(input('enter the value:'))
[Link](b,a)

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


if(i2==1):
[Link]()
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()
[Link]()
elif(a==2):
print('deleting at end:')
list1.del_at_end()
[Link]()
elif(a==3):
print('deleting at index:')
n=int(input('enter index:'))
list1.del_at_pos(n)
[Link]()
print('Exiting.....')

You might also like