QUESTION:
Develop a menu driven Program in C for the following operations on Singly Linked List
(SLL) of Student Data with the fields: USN, Name, Programme, Sem,
PhNo
Create a SLL of N Students Data by using front insertion.
Display the status of SLL and count the number of nodes in it
Perform Insertion / Deletion at End of SLL
Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
Exit
Here's a menu-driven C program that performs operations on a Singly Linked List
(SLL) for storing student data, as per your requirements. Student Data Fields:
Each node contains:
-
USN
(string)
-
Name
(string)
-
Programme
(string)
-
Sem
(integer)
-
PhNo
(string)
Supported Operations:
-
Create SLL by Front Insertion
-
Display List & Count Nodes
-
Insert at End
-
Delete from End
-
Insert at Front (acts like push in stack)
-
Delete from Front (acts like pop in stack)
-
Exit
Complete C Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to hold student data
struct Student {
char usn[20];
char name[50];
char program[30];
int sem;
char phno[15];
struct Student* next;
};
typedef struct Student* NODE;
NODE head = NULL;
// Function to create a new node with student data
NODE createNode() {
NODE newNode = (NODE)malloc(sizeof(struct Student));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
printf("Enter USN: ");
scanf("%s", newNode->usn);
printf("Enter Name: ");
scanf("%s", newNode->name);
printf("Enter Programme: ");
scanf("%s", newNode->program);
printf("Enter Semester: ");
scanf("%d", &newNode->sem);
printf("Enter Phone Number: ");
scanf("%s", newNode->phno);
newNode->next = NULL;
return newNode;
}
// Front insertion (like push in stack)
void insertFront() {
NODE newNode = createNode();
newNode->next = head;
head = newNode;
printf("Student inserted at front.\n");
}
// Insert at end
void insertEnd() {
NODE newNode = createNode();
if (head == NULL) {
head = newNode;
} else {
NODE temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("Student inserted at end.\n");
}
// Delete from front (like pop in stack)
void deleteFront() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
NODE temp = head;
head = head->next;
printf("Deleted student with USN: %s\n", temp->usn);
free(temp);
}
// Delete from end
void deleteEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
if (head->next == NULL) {
printf("Deleted student with USN: %s\n", head->usn);
free(head);
head = NULL;
return;
}
NODE temp = head, prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
printf("Deleted student with USN: %s\n", temp->usn);
free(temp);
prev->next = NULL;
}
// Display student list and count nodes
void displayAndCount() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
int count = 0;
NODE temp = head;
printf("\n--- Student List ---\n");
while (temp != NULL) {
printf("USN: %s | Name: %s | Program: %s | Sem: %d | Phone: %s\n",
temp->usn, temp->name, temp->program, temp->sem, temp->phno);
temp = temp->next;
count++;
}
printf("Total students: %d\n", count);
}
// Main function with menu
int main() {
int choice;
while (1) {
printf("\n--- Student SLL Menu ---\n");
printf("1. Create List by Front Insertion\n");
printf("2. Display and Count Nodes\n");
printf("3. Insert at End\n");
printf("4. Delete from End\n");
printf("5. Insert at Front (Stack Push)\n");
printf("6. Delete from Front (Stack Pop)\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertFront();
break;
case 2:
displayAndCount();
break;
case 3:
insertEnd();
break;
case 4:
deleteEnd();
break;
case 5:
insertFront();
break;
case 6:
deleteFront();
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
Supported Operations:
-
Create SLL by Front Insertion
-
Display List & Count Nodes
-
Insert at End
-
Delete from End
-
Insert at Front (acts like push in stack)
-
Delete from Front (acts like pop in stack)
-
Exit
Complete C Program: }
}
return 0;
}
How It Works (Stack Demo Included):
Output:
ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc studentsdata.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 1
Enter USN: 1jo2627
Enter Name: Joda
Enter Programme: IOT
Enter Semester: 3
Enter Phone Number: 9876543210
Student inserted at front.
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
Total students: 1
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 1
Enter USN: 1jo4532
Enter Name: Jones
Enter Programme: IOT
Enter Semester: 3
Enter Phone Number: 123456789
Student inserted at front.
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo4532 | Name: Jones | Program: IOT | Sem: 3 | Phone: 123456789
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
Total students: 2
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo4532 | Name: Jones | Program: IOT | Sem: 3 | Phone: 123456789
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
Total students: 2
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 3
Enter USN: 1jo2456
Enter Name: joseph
Enter Programme: IOT
Enter Semester: 3
Enter Phone Number: 6789012314
Student inserted at end.
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 3
Enter USN: IJO549
Enter Name: SATHYA
Enter Programme: IOT
Enter Semester: 3
Enter Phone Number: 6543290876
Student inserted at end.
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo4532 | Name: Jones | Program: IOT | Sem: 3 | Phone: 123456789
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
USN: 1jo2456 | Name: joseph | Program: IOT | Sem: 3 | Phone: 6789012314
USN: IJO549 | Name: SATHYA | Program: IOT | Sem: 3 | Phone: 6543290876
Total students: 4
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 4
Deleted student with USN: IJO549
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 4
Deleted student with USN: 1jo2456
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo4532 | Name: Jones | Program: IOT | Sem: 3 | Phone: 123456789
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
Total students: 2
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo4532 | Name: Jones | Program: IOT | Sem: 3 | Phone: 123456789
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
Total students: 2
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 6
Deleted student with USN: 1jo4532
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
Enter your choice: 2
--- Student List ---
USN: 1jo2627 | Name: Joda | Program: IOT | Sem: 3 | Phone: 9876543210
Total students: 1
--- Student SLL Menu ---
1. Create List by Front Insertion
2. Display and Count Nodes
3. Insert at End
4. Delete from End
5. Insert at Front (Stack Push)
6. Delete from Front (Stack Pop)
7. Exit
No comments:
Post a Comment