Singly Linked List (SLL) - Full Programs with Explanation
SLL - Insert at Beginning
Explanation: This function creates a new node and sets its next pointer to the current head of the list. The head is then
updated to point to this new node, effectively inserting it at the beginning.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
display(head);
return 0;
}
SLL - Insert at End
Explanation: A new node is created and added at the end by traversing the list until the last node and linking the last
node's next pointer to the new node.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
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;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
display(head);
return 0;
}
SLL - Delete from Beginning
Explanation: This function removes the first node in the list by updating the head to point to the next node and freeing
the memory of the original head.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 10;
newNode->next = NULL;
head = newNode;
deleteFromBeginning(&head);
display(head);
return 0;
}
SLL - Insert at Specific Position
Explanation: This function inserts a node at a given position by traversing to the node before the desired position, then
updating the links to include the new node.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtPosition(struct Node** head, int data, int pos) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (pos == 1) {
newNode->next = *head;
*head = newNode;
return;
}
struct Node* temp = *head;
for (int i = 1; i < pos - 1 && temp != NULL; i++)
temp = temp->next;
if (temp == NULL) return;
newNode->next = temp->next;
temp->next = newNode;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtPosition(&head, 10, 1);
insertAtPosition(&head, 20, 2);
insertAtPosition(&head, 30, 2);
display(head);
return 0;
}
SLL - Delete at Specific Position
Explanation: It removes a node from a specified position by updating the previous node's next pointer to skip over the
deleted node, then freeing it.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void deleteAtPosition(struct Node** head, int pos) {
if (*head == NULL) return;
struct Node* temp = *head;
if (pos == 1) {
*head = temp->next;
free(temp);
return;
}
for (int i = 1; temp != NULL && i < pos - 1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL) return;
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* n1 = (struct Node*)malloc(sizeof(struct Node));
struct Node* n2 = (struct Node*)malloc(sizeof(struct Node));
struct Node* n3 = (struct Node*)malloc(sizeof(struct Node));
n1->data = 10; n1->next = n2;
n2->data = 20; n2->next = n3;
n3->data = 30; n3->next = NULL;
head = n1;
deleteAtPosition(&head, 2);
display(head);
return 0;
}
Doubly Linked List (DLL) - Full Programs with Explanation
DLL - Insert at Beginning
Explanation: A new node is added at the beginning. Its next pointer is set to the current head, and if the list isn't empty,
the head's previous pointer is updated. The head is updated to the new node.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
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;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
display(head);
return 0;
}
DLL - Insert at End
Explanation: This function adds a node at the end of the list. It traverses to the last node and updates the next and
previous pointers appropriately.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
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;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
display(head);
return 0;
}
DLL - Insert at Specific Position
Explanation: Inserts a new node at a specific position in a doubly linked list by traversing and adjusting both next and
previous pointers for surrounding nodes.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void insertAtPosition(struct Node** head, int data, int pos) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (pos == 1) {
newNode->next = *head;
newNode->prev = NULL;
if (*head != NULL)
(*head)->prev = newNode;
*head = newNode;
return;
}
struct Node* temp = *head;
for (int i = 1; i < pos - 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;
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtPosition(&head, 10, 1);
insertAtPosition(&head, 20, 2);
insertAtPosition(&head, 30, 2);
display(head);
return 0;
}
DLL - Delete at Specific Position
Explanation: Deletes a node at a given position in a doubly linked list by correctly managing both next and previous
pointers of neighboring nodes and freeing memory.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void deleteAtPosition(struct Node** head, int pos) {
if (*head == NULL) return;
struct Node* temp = *head;
if (pos == 1) {
*head = temp->next;
if (*head != NULL)
(*head)->prev = NULL;
free(temp);
return;
}
for (int i = 1; i < pos && temp != NULL; i++)
temp = temp->next;
if (temp == NULL) return;
if (temp->prev != NULL)
temp->prev->next = temp->next;
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
}
void display(struct Node* head) {
while (head != NULL) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* n1 = (struct Node*)malloc(sizeof(struct Node));
struct Node* n2 = (struct Node*)malloc(sizeof(struct Node));
struct Node* n3 = (struct Node*)malloc(sizeof(struct Node));
n1->data = 10; n1->prev = NULL; n1->next = n2;
n2->data = 20; n2->prev = n1; n2->next = n3;
n3->data = 30; n3->prev = n2; n3->next = NULL;
head = n1;
deleteAtPosition(&head, 2);
display(head);
return 0;
}