Experiment 7
Experiment 7
1.INSERTION
ALGORITHM
Insertion at the Beginning:
1. Create a new node.
2. Point the new node's next pointer to the current head.
3. If the list is not empty, set the prev pointer of the current head to the new node.
4. Update the head of the list to the new node.
1
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return NULL;
}
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
2
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void insertAfter(struct Node* head, int target, int value)
{
struct Node* temp = head;
while (temp != NULL && temp->data != target) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with value %d not found!\n", target);
return;
}
struct Node* newNode = createNode(value);
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
newNode->prev = temp;}
3
void printList(struct Node* head) {
if (head == NULL) {
printf("List is empty!\n");
return;
}
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
printf("Inserting 10 at the beginning:\n");
insertAtBeginning(&head, 10);
printList(head);
printf("Inserting 20 at the beginning:\n");
insertAtBeginning(&head, 20);
printList(head);
printf("Inserting 30 at the end:\n");
insertAtEnd(&head, 30);
printList(head);
printf("Inserting 25 after node with value 20:\n");
insertAfter(head, 20, 25);
printList(head);
return 0;
}
4
OUTPUT:
APPLICATIONS:
1.Navigation Systems (Forward and Backward):
Doubly Linked Lists are ideal for scenarios where you need to move both forward and
backward, such as in browser history or navigation menus.
2. Music or Video Players:
For music or video players where you may need to move both forward and backward through
a playlist, a doubly linked list provides an efficient way to manage the playlist.
2.DELETION
ALGORITHM
Deletion from the Beginning:
1. If the list is empty, print an error message.
2. If the list has only one node, set the head to NULL.
3. If the list has multiple nodes, update the head pointer to point to the second node and set
the prev pointer of the new head to NULL.
Deletion from the End:
1. If the list is empty, print an error message.
2. Traverse to the last node.
3. If the list contains only one node, set the head to NULL.
4. If there are multiple nodes, update the next pointer of the second-to-last node to NULL.
5
Deletion of a Specific Node :
1. Traverse the list to find the node with the specified value.
2. Update the next pointer of the previous node.
3. the previous pointer of the next node (if they exist) to bypass the node being deleted.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
printf("Node deleted from the beginning.\n");
}
void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty.\n");
return; }
6
struct Node* temp = *head;
if (temp->next == NULL) {
*head = NULL;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
}
free(temp);
printf("Node deleted from the end.\n");
}
void deleteFromPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *head;
if (position == 1) {
*head = temp->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
printf("Node deleted from position %d.\n", position);
return;
}
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
7
if (temp == NULL) {
printf("Position out of bounds.\n");
return;
}
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
printf("Node deleted from position %d.\n", position);
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
void push(struct Node** head, int data) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
int main() {
8
struct Node* head = NULL;
push(&head, 10);
push(&head, 20);
push(&head, 30);
printf("Original List: ");
printList(head);
deleteFromBeginning(&head);
printf("After Deletion from Beginning: ");
printList(head);
deleteFromEnd(&head);
printf("After Deletion from End: ");
printList(head);
deleteFromPosition(&head, 2);
printf("After Deletion from Position 2: ");
printList(head);
return 0;
}
OUTPUT:
APPLICATIONS
1. Implementing Queues and Stacks: Doubly linked lists are used in both stacks and queues
for fast insertion and deletion from both ends.
2. File System Management:
Deletion is a common operation in file systems when removing files or directories. The
doubly linked list helps efficiently manage and remove file entries.
Result: The implementation of the programs have been done