Menu-Driven Singly Linked List Program in C with Insert and Delete Operations

A singly linked list is a dynamic data structure consisting of nodes, where each node contains data and a pointer to the next node. In this blog, we’ll walk through how to create a menu-driven program in C that allows multiple linked list operations such as insertions and deletions at specific positions.


Supported Operations

This program supports the following operations:

  • (a) Insert a node at the front of the list
  • (b) Insert a node at the end
  • (c) Insert a node in ascending order (based on data)
  • (d) Delete the first node
  • (e) Delete a node before a specified position
  • (f) Delete a node after a specified position

C Program for Singly Linked List

#include <stdio.h>
#include <stdlib.h>

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

struct Node* head = NULL;

// Insert at front
void insertFront(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}

// Insert at end
void insertEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
return;
}

struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;

temp->next = newNode;
}

// Insert in ascending order
void insertSorted(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (head == NULL || head->data >= value) {
newNode->next = head;
head = newNode;
return;
}

struct Node* temp = head;
while (temp->next != NULL && temp->next->data < value)
temp = temp->next;

newNode->next = temp->next;
temp->next = newNode;
}

// Delete first node
void deleteFirst() {
if (head == NULL) {
printf("List is empty\n");
return;
}

struct Node* temp = head;
head = head->next;
free(temp);
printf("First node deleted\n");
}

// Delete node before a given position
void deleteBeforePosition(int pos) {
if (pos <= 1 || head == NULL || head->next == NULL) {
printf("Cannot delete before position %d\n", pos);
return;
}

if (pos == 2) {
struct Node* temp = head;
head = head->next;
free(temp);
return;
}

struct Node* temp = head;
for (int i = 1; temp->next->next != NULL && i < pos - 2; i++)
temp = temp->next;

struct Node* del = temp->next;
temp->next = del->next;
free(del);
}

// Delete node after a given position
void deleteAfterPosition(int pos) {
struct Node* temp = head;
for (int i = 1; temp != NULL && i < pos; i++)
temp = temp->next;

if (temp == NULL || temp->next == NULL) {
printf("No node to delete after position %d\n", pos);
return;
}

struct Node* del = temp->next;
temp->next = del->next;
free(del);
}

// Display list
void display() {
struct Node* temp = head;
if (!temp) {
printf("List is empty\n");
return;
}

printf("Linked list: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
int choice, value, position;

do {
printf("\nMenu:\n");
printf("1. Insert at front\n2. Insert at end\n3. Insert in sorted order\n");
printf("4. Delete first node\n5. Delete before position\n6. Delete after position\n7. Display\n8. Exit\n");

printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value: ");
scanf("%d", &value);
insertEnd(value);
break;
case 3:
printf("Enter value: ");
scanf("%d", &value);
insertSorted(value);
break;
case 4:
deleteFirst();
break;
case 5:
printf("Enter position: ");
scanf("%d", &position);
deleteBeforePosition(position);
break;
case 6:
printf("Enter position: ");
scanf("%d", &position);
deleteAfterPosition(position);
break;
case 7:
display();
break;
case 8:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}

} while (choice != 8);

return 0;
}

Sample Output

Menu:
1. Insert at front
2. Insert at end
3. Insert in sorted order
4. Delete first node
5. Delete before position
6. Delete after position
7. Display
8. Exit

Enter your choice: 1
Enter value: 40

Linked list: 40 -> NULL

Enter your choice: 2
Enter value: 60

Linked list: 40 -> 60 -> NULL

Enter your choice: 3
Enter value: 50

Linked list: 40 -> 50 -> 60 -> NULL

Enter your choice: 5
Enter position: 3

Linked list: 50 -> 60 -> NULL

Key Points

  • Linked lists are dynamic and memory-efficient.
  • Use malloc() to allocate new nodes.
  • Be careful with edge cases in delete operations.
  • Always test with multiple scenarios.
Previous Article

Circular Queue Implementation in C Using Array (With INSERT, DELETE, DISPLAY)

Next Article

Stack Implementation in C Using Linked List (With Code & Explanation)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨