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

double linked list

The document contains a C program that implements a doubly linked list with various functionalities including insertion, deletion, searching, and displaying elements. It defines a Node structure and provides functions to manipulate the linked list, such as inserting at the start, end, or a specific position, and deleting from those locations. The main function demonstrates these operations and manages memory allocation and deallocation.

Uploaded by

gntpv4
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 views4 pages

double linked list

The document contains a C program that implements a doubly linked list with various functionalities including insertion, deletion, searching, and displaying elements. It defines a Node structure and provides functions to manipulate the linked list, such as inserting at the start, end, or a specific position, and deleting from those locations. The main function demonstrates these operations and manages memory allocation and deallocation.

Uploaded by

gntpv4
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

#include <stdio.

h>
#include <stdlib.h>

// Define a node structure


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

// Function to create a new node


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

// Function to insert a new node at the beginning of the doubly linked list
void insertAtStart(Node** head, int value) {
Node* newNode = createNode(value);

if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}

// Function to insert a new node at the end of the doubly linked list
void insertAtEnd(Node** head, int value) {
Node* newNode = createNode(value);

if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}

// Function to insert a new node at any position in the doubly linked list
void insertAtPosition(Node** head, int value, int position) {
if (position < 1) {
printf("Invalid position.\n");
return;
}

Node* newNode = createNode(value);

if (position == 1) {
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
return;
}

Node* current = *head;


int count = 1;

while (count < position - 1) {


if (current == NULL) {
printf("Invalid position.\n");
return;
}
current = current->next;
count++;
}

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

// Function to delete a node from the beginning of the doubly linked list
void deleteAtStart(Node** head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}

Node* temp = *head;


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

// Function to delete a node from the end of the doubly linked list
void deleteAtEnd(Node** head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}

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

Node* current = *head;


while (current->next != NULL) {
current = current->next;
}

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

free(current);
}

// Function to delete a node from any position in the doubly linked list
void deleteAtPosition(Node** head, int position) {
if (*head == NULL || position < 1) {
printf("Invalid position.\n");
return;
}

if (position == 1) {
Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
return;
}

Node* current = *head;


int count = 1;

while (count < position - 1) {


if (current == NULL) {
printf("Invalid position.\n");
return;
}
current = current->next;
count++;
}

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

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

// Function to search for an element in the doubly linked list


void searchElement(Node* head, int value) {
if (head == NULL) {
printf("List is empty.\n");
return;
}

Node* current = head;


int position = 1;

while (current != NULL) {


if (current->data == value) {
printf("%d found at position %d.\n", value, position);
return; // Element found
}

current = current->next;
position++;
}

printf("%d not found in the list.\n", value);


}

// Function to display the elements of the doubly linked list


void displayList(Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}

Node* current = head;

while (current != NULL) {


printf("%d <-> ", current->data);
current = current->next;
}

printf("NULL\n");
}

// Function to free the memory allocated for the doubly linked list
void freeList(Node* head) {
Node* current = head;
Node* nextNode;

while (current != NULL) {


nextNode = current->next;
free(current);
current = nextNode;
}
}

int main() {
Node* head = NULL;

// Insert elements at the beginning


insertAtStart(&head, 10);
insertAtStart(&head, 20);
insertAtStart(&head, 30);

// Display the list


printf("Original List: ");
displayList(head);

// Insert element at the end


insertAtEnd(&head, 40);
printf("List after inserting 40 at the end: ");
displayList(head);

// Insert element at any position


insertAtPosition(&head, 25, 2);
printf("List after inserting 25 at position 2: ");
displayList(head);

// Delete element from the start


deleteAtStart(&head);
printf("List after deleting from the start: ");
displayList(head);

// Delete element from the end


deleteAtEnd(&head);
printf("List after deleting from the end: ");
displayList(head);

// Delete element from any position


deleteAtPosition(&head, 1);
printf("List after deleting from position 1: ");
displayList(head);

// Search for an element


searchElement(head, 25);

// Free the memory


freeList(head);

return 0;
}

You might also like