DSC314 Data Structure Lab5
DSC314 Data Structure Lab5
#include <stdio.h>
#include <stdlib.h>
/* Menu-driven program */
int main() {
int choice, data;
do {
printf("\nMenu:\n");
printf("1. Insert an element\n");
printf("2. Print the list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &data);
sortedInsert(data);
printf("Value inserted successfully.\n");
break;
case 2:
printf("Current list: ");
printList();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 3);
return 0;
}
/* function to insert a new_node in a sorted list */
void sortedInsert(int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
#include <stdio.h>
#include <stdlib.h>
do {
printf("\nMenu:\n");
printf("1. Insert an element\n");
printf("2. Print the list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &data);
createNode(data);
printf("Node inserted successfully.\n");
break;
case 2:
printList();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 3);
return 0;
}