Program 10
Program 10
Program 10
Write a menu driven program to perform all opertions on singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
int main() {
int n, data, choice, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createlist(n);
printf("\nData in the list:\n");
displaylist();
while(1) {
printf("---LINKED LIST PROGRAMS---\n");
printf("1. INSERT AT BEGINNING\n");
printf("2. INSERT AT END\n");
printf("3. DELETE FROM BEGINNING\n");
printf("4. DELETE FROM END\n");
printf("5. DISPLAY LIST\n");
printf("6. EXIT\n");
printf("7. DELETE FROM MIDDLE OR SPECIFIC POSITION\n");
printf("8. INSERT AT MIDDLE OR SPECIFIC POSITION\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter data to insert at the beginning of the
list: ");
scanf("%d", &data);
insertnodeatbeginning(data);
break;
case 2:
printf("Enter data to insert at the end of the list:
");
scanf("%d", &data);
insertnodeatend(data);
Program 10 Page 1
Program 10
break;
case 3:
deleteFirstNode();
break;
case 4:
deleteLastNode();
break;
case 5:
displaylist();
break;
case 6:
exit(0);
break;
case 7:
printf("Enter the node position you want to delete:
");
scanf("%d", &position);
deleteMiddleNode(position);
break;
case 8:
printf("Enter data to insert at the middle of the
list: ");
scanf("%d", &data);
printf("Enter the position to insert new node: ");
scanf("%d", &position);
insertnodeatmiddle(data, position);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
void createlist(int n) {
struct node *newnode, *temp;
int data, i;
head = (struct node*)malloc(sizeof(struct node));
if (head == NULL) {
printf("Unable to allocate memory.\n");
return;
}
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
Program 10 Page 2
Program 10
if (newnode == NULL) {
printf("Unable to allocate memory.\n");
break;
}
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL;
temp->next = newnode;
temp = temp->next;
}
printf("Singly linked list created successfully.\n");
}
void displaylist() {
struct node *temp = head;
if (head == NULL) {
printf("List is empty.\n");
} else {
while (temp != NULL) {
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Program 10 Page 3
Program 10
temp = temp->next;
}
temp->next = newnode;
}
printf("Data inserted successfully.\n");
}
}
void deleteFirstNode() {
if (head == NULL) {
printf("List is already empty.\n");
} else {
struct node *toDelete = head;
head = head->next;
printf("Data deleted = %d\n", toDelete->data);
free(toDelete);
printf("Successfully deleted first node from list\n");
}
}
void deleteLastNode() {
if (head == NULL) {
printf("List is already empty.\n");
} else {
struct node *toDelete = head, *prevNode = NULL;
while (toDelete->next != NULL) {
Program 10 Page 4
Program 10
prevNode = toDelete;
toDelete = toDelete->next;
}
if (prevNode != NULL) {
prevNode->next = NULL;
} else { // If there is only one node in the list
head = NULL;
}
printf("Data deleted = %d\n", toDelete->data);
free(toDelete);
printf("Successfully deleted last node from list\n");
}
}
Program 10 Page 5
Program 10
Output
Enter the total number of nodes: 5
Enter the data of node 1: 1
Enter the data of node 2: 2
Enter the data of node 3: 3
Enter the data of node 4: 4
Enter the data of node 5: 5
Singly linked list created successfully.
Program 10 Page 6