Experiment-3
Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
return head;
struct Node* insertAtEnd(struct Node* head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
return newNode;
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
current->next = newNode;
return head;
struct Node* insertInMiddle(struct Node* head, int key, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (head == NULL) {
return newNode;
struct Node* current = head;
struct Node* previous = NULL;
while (current != NULL && current->data != key) {
previous = current;
current = current->next;
if (previous == NULL) {
newNode->next = head;
head = newNode;
} else {
newNode->next = current;
previous->next = newNode;
return head;
struct Node* deleteAtBeginning(struct Node* head) {
if (head == NULL) {
printf("List is empty. Cannot delete from the beginning.\n");
return NULL;
struct Node* temp = head;
head = head->next;
free(temp);
return head;
struct Node* deleteAtEnd(struct Node* head) {
if (head == NULL) {
printf("List is empty. Cannot delete from the end.\n");
return NULL;
if (head->next == NULL) {
free(head);
return NULL;
struct Node* current = head;
struct Node* previous = NULL;
while (current->next != NULL) {
previous = current;
current = current->next;
}
free(current);
previous->next = NULL;
return head;
struct Node* deleteInMiddle(struct Node* head, int key) {
if (head == NULL) {
printf("List is empty. Cannot delete from the middle.\n");
return NULL;
struct Node* current = head;
struct Node* previous = NULL;
while (current != NULL && current->data != key) {
previous = current;
current = current->next;
if (current == NULL) {
printf("Element %d not found in the list.\n", key);
return head;
}
if (previous == NULL) {
head = head->next;
} else {
previous->next = current->next;
free(current);
return head;
void display(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
printf("NULL\n");
void traverse(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
printf("\n");
int search(struct Node* head, int key) {
struct Node* current = head;
int position = 0;
while (current != NULL) {
if (current->data == key) {
return position;
current = current->next;
position++;
return -1;
int main() {
struct Node* head = NULL;
int choice, value, key, position;
do {
printf("\nMenu:\n");
printf("1. Insert at the Beginning\n");
printf("2. Insert at the End\n");
printf("3. Insert in the Middle\n");
printf("4. Delete at the Beginning\n");
printf("5. Delete at the End\n");
printf("6. Delete in the Middle\n");
printf("7. Display Linked List\n");
printf("8. Traverse Linked List\n");
printf("9. Search in Linked List\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert at the beginning: ");
scanf("%d", &value);
head = insertAtBeginning(head, value);
break;
case 2:
printf("Enter the value to insert at the end: ");
scanf("%d", &value);
head = insertAtEnd(head, value);
break;
case 3:
printf("Enter the key after which to insert in the middle: ");
scanf("%d", &key);
printf("Enter the value to insert in the middle: ");
scanf("%d", &value);
head = insertInMiddle(head, key, value);
break;
case 4:
head = deleteAtBeginning(head);
break;
case 5:
head = deleteAtEnd(head);
break;
case 6:
printf("Enter the key to delete in the middle: ");
scanf("%d", &key);
head = deleteInMiddle(head, key);
break;
case 7:
printf("Linked List: ");
display(head);
break;
case 8:
printf("Traversing Linked List: ");
traverse(head);
break;
case 9:
printf("Enter the element to search: ");
scanf("%d", &key);
position = search(head, key);
if (position != -1) {
printf("Element %d found at position %d\n", key, position);
} else {
printf("Element %d not found in the list\n", key);
break;
case 0:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 0);
return 0;