# Linked List Operations

Table of Contents

###### Linked List Operations

Program gives you more detailed explanation on operations of linked list. It will help students to understand the concepts of linked list and its working. It will help beginners to understand the concepts of inked list.

Operations we have performed are:

• Prepend
• Insert from tail
• Search a value
• Delete from head
• Delete from tail
• Delete value
• Traversing
```class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Author : ITVoyagers Website : itvoyagers.in
def __init__(self):
self.head = None
self.tail = None
self.size=0

#....................................Insert value in begining...............................................
def prepending(self, data):
newnode=Node(data)
if self.head ==None:
self.head=newnode
self.tail=newnode
else:
newnode.next=self.head
self.head=newnode
print("node inserted successfully with data ",data,"at the beginning")
self.size+=1
# Author : ITVoyagers Website : itvoyagers.in
#....................................Insert value from tail...............................................
def insertfromtail(self, data):
newnode=Node(data)
if self.tail ==None:
self.head=newnode
self.tail=newnode
else:
self.tail.next=newnode
self.tail=newnode
print("node inserted successfully with data ",data,"from the tail")
self.size+=1
# Author : ITVoyagers Website : itvoyagers.in
#....................................search a  value .....................................................
def search(self,value):
self.n = 0
current=self.head
while(current!=None):
self.n=self.n+1
if (current.data!=value):
current = current.next
else:
return True #if value is found
return False #if value is not found
# Author : ITVoyagers Website : itvoyagers.in
#....................................Delete value from head...............................................
def deletefromhead(self):
if self.head==None:
print("Empty linked list")
else:
value=self.head.data
print("Deleted node is",value)
self.head=self.head.next
self.size-=1
#....................................Delete value from tail...............................................
def deletefromtail(self):
if self.head==None:
print("Empty linked list")
else:
current=self.head
while (current.next!=None):
prev=current
current=current.next
value=current.data
prev.next=None
print("Deleted node is",value)
self.tail=prev
self.size-=1
# Author : ITVoyagers Website : itvoyagers.in
# ....................................traversing value from tail...............................................
def deletevalue(self,value):
current=self.head
while (current!=None and current.data!=value):
prev=current
current=current.next
if current==None:
print("node not found")
else:
self.size-=1
if current==self.head:
self.head = current.next
elif current== self.tail:
prev.next=current.next
self.tail=prev
else:
prev.next=current.next
print("deleted successfully")
# Author : ITVoyagers Website : itvoyagers.in
# ....................................traversing value from tail...............................................
def traversing(self):
current=self.head
while (current.next!=None):
print(current.data)
current=current.next
print(current.data)
#........................................................................................................................
k=LinkedList()
print("Menu")
print("1.Prepend n2.Insert from tail n3.Search a value n4.Delete from head")
print("5.Delete from tail n6.Delete value  n7.Traversing ")
ch=int(input("Enter your choice"))
while(ch!=8):
if ch==1:
value=int(input("enter a value to be inserted from head"))
k.prepending(value)
elif ch==2:
value=int(input("enter a value to be inserted from tail"))
k.insertfromtail(value)
elif ch==3:
value=int(input("enter a value to be searched"))
res=k.search(value)
if res == True:
print("Value is",value)
else:
print("Value not found")
elif ch==4:
k.deletefromhead()
elif ch==5:
k.deletefromtail()
elif ch==6:
value=int(input("enter a value to be deleted"))
k.deletevalue(value)
else:
k.traversing()
ch=int(input("Enter your choice"))
# Author : ITVoyagers Website : itvoyagers.in

```

Output :

```Menu
1.Prepend
2.Insert from tail
3.Search a value
4.Delete from head
5.Delete from tail
6.Delete value
7.Traversing
Enter your choice1
enter a value to be inserted from head3
node inserted successfully with data  3 at the beginning
Enter your choice1
enter a value to be inserted from head2
node inserted successfully with data  2 at the beginning
Enter your choice1
enter a value to be inserted from head5
node inserted successfully with data  5 at the beginning
Enter your choice7
5
2
3
Enter your choice
```

Note : Please note that above program is compatible with Python 3 or higher version