Doubly Linked List implementation in C
Doubly linked list is a type of linked list. It can traverse in both directions. The operations of doubly linked list are given below.
- · Creation of node.
- · Insert the data at the beginning.
- · Insert the data at the end.
- · Delete the data at the beginning.
- · Delete the data at the end.
- · Delete a data based on the value.
- · Traverse data in the forward direction.
- · Traverse data in the backward direction.
C Code:
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a doubly linked list node
struct Node {
int data;
struct Node*
l_prev;
struct Node*
l_next;
};
// Creation of a new node
struct Node* createNode(int value) {
struct Node*
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data =
value;
newNode->l_prev
= NULL;
newNode->l_next
= NULL;
return newNode;
}
// traverse in forward
void traverse_Forward(struct Node* head) {
struct Node* temp
= head;
printf("Forward->: ");
while (temp !=
NULL) {
printf("%d <-> ", temp->data);
temp =
temp->l_next;
}
printf("NULL\n");
}
// traverse in backward
void traverse_Backward(struct Node* tail) {
struct Node* temp
= tail;
printf("Backward <---: ");
while (temp !=
NULL) {
printf("%d <-> ", temp->data);
temp =
temp->l_prev;
}
printf("NULL\n");
}
// Insert the data in the beginning
struct Node* insertAt_Beginning(struct Node* head, int
value) {
struct Node*
newNode = createNode(value);
if (head != NULL)
{
newNode->l_next = head;
head->l_prev = newNode;
}
return newNode; //
new head
}
// Insert the data at the end
struct Node* insertAt_End(struct Node* head, int value) {
struct Node*
newNode = createNode(value);
if (head == NULL)
return newNode;
while
(temp->l_next != NULL) {
temp =
temp->l_next;
}
temp->l_next =
newNode;
newNode->l_prev
= temp;
return head;
}
// Delete the data from beginning
struct Node* delete_FromBeginning(struct Node* head) {
if (head == NULL)
return NULL;
struct Node* temp
= head;
head =
head->l_next;
if (head != NULL)
head->l_prev = NULL;
free(temp);
return head;
}
// Delete the data from end
struct Node* delete_FromEnd(struct Node* head) {
if (head == NULL)
return NULL;
struct Node* temp
= head;
while
(temp->l_next != NULL) {
temp =
temp->l_next;
}
if
(temp->l_prev != NULL) {
temp->l_prev->l_next = NULL;
} else {
head = NULL;
// list had only one node
}
free(temp);
return head;
}
// Delete a node by using the value
struct Node* delete_ByValue(struct Node* head, int value) {
struct Node* temp
= head;
while (temp !=
NULL && temp->data != value) {
temp =
temp->l_next;
}
if (temp == NULL)
return head; // value not found
if
(temp->l_prev != NULL) temp->l_prev->l_next = temp->l_next;
else head =
temp->l_next; // deleting head
if
(temp->l_next != NULL) temp->l_next->l_prev = temp->l_prev;
free(temp);
return head;
}
int main() {
struct Node* head
= NULL;
// Insert nodes
head =
insertAt_End(head, 100);
head =
insertAt_End(head, 120);
head =
insertAt_End(head, 31);
head =
insertAt_End(head, 43);
head =
insertAt_End(head, 90);
traverse_Forward(head);
// Insert the data
at beginning
head =
insertAt_Beginning(head, 5);
traverse_Forward(head);
// Delete the data
from beginning
head =
delete_FromBeginning(head);
traverse_Forward(head);
// Delete the data
from end
head =
delete_FromEnd(head);
traverse_Forward(head);
// Delete the node
by value
head =
delete_ByValue(head, 20);
traverse_Forward(head);
return 0;
}
Output:
Forward->: 100 <-> 120 <-> 31 <-> 43
<-> 90 <-> NULL
Forward->: 5 <-> 100 <-> 120 <-> 31
<-> 43 <-> 90 <-> NULL
Forward->: 100 <-> 120 <-> 31 <-> 43
<-> 90 <-> NULL
Forward->: 100 <-> 120 <-> 31 <-> 43
<-> NULL
Forward->: 100 <-> 120 <-> 31 <-> 43
<-> NULL
This is the way of implementing Doubly Linked list in C.
Hope, you understood it. Keep Coding!!!
Comments
Post a Comment