7.
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
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit.
#include <stdio.h>
#include <stdlib.h>
/* Structure to store student details */
struct node {
char usn[25], name[25], branch[25];
int sem;
long int phone;
struct node *link; // Pointer to next node
};
typedef struct node * NODE;
NODE start = NULL; // Points to first node in SLL
int count = 0; // Maintains total number of nodes
/* Function to create a new student node */
NODE create() {
NODE snode;
snode = (NODE)malloc(sizeof(struct node));
if (snode == NULL) {
printf("\nMemory is not available");
exit(1);
}
// Read student details
printf("\nEnter the USN, Name, Branch, Sem, Phone No of the student: ");
scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch, &snode->sem, &snode->phone);
snode->link = NULL; // New node is initially isolated
count++;
return snode;
}
/* Insert a node at the front of SLL */
NODE insertfront() {
NODE temp;
temp = create(); // Create a new student node
if (start == NULL) { // If list is empty
return temp; // New node becomes first node
}
temp->link = start; // Link new node to existing first node
return temp; // New node becomes new start node
}
/* Delete a node from the front of SLL */
NODE deletefront() {
NODE temp;
if (start == NULL) { // Underflow condition
printf("\nLinked list is empty");
return NULL;
}
if (start->link == NULL) { // Only one node exists
printf("\nThe Student node with USN:%s is deleted ", start->usn);
count--;
free(start);
return NULL; // List becomes empty
}
temp = start; // Store first node
start = start->link; // Move start to second node
printf("\nThe Student node with USN:%s is deleted", temp->usn);
count--;
free(temp); // Delete old first node
return start;
}
/* Insert a node at the end of SLL */
NODE insertend() {
NODE cur, temp;
temp = create();
if (start == NULL) { // If list empty, new node becomes first
return temp;
}
cur = start;
while (cur->link != NULL) { // Traverse to last node
cur = cur->link;
}
cur->link = temp; // Link last node to new node
return start;
}
/* Delete a node from the end of SLL */
NODE deleteend() {
NODE cur, prev;
if (start == NULL) { // Underflow condition
printf("\nLinked List is empty");
return NULL;
}
if (start->link == NULL) { // Only one node
printf("\nThe student node with USN:%s is deleted", start->usn);
free(start);
count--;
return NULL;
}
prev = NULL;
cur = start;
// Traverse until last node
while (cur->link != NULL) {
prev = cur;
cur = cur->link;
}
// cur is last node, prev is second last
printf("\nThe student node with USN:%s is deleted", cur->usn);
free(cur);
prev->link = NULL; // Second last becomes last node
count--;
return start;
/* Display all student nodes in SLL */
void display() {
NODE cur;
int num = 1;
if (start == NULL) {
printf("\nNo contents to display in SLL\n");
return;
}
printf("\nThe contents of SLL:\n");
cur = start;
// Traverse and print all nodes
while (cur != NULL) {
printf("\n||%d|| USN:%s | Name:%s | Branch:%s | Sem:%d | Ph:%ld |",
num, cur->usn, cur->name, cur->branch, cur->sem, cur->phone);
cur = cur->link;
num++;
}
printf("\nNumber of student nodes = %d\n", count);
}
/* Stack operations using SLL (push/front insert and pop/front delete) */
void stackdemo() {
int ch;
while (1) {
printf("\nStack Demo using SLL\n");
printf("1. Push operation\n");
printf("2. Pop operation\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice for stack demo: ");
scanf("%d", &ch);
switch (ch) {
case 1: // Push -> Insert at front
start = insertfront();
break;
case 2: // Pop -> Delete from front
start = deletefront();
break;
case 3:
display();
break;
default:
return; // Exit stack demo
}
}
}
/* Main function with menu-driven SLL program */
int main() {
int ch, i, n;
while (1) {
printf("\nMenu");
printf("\n Enter your choice for SLL operation \n");
printf("\n1. Create SLL of Student Nodes");
printf("\n2. Display Status");
printf("\n3. Insert at End");
printf("\n4. Delete at End");
printf("\n5. Stack Demo using SLL (Front operations)");
printf("\n6. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the number of students: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
start = insertfront();
break;
case 2:
display();
break;
case 3:
start = insertend();
break;
case 4:
start = deleteend();
break;
case 5:
stackdemo();
break;
case 6:
exit(0);
default:
printf("\nPlease enter a valid choice");
}
}
}
Output
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 2
No contents to display in SLL
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice:1
Enter the number of students: 3
Enter the USN, Name, Branch, Sem, Phone No of the student:
001
Anarv
CSE
3
123456
Enter the USN, Name, Branch, Sem, Phone No of the student:
002
Bob
CSE
3
789456
Enter the USN, Name, Branch, Sem, Phone No of the student:
003
John
CSE
3
998456
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 2
The contents of SLL:
||1|| USN:003 | Name:John | Branch:CSE | Sem:3 | Ph:998456 |
||2|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||3|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
Number of student nodes = 3
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 3
Enter the USN, Name, Branch, Sem, Phone No of the student:
004
Marry
CSE
3
65489
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 2
The contents of SLL:
||1|| USN:003 | Name:John | Branch:CSE | Sem:3 | Ph:998456 |
||2|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||3|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
||4|| USN:004 | Name:Marry | Branch:CSE | Sem:3 | Ph:65489 |
Number of student nodes = 4
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 4
The student node with USN:004 is deleted
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 2
The contents of SLL:
||1|| USN:003 | Name:John | Branch:CSE | Sem:3 | Ph:998456 |
||2|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||3|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
Number of student nodes = 3
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 4
The student node with USN:001 is deleted
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 5
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 3
The contents of SLL:
||1|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||2|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
Number of student nodes = 2
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 1
Enter the USN, Name, Branch, Sem, Phone No of the student:
005
Michel
CSE
3
745896
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 3
||1|| USN:005 | Name:Michel | Branch:CSE | Sem:3 | Ph:745896 |
||2|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||3|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
Number of student nodes = 3
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 1
Enter the USN, Name, Branch, Sem, Phone No of the student:
006
Rosy
CSE
3
547963
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 3
The contents of SLL:
||1|| USN:006 | Name:Rosy | Branch:CSE | Sem:3 | Ph:547963 |
||2|| USN:005 | Name:Michel | Branch:CSE | Sem:3 | Ph:745896 |
||3|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||4|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
Number of student nodes = 4
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 2
The Student node with USN:006 is deleted
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 3
The contents of SLL:
||1|| USN:005 | Name:Michel | Branch:CSE | Sem:3 | Ph:745896 |
||2|| USN:002 | Name:Bob | Branch:CSE | Sem:3 | Ph:789456 |
||3|| USN:001 | Name:Anarv | Branch:CSE | Sem:3 | Ph:123456 |
Number of student nodes = 3
Stack Demo using SLL
1. Push operation
2. Pop operation
3. Display
4. Exit
Enter your choice for stack demo: 4
Menu
Enter your choice for SLL operation
1. Create SLL of Student Nodes
2. Display Status
3. Insert at End
4. Delete at End
5. Stack Demo using SLL (Front operations)
6. Exit
Enter your choice: 6
********