0% found this document useful (0 votes)
5 views

DS Lab Program 7

The document outlines a C program for managing a singly linked list (SLL) of student data, including operations for creating, displaying, inserting, and deleting nodes. It defines a 'Student' structure with fields for USN, name, programme, semester, and phone number, and provides a menu-driven interface for user interaction. The program supports various operations such as front insertion, end insertion/deletion, and front deletion, along with displaying the list and counting nodes.

Uploaded by

shwetha.csefac
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DS Lab Program 7

The document outlines a C program for managing a singly linked list (SLL) of student data, including operations for creating, displaying, inserting, and deleting nodes. It defines a 'Student' structure with fields for USN, name, programme, semester, and phone number, and provides a menu-driven interface for user interaction. The program supports various operations such as front insertion, end insertion/deletion, and front deletion, along with displaying the list and counting nodes.

Uploaded by

shwetha.csefac
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Program 07 : Singly Linked List of Student Data

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>

typedef struct Student {

char USN[15];

char name[50];

char programme[50];

int sem;

char phNo[15];

struct Student* next;

} Student;

Student* head = NULL;

// Function prototypes

void createList(int n);

void displayList();

int countNodes();

void insertAtEnd();
void deleteAtEnd();

void insertAtFront();

void deleteAtFront();

int main() {

int choice, n;

while (1) {

printf("\nMenu:\n");

printf("1. Create SLL of N Students Data by front insertion\n");

printf("2. Display the status of SLL and count the number of nodes\n");

printf("3. Insertion at End of SLL\n");

printf("4. Deletion at End of SLL\n");

printf("5. Insertion at Front of SLL\n");

printf("6. Deletion at Front of SLL\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the number of students: ");

scanf("%d", &n);

createList(n);

break;
case 2:

displayList();

printf("Total nodes: %d\n", countNodes());

break;

case 3:

insertAtEnd();

break;

case 4:

deleteAtEnd();

break;

case 5:

insertAtFront();

break;

case 6:

deleteAtFront();

break;

case 7:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

void createList(int n) {
for (int i = 0; i < n; i++) {

Student* newStudent = (Student*)malloc(sizeof(Student));

printf("Enter USN: ");

scanf("%s", newStudent->USN);

printf("Enter Name: ");

scanf("%s", newStudent->name);

printf("Enter Programme: ");

scanf("%s", newStudent->programme);

printf("Enter Sem: ");

scanf("%d", &newStudent->sem);

printf("Enter Phone No: ");

scanf("%s", newStudent->phNo);

newStudent->next = head; // Insert at front

head = newStudent; // Update head

void displayList() {

Student* temp = head;

if (temp == NULL) {

printf("The list is empty.\n");

return;

printf("\nStudents in the list:\n");


while (temp != NULL) {

printf("USN: %s, Name: %s, Programme: %s, Sem: %d, Phone No: %s\n",

temp->USN, temp->name, temp->programme, temp->sem, temp->phNo);

temp = temp->next;

int countNodes() {

int count = 0;

Student* temp = head;

while (temp != NULL) {

count++;

temp = temp->next;

return count;

void insertAtEnd() {

Student* newStudent = (Student*)malloc(sizeof(Student));

printf("Enter USN: ");

scanf("%s", newStudent->USN);

printf("Enter Name: ");

scanf("%s", newStudent->name);

printf("Enter Programme: ");

scanf("%s", newStudent->programme);
printf("Enter Sem: ");

scanf("%d", &newStudent->sem);

printf("Enter Phone No: ");

scanf("%s", newStudent->phNo);

newStudent->next = NULL;

if (head == NULL) {

head = newStudent;

return;

Student* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newStudent;

void deleteAtEnd() {

if (head == NULL) {

printf("The list is empty. Cannot delete.\n");

return;

if (head->next == NULL) {
free(head);

head = NULL;

return;

Student* temp = head;

while (temp->next->next != NULL) {

temp = temp->next;

}
printf("Node deleted is data of USN %s\n ", temp->next->USN);

free(temp->next);

temp->next = NULL;

void insertAtFront() {

Student* newStudent = (Student*)malloc(sizeof(Student));

printf("Enter USN: ");

scanf("%s", newStudent->USN);

printf("Enter Name: ");

scanf("%s", newStudent->name);

printf("Enter Programme: ");

scanf("%s", newStudent->programme);

printf("Enter Sem: ");

scanf("%d", &newStudent->sem);

printf("Enter Phone No: ");

scanf("%s", newStudent->phNo);
newStudent->next = head;

head = newStudent;

void deleteAtFront() {

if (head == NULL) {

printf("The list is empty. Cannot delete.\n");

return;

Student* temp = head;

head = head->next;
printf("Node deleted is data of USN %s\n ", temp->USN);

free(temp);
}

OUTPUT –

You might also like