DS Lab Program 7
DS Lab Program 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>
#include <string.h>
char USN[15];
char name[50];
char programme[50];
int sem;
char phNo[15];
} Student;
// Function prototypes
void displayList();
int countNodes();
void insertAtEnd();
void deleteAtEnd();
void insertAtFront();
void deleteAtFront();
int main() {
int choice, n;
while (1) {
printf("\nMenu:\n");
printf("2. Display the status of SLL and count the number of nodes\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &n);
createList(n);
break;
case 2:
displayList();
break;
case 3:
insertAtEnd();
break;
case 4:
deleteAtEnd();
break;
case 5:
insertAtFront();
break;
case 6:
deleteAtFront();
break;
case 7:
exit(0);
default:
void createList(int n) {
for (int i = 0; i < n; i++) {
scanf("%s", newStudent->USN);
scanf("%s", newStudent->name);
scanf("%s", newStudent->programme);
scanf("%d", &newStudent->sem);
scanf("%s", newStudent->phNo);
void displayList() {
if (temp == NULL) {
return;
printf("USN: %s, Name: %s, Programme: %s, Sem: %d, Phone No: %s\n",
temp = temp->next;
int countNodes() {
int count = 0;
count++;
temp = temp->next;
return count;
void insertAtEnd() {
scanf("%s", newStudent->USN);
scanf("%s", newStudent->name);
scanf("%s", newStudent->programme);
printf("Enter Sem: ");
scanf("%d", &newStudent->sem);
scanf("%s", newStudent->phNo);
newStudent->next = NULL;
if (head == NULL) {
head = newStudent;
return;
temp = temp->next;
temp->next = newStudent;
void deleteAtEnd() {
if (head == NULL) {
return;
if (head->next == NULL) {
free(head);
head = NULL;
return;
temp = temp->next;
}
printf("Node deleted is data of USN %s\n ", temp->next->USN);
free(temp->next);
temp->next = NULL;
void insertAtFront() {
scanf("%s", newStudent->USN);
scanf("%s", newStudent->name);
scanf("%s", newStudent->programme);
scanf("%d", &newStudent->sem);
scanf("%s", newStudent->phNo);
newStudent->next = head;
head = newStudent;
void deleteAtFront() {
if (head == NULL) {
return;
head = head->next;
printf("Node deleted is data of USN %s\n ", temp->USN);
free(temp);
}
OUTPUT –