Lab Report 5 Ds
Lab Report 5 Ds
LAB REPORT NO # 05
Course Title: Data Structure LAB
Course Code: CSE 106 Section: D5
Student Details
Name ID
[For Teachers use only: Don’t Write Anything inside this box]
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
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.
● Code
#include <stdio.h>
#include <stdlib.h>
if (position == 1) {
newNode->next = head;
return newNode;
}
if (current == NULL) {
printf("Invalid position for insertion.\n");
free(newNode);
return head;
}
newNode->next = current->next;
current->next = newNode;
return head;
}
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:
4. Otherwise, traverse the linked list until you reach the node at the position just before the
desired insertion point:
5. Insert the new node into the linked list at the specified position:
Insertion Methods:
There are several ways to insert a node into a linked list:
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.