Linked List
Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = newData;
newNode->next = head;
head = newNode;
return head;
temp = temp->next;
prin ("NULL\n");
int main() {
printList(head);
return 0;
}
C code snippet to add a node at the beginning of a doubly linked list:
Copy
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = newData;
// If the list is not empty, update the previous pointer of the exis ng head
if (head != NULL) {
head->prev = newNode;
head = newNode;
return head;
temp = temp->next;
prin ("NULL\n");
int main() {
struct Node* head = NULL; // Ini alize an empty doubly linked list
printList(head);
return 0;
}
C code snippet to add a node at the end of a doubly linked list:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = newData;
if (head == NULL) {
newNode->prev = NULL;
return newNode;
temp = temp->next;
}
// Update the last node's next to point to the new node
temp->next = newNode;
newNode->prev = temp;
return head;
temp = temp->next;
prin ("NULL\n");
int main() {
struct Node* head = NULL; // Ini alize an empty doubly linked list
printList(head);
return 0;
}
Here’s a C code snippet to insert a node at the Kth posi on in a doubly linked list:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = newData;
if (k == 1) {
newNode->prev = NULL;
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
// Traverse the list to find the (k-1)th node
temp = temp->next;
if (temp == NULL) {
free(newNode);
return head;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
temp->next = newNode;
return head;
prin ("NULL\n");
int main() {
struct Node* head = NULL; // Ini alize an empty doubly linked list
printList(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (head == NULL) {
return head;
if (posi on == 1) {
head = temp->next;
if (head != NULL) {
head->prev = NULL;
free(temp);
return head;
temp = temp->next;
if (temp == NULL) {
prin ("Invalid posi on.\n");
return head;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
if (temp->prev != NULL) {
temp->prev->next = temp->next;
free(temp);
return head;
temp = temp->next;
prin ("NULL\n");
int main() {
struct Node* head = NULL; // Ini alize an empty doubly linked list
return 0;
Here’s a collec on of essen al opera ons for working with a doubly linked list in C:
Copy
newNode->data = newData;
newNode->prev = NULL;
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
Copy
newNode->data = newData;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
return head;
Copy
newNode->data = newData;
if (k == 1) {
newNode->prev = NULL;
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
return newNode;
temp = temp->next;
if (temp == NULL) {
free(newNode);
return head;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
temp->next = newNode;
return head;
Copy
if (head == NULL) {
return head;
head = temp->next;
if (head != NULL) {
head->prev = NULL;
free(temp);
return head;
temp = temp->next;
if (temp == NULL) {
return head;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
if (temp->prev != NULL) {
temp->prev->next = temp->next;
free(temp);
return head;
Copy
temp = temp->next;
prin ("NULL\n");
Here’s how these opera ons can be u lized in a main func on:
Copy
int main() {
printList(head);
return 0;