0% found this document useful (0 votes)
5 views4 pages

Doubly Linked List

A doubly linked list consists of nodes that contain data and pointers to both the previous and next nodes, allowing traversal in both directions. The document includes C code for implementing a doubly linked list with functions to create nodes, insert at the beginning and end, delete from the beginning and end, and print the list in both forward and backward directions. The main function demonstrates the usage of these operations.
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)
5 views4 pages

Doubly Linked List

A doubly linked list consists of nodes that contain data and pointers to both the previous and next nodes, allowing traversal in both directions. The document includes C code for implementing a doubly linked list with functions to create nodes, insert at the beginning and end, delete from the beginning and end, and print the list in both forward and backward directions. The main function demonstrates the usage of these operations.
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/ 4

Doubly Linked List:

● It consists of an information field and two pointer fields. The information field contains
the data. The first pointer field contains an address of the previous node, whereas another
pointer field contains a reference to the next node. Thus, we can go in both directions
(backward as well as forward).
● In a singly linked list, you can only move in one direction because each node only has the
address of the next node and doesn't keep track of the previous ones. However, a doubly
linked list solves this problem. Each node in a doubly linked list has the address of both the
next and the previous node. This means you can move forward and backward through the
list, making it easier to find and use information about previous nodes.
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


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

Node* head = NULL;


Node* tail = NULL;

// Create a new node


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

// Insert at beginning
void insertAtBeginning(int data) {
Node* newNode = createNode(data);
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}

// Insert at end
void insertAtEnd(int data) {
Node* newNode = createNode(data);
if (tail == NULL) {
head = newNode;
tail = newNode;
} else {
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
}
}

// Delete from beginning


void deleteFromBeginning() {
if (head == NULL) return;
Node* temp = head;
head = head->next;
if (head != NULL) head->prev = NULL;
else tail = NULL;
free(temp);
}

// Delete from end


void deleteFromEnd() {
if (tail == NULL) return;
Node* temp = tail;
tail = tail->prev;
if (tail != NULL) tail->next = NULL;
else head = NULL;
free(temp);
}

// Print forward
void printForward() {
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Print backward
void printBackward() {
Node* temp = tail;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
}

int main() {
insertAtEnd(1);
insertAtEnd(2);
insertAtEnd(3);
insertAtBeginning(0);
insertAtBeginning(-1);

printf("Forward: ");
printForward();
printf("Backward: ");
printBackward();

deleteFromBeginning();
deleteFromEnd();

printf("After deletion (Forward): ");


printForward();

printf("After deletion (Backward): ");


printBackward();

return 0;
}

You might also like