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

linked list

The document discusses the differences between arrays and linked lists, highlighting that linked lists consist of nodes that can be stored non-contiguously in memory, allowing for dynamic memory usage. It details the types of linked lists (singly, doubly, and circular), along with operations for insertion, deletion, updating, and copying nodes in both singly and doubly linked lists. Additionally, it provides example code for performing operations on a doubly linked list.

Uploaded by

rajbhagat626
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)
6 views

linked list

The document discusses the differences between arrays and linked lists, highlighting that linked lists consist of nodes that can be stored non-contiguously in memory, allowing for dynamic memory usage. It details the types of linked lists (singly, doubly, and circular), along with operations for insertion, deletion, updating, and copying nodes in both singly and doubly linked lists. Additionally, it provides example code for performing operations on a doubly linked list.

Uploaded by

rajbhagat626
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/ 11

Data Structure UT II | Linked List

1. Difference between Array and Linked List


Array Linked List
Array is a collec on of elements of Linked list is a collec on of objects known
a similar data type. as node.
Array elements store in a Linked list elements can be stored
con guous memory loca on. anywhere in the memory or randomly
stored.
Array works with a sta c memory. Linked list works with dynamic memory.
(memory size is fixed and can not (memory size can be changed at the run
be changed at the run me). me according to our requirements).
Array elements are independent of Linked list elements are dependent on
each other. each other.
- As each node contains the address
of the next node so to access the
next node, we need to access its
previous node.
Array takes more me while Linked list takes less me while
performing any opera on like performing any opera on like inser on,
inser on, dele on, etc. dele on, etc.
Accessing any element in an array Accessing any element in a linked list is
is faster as the element in an array slower as it starts traversing from the first
can be directly accessed through element of the linked list
the index
Example: Example:
2. Define Linked list (write according to marks)
3. Describe the Representa on of linked list (same answer)
- Linked list can be defined as collec on of objects called nodes that are
randomly stored in the memory.
- Example: Representa on of 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;
};

struct Node *createNode(int data) {


struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = newNode->next = NULL;
return newNode;
}

// a. Insert a node in the beginning

struct Node *insertAtBeginning(struct Node *head, int data) {


struct Node *newNode = createNode(data);
if (head) {
head->prev = newNode;
newNode->next = head;
}
return newNode;
}
// b. Delete a node from the end

struct Node *deleteFromEnd(struct Node *head) {


if (!head) {
prin ("List is empty.\n");
return NULL;
}
if (!head->next) {
free(head);
return NULL;
}
struct Node *temp = head;
while (temp->next) temp = temp->next;
temp->prev->next = NULL;
free(temp);
return head;
}

//c. Display the list

void displayList(struct Node *head) {


struct Node *temp = head;
prin ("Doubly Linked List: ");
while (temp) {
prin ("%d -> ", temp->data);
temp = temp->next;
}
prin ("NULL\n");
}
// d. Seach the given element

void searchElement(struct Node *head, int key) {


struct Node *temp = head;
int pos = 1;
while (temp) {
if (temp->data == key) {
prin ("Element %d found at posi on %d.\n", key, pos);
return;
}
temp = temp->next;
pos++;
}
prin ("Element %d not found in the list.\n", key);
}

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;
}

You might also like