Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
struct Node {
int data;
struct Node* next;
};
// Insert at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next = *head;
*head = newNode;
// Insert at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next =
NULL; if (*head ==
NULL) {
*head = newNode;
return;
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
// Insert at a given position
void insertAtPosition(struct Node** head, int data, int position)
{ struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
if (position == 0)
{ newNode->next =
*head;
*head = newNode;
return;
struct Node* temp = *head;
for (int i = 0; i < position - 1 && temp != NULL; i++)
{ temp = temp->next;
if (temp == NULL) return;
newNode->next = temp-
>next; temp->next =
newNode;
// Delete from the beginning
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)-
>next; free(temp);
}
// Delete from the end
void deleteFromEnd(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
if (temp->next == NULL) {
*head = NULL;
free(temp);
return;
while (temp->next->next != NULL) {
temp = temp->next;
free(temp->next);
temp->next =
NULL;
// Delete from a given position
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) return;
struct Node* temp = *head;
if (position == 0) {
*head = temp-
>next; free(temp);
return;
for (int i = 0; i < position - 1 && temp != NULL; i++)
{ temp = temp->next;
}
if (temp == NULL || temp->next == NULL) return;
struct Node* deleteNode = temp->next;
temp->next = temp->next->next;
free(deleteNode);
// Search an element
int searchElement(struct Node* head, int key)
{ int position = 0;
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key) return position;
temp = temp->next;
position++;
return -1; // Element not found
// Reverse the linked list
void reverseList(struct Node** head)
{ struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL) {
next = current-
>next; current->next
= prev; prev =
current; current =
next;
}
*head = prev;
// Merge two linked lists
struct Node* mergeLists(struct Node* head1, struct Node* head2) {
if (head1 == NULL) return head2;
if (head2 == NULL) return head1;
struct Node* mergedHead = NULL;
if (head1->data <= head2->data) {
mergedHead = head1;
mergedHead->next = mergeLists(head1->next, head2);
} else {
mergedHead = head2;
mergedHead->next = mergeLists(head1, head2->next);
return mergedHead;
// Display the linked list
void displayList(struct Node* head)
{ struct Node* temp = head;
while (temp != NULL)
{ printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
}
// Main function
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
insertAtPosition(&head, 15, 1);
printf("Linked list after insertions:\n");
displayList(head);
deleteFromBeginning(&head);
deleteFromEnd(&head);
printf("Linked list after deletions:\n");
displayList(head);
int position = searchElement(head, 15);
if (position != -1) {
printf("Element found at position: %d\n", position);
} else {
printf("Element not found\n");
reverseList(&head);
printf("Linked list after reversal:\
n"); displayList(head);
return 0;
}
Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
struct Node {
int data;
struct Node*
prev; struct
Node* next;
};
// Insert at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->prev = NULL;
newNode->next =
*head; if (*head !=
NULL) {
(*head)->prev = newNode;
*head = newNode;
// Insert at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next = NULL;
if (*head == NULL)
{ newNode->prev =
NULL;
*head = newNode;
return;
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
temp->next =
newNode; newNode-
>prev = temp;
// Insert at a given position
void insertAtPosition(struct Node** head, int data, int position) {
if (position == 0) {
insertAtBeginning(head, data);
return;
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
struct Node* temp = *head;
for (int i = 0; i < position - 1 && temp != NULL; i++)
{ temp = temp->next;
if (temp == NULL) return;
newNode->next = temp-
>next; newNode->prev =
temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
temp->next = newNode;
// Delete from the beginning
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)-
>next; if (*head !=
NULL) {
(*head)->prev = NULL;
free(temp);
// Delete from the end
void deleteFromEnd(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
if (temp->next == NULL) {
*head = NULL;
free(temp);
return;
while (temp->next != NULL)
{ temp = temp->next;
temp->prev->next = NULL;
free(temp);
// Delete from a given position
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) return;
struct Node* temp = *head;
if (position == 0) {
deleteFromBeginning(head);
return;
for (int i = 0; i < position && temp != NULL; i++)
{ temp = temp->next;
if (temp == NULL) return;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
if (temp->prev != NULL) {
temp->prev->next = temp->next;
free(temp);
// Search an element
int searchElement(struct Node* head, int key)
{ int position = 0;
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == key) return position;
temp = temp->next;
position++;
return -1; // Element not found
// Reverse the linked list
void reverseList(struct Node** head)
{ struct Node* temp = NULL;
struct Node* current = *head;
while (current != NULL) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
if (temp != NULL) {
*head = temp->prev;
// Merge two doubly linked lists
void mergeLists(struct Node** head1, struct Node** head2)
{ if (*head1 == NULL) {
*head1 = *head2;
return;
struct Node* temp = *head1;
while (temp->next != NULL)
{ temp = temp->next;
temp->next =
*head2; if (*head2 !=
NULL) {
(*head2)->prev = temp;
// Traverse/Display the linked list
void displayList(struct Node* head)
{ struct Node* temp = head;
while (temp != NULL)
{ printf("%d <-> ", temp-
>data); temp = temp->next;
printf("NULL\n");
// Main function
int main() {
struct Node* head = NULL; // First node named as head
// Insertion examples
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
insertAtPosition(&head, 15, 1);
printf("Doubly linked list after insertions:\n");
displayList(head);
// Deletion examples
deleteFromBeginning(&head);
deleteFromEnd(&head);
printf("Doubly linked list after deletions:\n");
displayList(head);
// Searching an element
int position = searchElement(head, 15);
if (position != -1) {
printf("Element found at position: %d\n", position);
} else {
printf("Element not found\n");
// Reversing the list
reverseList(&head);
printf("Doubly linked list after reversal:\n");
displayList(head);
return 0;
}
Circular Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
struct Node {
int data;
struct Node* next;
};
// Insert at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
if (*head == NULL) {
newNode->next = newNode; // Point to itself
*head = newNode;
return;
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
newNode->next =
*head; temp->next =
newNode;
*head = newNode;
// Insert at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
if (*head == NULL) {
newNode->next = newNode; // Point to itself
*head = newNode;
return;
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
temp->next = newNode;
newNode->next =
*head;
// Insert at a given position
void insertAtPosition(struct Node** head, int data, int position)
{ struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = data;
if (position == 0) {
insertAtBeginning(head, data);
return;
struct Node* temp = *head;
for (int i = 0; i < position - 1 && temp->next != *head; i++)
{ temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
// Delete from the beginning
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
if ((*head)->next == *head) { // Single node in the list
*head = NULL;
free(temp);
return;
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
*head = (*head)-
>next; last->next =
*head; free(temp);
// Delete from the end
void deleteFromEnd(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
if (temp->next == *head) { // Single node in the list
*head = NULL;
free(temp);
return;
}
struct Node* prev = NULL;
while (temp->next != *head) {
prev = temp;
temp = temp->next;
prev->next = *head;
free(temp);
// Delete from a given position
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) return;
struct Node* temp = *head;
if (position == 0) {
deleteFromBeginning(head);
return;
struct Node* prev = NULL;
for (int i = 0; i < position && temp->next != *head; i++)
{ prev = temp;
temp = temp->next;
if (temp == *head)
return; prev->next =
temp->next; free(temp);
// Search an element
int searchElement(struct Node* head, int key) {
if (head == NULL) return -1;
struct Node* temp = head;
int position = 0;
do {
if (temp->data == key) return position;
temp = temp->next;
position++;
} while (temp != head);
return -1; // Element not found
// Reverse the linked list
void reverseList(struct Node** head) {
if (*head == NULL) return;
struct Node *prev = NULL, *current = *head, *next = NULL, *tail = *head;
do {
next = current-
>next; current->next
= prev; prev =
current; current =
next;
} while (current != *head);
tail->next = prev; // Update last node's next pointer
*head = prev;
// Merge two circular singly linked lists
void mergeLists(struct Node** head1, struct Node** head2)
{ if (*head1 == NULL) {
*head1 = *head2;
return;
if (*head2 == NULL) return;
struct Node* temp1 = *head1;
while (temp1->next != *head1) {
temp1 = temp1->next;
struct Node* temp2 = *head2;
while (temp2->next != *head2) {
temp2 = temp2->next;
temp1->next =
*head2; temp2->next
= *head1;
// Display the linked list
void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
struct Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(head)\n");
}
// Main function
int main() {
struct Node* head = NULL; // Initialize the head of the circular list
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
insertAtPosition(&head, 15, 1);
printf("Circular singly linked list after insertions:\n");
displayList(head);
deleteFromBeginning(&head);
deleteFromEnd(&head);
printf("Circular singly linked list after deletions:\n");
displayList(head);
int position = searchElement(head, 15);
if (position != -1) {
printf("Element found at position: %d\n", position);
} else {
printf("Element not found\n");
reverseList(&head);
printf("Circular singly linked list after reversal:\n");
displayList(head);
return 0;