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

343330PYTHON_EXP 6

The document outlines an experiment focused on implementing a menu-driven program for a linked list data structure using Python. It covers the theory behind linked lists, including their advantages over arrays, basic operations like traversal, insertion, and deletion, and provides a practical implementation example. Additionally, it includes sections for students to complete their work and reflect on their learning outcomes.

Uploaded by

vedantshimpi3114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

343330PYTHON_EXP 6

The document outlines an experiment focused on implementing a menu-driven program for a linked list data structure using Python. It covers the theory behind linked lists, including their advantages over arrays, basic operations like traversal, insertion, and deletion, and provides a practical implementation example. Additionally, it includes sections for students to complete their work and reflect on their learning outcomes.

Uploaded by

vedantshimpi3114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PART A

(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.06

A.1 Aim:

To implement menu driven program for Linked list data structure


A.2 Prerequisite:
1. python basics and control structures

A.3 Outcome:
After successful completion of this experiment students will be able to

To demonstrate basic concepts in python looping.

A.4 Theory& Procedure:

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

Why 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.

Insertion of a new element / Deletion of a existing element in an array of elements is expensive:


The room has to be created for the new elements and to create room existing elements have to be
shifted but in Linked list if we have the head node then we can traverse to any node through it
and insert new node at the required position.

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.

Traversal - access each element of the linked list

Insertion - adds a new element to the linked list

Deletion - removes the existing elements

Before you learn about linked list operations in detail, make sure to know about Linked List first.

Things to Remember about Linked List

head points to the first node of the linked list

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;

struct node *next;

};

Traverse a Linked List

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.

struct node *temp = head;

printf("\n\nList elements are - \n");

while(temp != NULL) {
printf("%d --->",temp->data);

temp = temp->next;

The output of this program will be:

List elements are -

1 --->2 --->3 --->

Insert Elements to a Linked List

You can add elements to either the beginning, middle or end of the linked list.

1. Insert at the beginning

Allocate memory for new node

Store data

Change next of new node to point to head

Change head to point to recently created node

struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = 4;

newNode->next = head;

head = newNode;

2. Insert at the End

Allocate memory for new node

Store data

Traverse to last node


Change next of last node to recently created node

struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = 4;

newNode->next = NULL;

struct node *temp = head;

while(temp->next != NULL){

temp = temp->next;

temp->next = newNode;

3. Insert at the Middle

Allocate memory and store data for new node

Traverse to node just before the required position of new node

Change next pointers to include new node in between

struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = 4;

struct node *temp = head;

for(int i=2; i < position; i++) {

if(temp->next != NULL) {

temp = temp->next;
}

newNode->next = temp->next;

temp->next = newNode;

Delete from a Linked List

You can delete either from the beginning, end or from a particular position.

1. Delete from beginning

Point head to the second node

head = head->next;

2. Delete from end

Traverse to second last element

Change its next pointer to null

struct node* temp = head;

while(temp->next->next!=NULL){

temp = temp->next;

temp->next = NULL;

3. Delete from middle

Traverse to element before the element to be deleted

Change next pointers to exclude the node from the chain

for(int i=2; i< position; i++) {

if(temp->next!=NULL) {

temp = temp->next;

}
}

temp->next = temp->next->next;

PART B

(PART B : TO BE COMPLETED BY STUDENTS)

(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)

Roll No.: B62 Name: Sanket Mangesh Warghade

Class :SE-B Batch :B3

Date of Experiment: Date of Submission

Grade :

B.1 Document created by the student:

1. Implement Linked list program for insertion and deletion oprations.


Code:
Output:
B.3 Observations and learning:

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:

We learned to demonstrate basic concepts in python looping.

B.5 Question of Curiosity

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:

def init (self, data):

self.data = data

self.next = None

class LinkedList:

def init (self):

self.head = None

def append(self, data):

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:

print(current.data, end=" -> ")

current = current.next

print("None")

def merge_sort(self, head):

if not head or not head.next:

return head

# Split the linked list into halves

mid = self.find_middle(head)

next_to_mid = mid.next

mid.next = None

# Recursively sort the two halves

left_half = self.merge_sort(head)

right_half = self.merge_sort(next_to_mid)
# Merge the sorted halves

sorted_list = self.merge(left_half, right_half)

return sorted_list

def find_middle(self, head):

slow = head

fast = head

while fast.next and fast.next.next:

slow = slow.next

fast = fast.next.next

return slow

def merge(self, left, right):

merged_list = None

if not left:

return right

if not right:

return left

if left.data <= right.data:

merged_list = left

merged_list.next = self.merge(left.next, right)

else:

merged_list = right

merged_list.next = self.merge(left, right.next)


return merged_list

# Create a linked list

linked_list = LinkedList()

linked_list.append(5)

linked_list.append(3)

linked_list.append(8)

linked_list.append(1)

linked_list.append(7)

print("Original Linked List:")

linked_list.display()

# Sort the linked list using Merge Sort

linked_list.head = linked_list.merge_sort(linked_list.head)

print("\nSorted Linked List:")

linked_list.display()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy