0% found this document useful (0 votes)
3 views5 pages

Doubly Linked List Algorithms

The document provides algorithms and code snippets for basic operations on a doubly linked list, including insertion at the beginning and end, deletion from the beginning and end, and traversal in both forward and backward directions. Each operation is accompanied by a clear algorithm and corresponding C code implementation. The focus is on manipulating nodes and maintaining the correct pointers for a doubly linked structure.
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)
3 views5 pages

Doubly Linked List Algorithms

The document provides algorithms and code snippets for basic operations on a doubly linked list, including insertion at the beginning and end, deletion from the beginning and end, and traversal in both forward and backward directions. Each operation is accompanied by a clear algorithm and corresponding C code implementation. The focus is on manipulating nodes and maintaining the correct pointers for a doubly linked structure.
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/ 5

Doubly Linked List - Algorithms and Code Snippets

Insert at Beginning

Algorithm:
1. Create a new node.
2. Set newNode->next to head.
3. Set newNode->prev to NULL.
4. If head is not NULL, set head->prev = newNode.
5. Set head = newNode.

Code:
void insertAtBeginning(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;

if (head != NULL)
head->prev = newNode;

head = newNode;
}

Insert at End

Algorithm:
1. Create a new node.
2. If list is empty, set head = newNode.
3. Else, traverse to last node.
4. Set last->next = newNode, newNode->prev = last.

Code:
void insertAtEnd(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
Node* temp = head;

newNode->data = data;
newNode->next = NULL;

if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}

while (temp->next != NULL)


temp = temp->next;

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

Delete from Beginning

Algorithm:
1. If list is empty, return.
2. Store head in temp.
3. Move head to head->next.
4. Free temp.

Code:
void deleteFromBeginning() {
if (head == NULL) return;

Node* temp = head;


head = head->next;
if (head != NULL)
head->prev = NULL;

free(temp);
}

Delete from End

Algorithm:
1. If list is empty, return.
2. If only one node, free it and set head to NULL.
3. Else, traverse to last node.
4. Update secondLast->next = NULL.
5. Free last node.

Code:
void deleteFromEnd() {
if (head == NULL) return;

if (head->next == NULL) {
free(head);
head = NULL;
return;
}

Node* temp = head;


while (temp->next != NULL)
temp = temp->next;

temp->prev->next = NULL;
free(temp);
}
Traversal (Forward)

Algorithm:
1. Start from head.
2. Print data while moving through next.

Code:
void displayForward() {
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}

Traversal (Backward)

Algorithm:
1. Go to last node.
2. Print data while moving through prev.

Code:
void displayBackward() {
Node* temp = head;
if (temp == NULL) return;

while (temp->next != NULL)


temp = temp->next;

while (temp != NULL) {


printf("%d ", temp->data);
temp = temp->prev;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy