343330PYTHON_EXP 6
343330PYTHON_EXP 6
Experiment No.06
A.1 Aim:
A.3 Outcome:
After successful completion of this experiment students will be able to
Like arrays, a Linked List is a linear data structure. Unlike arrays, linked list elements are not
stored at a contiguous location; the elements are linked using pointers. They include a series of
connected nodes. Here, each node stores the data and the address of the next node.
Linked-List
Arrays can be used to store linear data of similar types, but arrays have the following limitations:
The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the
usage.
Example:
In a system, if we maintain a sorted list of IDs in an array id[] = [1000, 1010, 1050, 2000, 2040].
If we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the
elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For
example, to delete 1010 in id[], everything after 1010 has to be moved due to this so much work
is being done which affects the efficiency of the code.
Here's a list of basic linked list operations that we will cover in this article.
Before you learn about linked list operations in detail, make sure to know about Linked List first.
next pointer of the last node is NULL, so if the next current node is NULL, we have reached the
end of the linked list.
In all of the examples, we will assume that the linked list has three nodes 1 --->2 --->3 with node
structure as below:
struct node {
int data;
};
Displaying the contents of a linked list is very simple. We keep moving the temp node to the
next one and display its contents.
When temp is NULL, we know that we have reached the end of the linked list so we get out of
the while loop.
while(temp != NULL) {
printf("%d --->",temp->data);
temp = temp->next;
You can add elements to either the beginning, middle or end of the linked list.
Store data
newNode->data = 4;
newNode->next = head;
head = newNode;
Store data
newNode->data = 4;
newNode->next = NULL;
while(temp->next != NULL){
temp = temp->next;
temp->next = newNode;
newNode->data = 4;
if(temp->next != NULL) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
You can delete either from the beginning, end or from a particular position.
head = head->next;
while(temp->next->next!=NULL){
temp = temp->next;
temp->next = NULL;
if(temp->next!=NULL) {
temp = temp->next;
}
}
temp->next = temp->next->next;
PART B
(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge
faculties at the end of the practical in case the there is no Black board access available)
Grade :
Overall, implementing a menu-driven program for a linked list data structure is an excellent
exercise for improving programming skills, understanding data structures, and enhancing problem-
solving abilities. It combines theory with practical implementation, making it a valuable learning
experience.
B.4 Conclusion:
1. Write LL operations in details such as Traversal, Insertion, Deletion, Search, Sort etc.
Traversal:
To traverse a singly linked list, start from the head node and iterate through each node by following
the next pointers until you reach the end of the list (i.e., when the next pointer of a node is null).
This can be done using a loop or recursion.
Insertion:
At the beginning: Create a new node, set its next pointer to the current head of the list, and then
update the head to point to the new node.
At the end: Traverse the list until you reach the last node, then create a new node and set the next
pointer of the last node to the new node.
After a specific node: Traverse the list until you find the desired node, then create a new node and
adjust the pointers accordingly.
Deletion:
From the beginning: Update the head to point to the next node and free the memory of the node
being deleted.
From the end: Traverse the list until you reach the second to last node, update its next pointer to
null, and free the memory of the last node.
After a specific node: Traverse the list until you find the node before the node to be deleted, then
adjust the pointers to bypass the node to be deleted, and free its memory.
Search:
Start from the head node and traverse the list while comparing the value of each node with the
target value until either the target value is found or the end of the list is reached.
Sort:
One common approach to sort a singly linked list is to use a sorting algorithm such as Merge Sort
or Insertion Sort.
Merge Sort: Divide the list into smaller sublists recursively, sort the sublists, and then merge them
together in sorted order.
Insertion Sort: Traverse the list and insert each node into its correct position in a new sorted list.
2.Writedown any sorting algorithm to sort the elements in the Linked List.
class Node:
self.data = data
self.next = None
class LinkedList:
self.head = None
new_node = Node(data)
if not self.head:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def display(self):
current = self.head
while current:
current = current.next
print("None")
return head
mid = self.find_middle(head)
next_to_mid = mid.next
mid.next = None
left_half = self.merge_sort(head)
right_half = self.merge_sort(next_to_mid)
# Merge the sorted halves
return sorted_list
slow = head
fast = head
slow = slow.next
fast = fast.next.next
return slow
merged_list = None
if not left:
return right
if not right:
return left
merged_list = left
else:
merged_list = right
linked_list = LinkedList()
linked_list.append(5)
linked_list.append(3)
linked_list.append(8)
linked_list.append(1)
linked_list.append(7)
linked_list.display()
linked_list.head = linked_list.merge_sort(linked_list.head)
linked_list.display()