3.1 Single Linked Lists
3.1 Single Linked Lists
3.1 Single Linked Lists
• Linked List can be defined as collection of objects called nodes that are randomly stored in
the memory.
• A node contains two fields i.e. data stored at that particular address and the pointer which
contains the address of the next node in the memory.
• The last node of the list contains pointer to the null.
Linked list is the data structure which can overcome all the limitations of an array. Using linked
list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available memory
space.
Each node divided into two parts: the first part contains data field and the 2nd part called the
‘Link’ filed contains the address of the next node in the list. Such an address, which is used to
access a particular node, is known as a pointer. The entire linked list is accessed from an
Data Structure with Python, Dr. K. Bhowal
external pointer that points to (contains the address of) the first node in the list. (By an
“external” pointer, we mean one that is not included within the node. Rather its value can be
accessed directly by referencing a variable). Internal pointer on the other hand is a pointer
within the node. ‘next’ is the internal pointer in the next example.
The above declaration defines a new data type, who’s each element is of type Node type.
data next
Python implementation:
The following function creates a linked list where each node contains a value:
def createSLL(l):
while True:
x = int(input("Enter data:"))
l.data = x
ans = input("Do you want to create another node (y/n)?")
if ans == "n" or ans == "N":
break
else:
l.next = Node()
l = l.next
def insertBegin(l):
newNode = Node()
x = int(input("Enter data for new node:"))
newNode.data = x
newNode.next = l
return newNode
def insertAfterLastNode(l):
n1 = l
while n1.next:
n1 = n1.next
newNode = Node()
x = int(input("Enter data for new node:"))
newNode.data = x
newNode.next = n1.next
n1.next = newNode
return l
def insertAfterSpecificNode(l):
d = int(input("Enter data of a node after which want to insert:"))
n1 = findNode(l,d)
if n1 == None:
print("Node not found")
return l
newNode = Node()
x = int(input("Enter data for new node:"))
newNode.data = x
newNode.next = n1.next
n1.next = newNode
return l
4. Deletion of a node
Deletion of a node from linked list has the following three instances:
a) Deletion of head
b) Deletion of the last node
c) Deletion of a specified node.
In order to delete a node from the list, it is necessary to search for location of deletion. The steps
involved in deleting the node from the linked list are as follows:
1. If the linked list is empty then display the message “Deletion is not possible”.
2. If the node to be deleted is the first node (pointed to by head pointer) then set the pointer
head to point to the second node in the listed list.
3. If the node to be deleted is the last node, then go on locating the last but one node and set
its link field to point to UNLL pointer.
4. If the situation is other than the above three, then delete the node from specified position
within the linked list.
Program :
#Single Linked list creation & deletion operations
class Node:
def __init__(self):
self.data = None
self.next = None
def createSLL(l):
while True:
x = int(input("Enter data:"))
l.data = x
ans = input("Do you want to create another node (y/n)?")
if ans == "n" or ans == "N":
break
else:
l.next = Node()
l = l.next
def deleteFirstNode(h):
tempNode = h
h = h.next
tempNode.next = None
del tempNode
return h
def deleteSpecificNode(h):
d = int(input("Enter data of a node to delete:"))
l = findNode(h,d)
tempNode = l.next
l.next = l.next.next
tempNode.next = None
del tempNode
return h
def deleteLastNode(h):
l = h
while l.next.next:
l = l.next
tempNode = l.next
l.next = None
del tempNode
return h
def printList(slist):
while slist.next:
print(slist.data, end = " ")
slist = slist.next
print(slist.data, end = " ")
head = Node()
while True:
print("1. Create Linked List")
print("2. Display")
print("3. Delete First Node")
print("4. Delete a specific Node")
print("5. Delete last Node")
print("6. Exit")
x = int(input("Enter your choice:"))
if x == 1:
createSLL(head)
elif x == 2:
Data Structure with Python, Dr. K. Bhowal
printList(head)
elif x == 3:
head = deleteFirstNode(head)
elif x == 4:
head = deleteSpecificNode(head)
elif x == 5:
head = deleteLastNode(head)
elif x == 6:
break
else:
print("Wrong choice")
def merge()
{
list1= Node()
list2=Node()
Data Structure with Python, Dr. K. Bhowal
print(“Enter data for List:”)
createSLL(list)
print(“Enter data for list2:”)
createSLL(list2)
11=list
While 11.next!=None:
11=11.next
11.next=list2
print(list)
}
def spilt(list)
{
key = int(input(“Enter key for 2nd head:”))
head1=list
while list.next.data!=key:
list=list.next
head2=list.next
list.next=None
print(head2)
print(head2)
}
return p;
}
Polynomial Addition:
Linked lists are widely used to represent and manipulate polynomials, polynomials are the
expression containing number of terms with nonzero coefficients and exponents. In the linked
list representation of polynomials, each term is considered as node. And such a node contains
three fields:
1. Coefficient field-> holds the value of the coefficient of a term.
2. Exponent field-> holds the exponent value to that term.
3. Linked field-> holds the address of the next term of the polynomial.
Data Structure with Python, Dr. K. Bhowal
The logical representation of the above node is given below:
class Node:
def __init__(self):
self.coef = None
self.exp = None
self.next = None
#Polynomial addition
class Node:
def __init__(self):
self.coef = None
self.exp = None
self.next = None
def createPoly(p):
while True:
coef = int(input("Enter coeficient:"))
exp = int(input("Enter power:"))
p.coef = coef
p.exp = exp
ans = input("Another term (y/n)?")
if ans == "n" or ans == "N":
break
else:
p.next = Node()
p = p.next
def displayPoly(p):
while p != None:
print(p.coef, "x^",p.exp , end = " ")
p = p.next
Data Structure with Python, Dr. K. Bhowal
def addPoly(poly1,poly2):
poly3 = Node()
p1 = poly1
p2 = poly2
p3 = poly3
if p1 == None:
return p2
if p2 == None:
return p1
p3.next = Node()
p3 = p3.next
if p1 != None:
while p1 != None:
p3.coef = p1.coef
p3.exp = p1.exp
p1 = p1.next
p3.next = Node()
p3 = p3.next
if p2 != None:
while p2 != None:
p3.coef = p2.coef
p3.exp = p2.exp
p2 = p2.next
p3.next = Node()
p3 = p3.next
return poly3
p1 = Node()
Data Structure with Python, Dr. K. Bhowal
p2 = Node()
print("Enter Polynomial 1:")
createPoly(p1)
print("Polynomial 1:")
displayPoly(p1)
print()
print("Enter Polynomial 2:")
createPoly(p2)
print("Polynomial 2:")
displayPoly(p2)
print()
p3 = addPoly(p1,p2)
print("Polynomial 3:")
displayPoly(p3)
Advantages:
1. Accessibility of a node in the forward direction is easier.
2. Insertion and deletion of nodes are easier.
Disadvantages:
1. Accessing the preceding node of a current node is not possible as there is no backward
traversal.
2. Accessing a node is time-consuming.