double linked list
double linked list
h>
#include <stdlib.h>
// 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;
}
if (position == 1) {
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
return;
}
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;
}
// 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;
}
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;
}
if (current->prev != NULL) {
current->prev->next = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
current = current->next;
position++;
}
printf("NULL\n");
}
// Function to free the memory allocated for the doubly linked list
void freeList(Node* head) {
Node* current = head;
Node* nextNode;
int main() {
Node* head = NULL;
return 0;
}