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

Lab Report 5 Ds

The document describes a student's implementation of a linked list that allows inserting nodes at different positions. Key points: 1. The student created functions to insert nodes at the beginning, end, or anywhere in the linked list. 2. Inserting at the beginning has O(1) time complexity while inserting at the end or a specific position takes O(n) time for singly linked lists. 3. The implementation allows dynamically managing data through insertion and removal of nodes, making linked lists useful for various applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Lab Report 5 Ds

The document describes a student's implementation of a linked list that allows inserting nodes at different positions. Key points: 1. The student created functions to insert nodes at the beginning, end, or anywhere in the linked list. 2. Inserting at the beginning has O(1) time complexity while inserting at the end or a specific position takes O(n) time for singly linked lists. 3. The implementation allows dynamically managing data through insertion and removal of nodes, making linked lists useful for various applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Green University of Bangladesh

Department of Computer Science and


Engineering(CSE) Faculty of Sciences and Engineering
Semester: (Fall, Year:2023), B.Sc. in CSE (Day)

LAB REPORT NO # 05
Course Title: Data Structure LAB
Course Code: CSE 106 Section: D5

Lab Experiment Name: Implement a linked list for inserting a node.

Student Details
Name ID

1. Taj Uddin 221002622

Lab Date : 31-10-2023


Submission Date : 07-11-2023
Course Teacher’s Name : Ms. Farhana Akter Sunny

[For Teachers use only: Don’t Write Anything inside this box]
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................

1. TITLE OF THE LAB EXPERIMENT


• Implement a linked list for inserting a node

2. OBJECTIVES
● To implement a linked list data structure in a programming language of choice and create
a function for inserting a new node at a specified position within the linked list.
3. PROCEDURE

● Algorithm
Algorithm InsertNode(LinkedList, newNode, position):
Input:
- LinkedList: The linked list in which to insert the node.
- newNode: The node to be inserted.
- position: The position (index) at which to insert the node.

Output:
- LinkedList with the new node inserted at the specified position.

● Problem : Implement a linked list for inserting a node in.

1. Beginning 2. End 3. Anywhere

● Code
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the linked list


struct Node {
int data;
struct Node* next;
};

// Function to insert a new node at the beginning of the linked list


struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
return newNode;
}

// Function to insert a new node at the end of the linked list


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
return newNode;
}

struct Node* current = head;


while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
return head;
}

// Function to insert a new node at a specific position in the linked list


struct Node* insertAnywhere(struct Node* head, int data, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;

if (position == 1) {
newNode->next = head;
return newNode;
}

struct Node* current = head;


int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}

if (current == NULL) {
printf("Invalid position for insertion.\n");
free(newNode);
return head;
}

newNode->next = current->next;
current->next = newNode;

return head;
}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
int choice, data, position;

while (1) {
printf("Linked List Menu:\n");
printf("1. Insert at the Beginning\n");
printf("2. Insert at the End\n");
printf("3. Insert Anywhere\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the data to insert at the beginning: ");
scanf("%d", &data);
head = insertAtBeginning(head, data);
break;
case 2:
printf("Enter the data to insert at the end: ");
scanf("%d", &data);
head = insertAtEnd(head, data);
break;
case 3:
printf("Enter the data to insert: ");
scanf("%d", &data);
printf("Enter the position for insertion: ");
scanf("%d", &position);
head = insertAnywhere(head, data, position);
break;
case 4:
printf("Linked List: ");
displayList(head);
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}
Output :
4. IMPLEMENTATION
1. Create a temporary variable currentNode and set it to the head of the linked list.

2. Create a variable currentPosition and initialize it to 1 (the index of the first node).

3. If position is 1, insert the new node as the new head of the linked list:

- Set newNode.next to the current head of the linked list.

- Update the head of the linked list to newNode.

4. Otherwise, traverse the linked list until you reach the node at the position just before the
desired insertion point:

- Move currentNode to currentNode.next and increment currentPosition by 1 until


currentPosition is one less than the desired position.

5. Insert the new node into the linked list at the specified position:

- Set newNode.next to currentNode.next.

- Set currentNode.next to newNode.

6. ANALYSIS AND DISCUSSION

Analysis and Discussion:


Data Structure Overview:
A linked list is a data structure that consists of nodes, where each node contains a data element
and a reference (or a pointer) to the next node in the list. The first node is called the head, and
the last node typically points to NULL, indicating the end of the list.

Insertion Methods:
There are several ways to insert a node into a linked list:

a. Insertion at the Beginning:


This method involves adding a new node at the front of the list. It's efficient because it only
requires updating the reference of the new node to point to the current head and changing the
head to the new node.
b. Insertion at the End:
In this case, you add a new node to the end of the list. This operation is generally less efficient
for singly-linked lists because you have to traverse the entire list to find the last node, but it's
efficient for doubly-linked lists.

c. Insertion at a Specific Position:


You can insert a new node at a specific position within the list. This operation requires
traversing the list until you reach the desired position and updating the references accordingly.

Time Complexity:
The time complexity of insertion in a linked list depends on where the insertion occurs:

Insertion at the beginning (prepending) has a time complexity of O(1) since it involves only a
few pointer updates.
Insertion at the end (appending) has a time complexity of O(n) for singly-linked lists and O(1)
for doubly-linked lists.
Insertion at a specific position takes O(n) in the worst case as it requires traversing the list to
find the insertion point.
Advantages:

Dynamic Size: Linked lists can easily grow or shrink as elements are inserted or removed.
Efficient for Insertions: Inserting an element in the middle of a linked list is efficient compared
to arrays since you don't need to shift existing elements.
Disadvantages:

Inefficient Random Access: Accessing elements by index is less efficient compared to arrays, as
you must traverse the list.
Higher Memory Overhead: Each node requires memory for both data and a reference, making
linked lists less memory-efficient than arrays.
In summary, implementing a linked list for inserting nodes involves considering the trade-offs
between different insertion methods, and the choice depends on the specific use case. Linked
lists are valuable for scenarios where dynamic sizing and efficient insertions are critical, but they
may not be the best choice for random access or scenarios where memory usage is a concern.
7. SUMMARY:
I've implemented a linked list data structure for inserting nodes. A linked list is a linear data
structure where each node contains a data element and a reference (or link) to the next node in
the sequence. In my implementation, you can insert new nodes at various positions within the
linked list. The basic operations supported by this implementation include:

Insertion at the beginning: You can add a new node at the front of the linked list, making it the
new head.

Insertion at the end: You can append a node to the end of the linked list.

Insertion at a specific position: You can insert a node at a specified position in the list, shifting
other nodes accordingly.

This linked list allows you to dynamically manage and manipulate your data, making it a
fundamental data structure for various applications. It's important to note that I can provide you
with code examples or further details on how to use and implement the linked list if needed.

You might also like