Name of student: Rayan Jain
UID: 2023800036
Batch: CSE A BATCH C
Exp.No. 4
Aim: Double Linked List application
Problem:
1. Create a doubly Linked List. All possible test cases to be considered
2. Deletion of a given node. All possible test cases to be considered
3. Handwritten task is to show hand-run stepwise implementation of Doubly lined List of
all possible test cases
Program in text:
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node in a doubly linked list.
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node with the given data
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to insert a node at the beginning of the doubly linked list
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head != NULL) {
newNode->next = *head;
(*head)->prev = newNode;
}
*head = newNode;
}
// Function to insert a node at the end of the doubly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
struct Node* temp = *head;
if (*head == NULL) {
*head = newNode;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
// Function to insert a node after a given position
void insertAfter(struct Node* prevNode, int data) {
if (prevNode == NULL) {
printf("Previous node cannot be NULL\n");
return;
}
struct Node* newNode = createNode(data);
newNode->next = prevNode->next;
newNode->prev = prevNode;
if (prevNode->next != NULL) {
prevNode->next->prev = newNode;
}
prevNode->next = newNode;
}
// Function to delete a node from the doubly linked list
void deleteNode(struct Node** head, struct Node* delNode) {
if (*head == delNode) {
*head = delNode->next;
}
if (delNode->next != NULL) {
delNode->next->prev = delNode->prev;
}
if (delNode->prev != NULL) {
delNode->prev->next = delNode->next;
}
free(delNode);
}
// Function to delete a node at a given position (1-based index)
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL || position <= 0) {
printf("Invalid position!\n");
return;
}
struct Node* temp = *head;
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
deleteNode(head, temp);
}
// Function to traverse the list forward
void traverseForward(struct Node* head) {
struct Node* temp = head;
printf("List (forward): ");
if (head == NULL) {
printf("Empty list\n");
return;
}
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int choice, data, position;
do {
printf("\n1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert After Position\n");
printf("4. Delete at Position\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
traverseForward(head);
break;
case 2:
printf("Enter the value to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
traverseForward(head);
break;
case 3:
printf("Enter the position after which to insert: ");
scanf("%d", &position);
printf("Enter the value to insert: ");
scanf("%d", &data);
// Find the node after which to insert
struct Node* temp = head;
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range!\n");
} else {
insertAfter(temp, data);
}
traverseForward(head);
break;
case 4:
printf("Enter the position of the node to delete: ");
scanf("%d", &position);
deleteAtPosition(&head, position);
traverseForward(head);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
break;
}
} while (choice != 5);
return 0;
}
Observations:
The code implements a doubly linked list where each node contains data, next, and prev pointers. It
supports user-driven operations like inserting at the beginning, end, or after a specified position and
deleting a node at a given position. The program efficiently updates both next and prev pointers. Users
can add or remove nodes, and the list can be displayed after each operation.
Output:
Handwritten assignment:
Step-wise explanation of the given code.