linked list
linked list
- A node contains two fields that is data stored at the par cular address
and the pointer which contains the address of the next node in the
memory.
- The last node of the list contains a pointer to the null.
- The list is not required to be con guously present in the memory. The
node can reside anywhere in the memory and linked together to make a
list. This achieves op mized u liza on of space.
- List size is limited to the memory size and doesn’t need to be declared in
advance.
- Empty node cannot be present in the linked list.
4. Explain in detail, types of linked list
There are three types of linked list that are:
i. Singly linked list
ii. Doubly linked list
iii. Circular Linked list
A. Singly Linked List:
B. Doubly Linked list:
C. Circular Linked list:
5. Write opera on for along with descrip on:
a. Inser on and dele on with singly linked list.
b. Inser on and dele on with doubly linked list.
Inser on and Dele on in Singly Linked List:
Inser on:
1. At the Beginning – A new node is inserted at the front by adjus ng
the head pointer.
2. At the End – The last node’s next pointer is updated to link to the
new node.
3. A er a Specified Node – The list is traversed to find the target node,
and pointers are adjusted to insert the new node.
Dele on:
1. At the Beginning – The head is updated to the next node, removing
the first node.
2. At the End – The second-last node’s next is set to NULL, dele ng the
last node.
3. A er a Specified Node – The list is traversed, and pointers are
adjusted to bypass the target node.
Inser on and Dele on in Doubly Linked List:
Inser on:
1. At the Beginning – The new node’s next points to the head, and the
head’s prev is updated.
2. At the End – The last node’s next is updated, and the new node’s
prev is set accordingly.
3. A er a Specified Node – Both prev and next pointers are updated to
insert the new node.
Dele on:
1. At the Beginning – The head is updated, and its prev is set to NULL.
2. At the End – The second-last node’s next is set to NULL.
3. Dele ng a Specific Node – The node’s prev and next pointers are
adjusted to remove it efficiently.
6. Write in detail about steps used in update and copy opera ons with
singly linked list.
Update and Copy Opera ons in a Singly Linked List.
- The update opera on modifies the value of a node without altering its
structure
- The copy opera on creates a new independent replica of the list.
Steps in Update Opera on:
1. Traverse the List – Start from the head node and move through each
node using the next pointer.
2. Compare Values – Check if the current node’s value matches the target
value that needs to be updated.
3. Update the Value – Replace the current node’s value with the new
value once found.
4. Stop Traversal – The traversal stops when the value is updated or the
end of the list is reached.
Steps in Copy Opera on:
1. Create a New List – Ini alize an empty singly linked list.
2. Traverse the Original List – Start from the head node and iterate
through each node.
3. Replicate Each Node – For each node in the original list, create a new
node with the same value and append it to the new list.
4. Link Nodes – Set the next pointers in the new list to maintain the same
sequence as the original list.
7. Write in detail about steps used in update and copy opera ons with
doubly linked list.
Update and Copy Opera ons in a Doubly Linked List
- The update opera on modifies a node’s value.
- The copy opera on creates a new independent replica of the list.
Steps in Update Opera on:
1. Traverse the List – Start from the head node and move through each node
using the next pointer.
2. Compare Values – Check if the current node’s value matches the target
value to be updated.
3. Update the Value – Replace the current node’s value with the new value
once found.
4. Stop Traversal – The traversal stops when the value is updated or the end
of the list is reached.
Steps in Copy Opera on:
1. Create a New List – Ini alize an empty doubly linked list.
2. Traverse the Original List – Start from the head node and iterate through
all nodes using the next pointer.
3. Replicate Each Node – Create a new node for each node in the original list
with the same value.
o Link the next and prev pointers to maintain the structure.
4. Return the New List – Return the head of the copied list.
8. Write Programs to perform the following opera ons on doubly linked list:
a. Insert a node in the beginning
b. Delete a node from the end
c. Search for a given element in the list
d. Display the list
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node {
int data;
struct Node *prev, *next;
};
int main() {
struct Node *head = NULL;
int choice, value;
clrscr();
do {
prin ("\n1.Insert at Beginning 2.Delete from End 3.Search 4.Display
5.Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: prin ("Enter value: "); scanf("%d", &value); head =
insertAtBeginning(head, value); break;
case 2: head = deleteFromEnd(head); break;
case 3: prin ("Enter element: "); scanf("%d", &value);
searchElement(head, value); break;
case 4: displayList(head); break;
case 5: prin ("Exi ng program.\n"); break;
default: prin ("Invalid choice. Try again.\n");
}
} while (choice != 5);
getch();
return 0;
}