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

Doubly Linked List

Uploaded by

adityaagarwal388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Doubly Linked List

Uploaded by

adityaagarwal388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Program-01 Creating a Doubly Linked List and Printing the elements from forward & backward

#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1); // Exit if memory allocation fails
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

// Function to insert a node at the end of the list


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode; // If the list is empty, the new node is the head
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
newNode->prev = temp;
return head;
}

// Function to traverse the list forward and print the data of each node
void traverseForward(struct Node* head) {
struct Node* temp = head;
printf("Forward: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to traverse the list backward and print the data of each node
void traverseBackward(struct Node* tail) {
struct Node* temp = tail;
printf("Backward: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}

// Function to free the memory allocated for the doubly linked list
void freeList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
struct Node* next = temp->next;
free(temp);
temp = next;
}
}

int main() {
struct Node* head = NULL; // Initialize the list to NULL
struct Node* tail = NULL; // Initialize the tail to NULL

// Insert nodes at the end


head = insertAtEnd(head, 10);
tail = head; // Update tail after inserting first node
head = insertAtEnd(head, 20);
head = insertAtEnd(head, 30);
head = insertAtEnd(head, 40);
tail = head->next->next->next; // Update tail after inserting all nodes

// Traverse and print the doubly linked list


traverseForward(head);
traverseBackward(tail);

// Free memory allocated for the doubly linked list


freeList(head);

return 0;
}
Program-02 Deletion in Doubly linked list
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1); // Exit if memory allocation fails
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

// Function to insert a node at the end of the list


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode; // If the list is empty, the new node is the head
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
newNode->prev = temp;
return head;
}

// Function to delete a node with a specific value


struct Node* deleteNode(struct Node* head, int key) {
struct Node* temp = head;

// If the head node itself holds the key to be deleted


if (temp != NULL && temp->data == key) {
head = temp->next; // Change head
if (head != NULL) {
head->prev = NULL;
}
free(temp); // Free old head
return head;
}

// Search for the key to be deleted


while (temp != NULL && temp->data != key) {
temp = temp->next;
}
// If the key was not present in the list
if (temp == NULL) {
printf("Key not found\n");
return head;
}

// Unlink the node from the list


if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}

free(temp); // Free memory


return head;
}

// Function to traverse the list forward and print the data of each node
void traverseForward(struct Node* head) {
struct Node* temp = head;
printf("Forward: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to free the memory allocated for the doubly linked list
void freeList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
struct Node* next = temp->next;
free(temp);
temp = next;
}
}

int main() {
struct Node* head = NULL; // Initialize the list to NULL

// Insert nodes at the end


head = insertAtEnd(head, 10);
head = insertAtEnd(head, 20);
head = insertAtEnd(head, 30);
head = insertAtEnd(head, 40);

// Traverse and print the doubly linked list


printf("Original List:\n");
traverseForward(head);

// Delete a node and print the list


head = deleteNode(head, 20);
printf("After Deleting 20:\n");
traverseForward(head);

// Free memory allocated for the doubly linked list


freeList(head);
return 0;
}
Program-03 Insertion in Doubly linked list at the beginning, end and at the any
specific position (in middle)

#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1); // Exit if memory allocation fails
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

// Function to insert a node at the beginning of the list


struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode; // If the list is empty, the new node is the head
}
newNode->next = head;
head->prev = newNode;
return newNode;
}

// Function to insert a node at the end of the list


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode; // If the list is empty, the new node is the head
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
return head;
}

// Function to insert a node at a specific position in the list


struct Node* insertAtPosition(struct Node* head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 1) {
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
return newNode;
}
struct Node* temp = head;
int currentPosition = 1;
while (temp != NULL && currentPosition < position - 1) {
temp = temp->next;
currentPosition++;
}
if (temp == NULL) {
printf("Invalid position\n");
free(newNode);
return head;
}
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
return head;
}

// Function to traverse the list forward and print the data of each node
void traverseForward(struct Node* head) {
struct Node* temp = head;
printf("Forward: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to free the memory allocated for the doubly linked list
void freeList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
struct Node* next = temp->next;
free(temp);
temp = next;
}
}

int main() {
struct Node* head = NULL; // Initialize the list to NULL

// Insert nodes at the beginning, end, and specific positions


head = insertAtBeginning(head, 30); // List: 30
head = insertAtBeginning(head, 20); // List: 20 -> 30
head = insertAtBeginning(head, 10); // List: 10 -> 20 -> 30
head = insertAtEnd(head, 40); // List: 10 -> 20 -> 30 -> 40
head = insertAtEnd(head, 50); // List: 10 -> 20 -> 30 -> 40 -> 50
head = insertAtPosition(head, 25, 3); // List: 10 -> 20 -> 25 -> 30 -> 40 -> 50
head = insertAtPosition(head, 5, 1); // List: 5 -> 10 -> 20 -> 25 -> 30 -> 40 -> 50

// Traverse and print the doubly linked list forward


traverseForward(head);

// Free memory allocated for the doubly linked list


freeList(head);
return 0;
}

You might also like