0% found this document useful (0 votes)
13 views4 pages

SLL Without Comments

The document contains a C program that implements a linked list to manage student records. It allows users to insert students at the front or end of the list, display the list with a count of students, and delete students from either end. The program runs in a loop, providing a menu for user interaction until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

SLL Without Comments

The document contains a C program that implements a linked list to manage student records. It allows users to insert students at the front or end of the list, display the list with a count of students, and delete students from either end. The program runs in a loop, providing a menu for user interaction until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

typedef struct Student {


char usn[15], name[30], programme[30], phone[15];
int sem;
struct Student* next;
} Student;

Student* head = NULL;

void frontInsert() {
Student* newNode = (Student*)malloc(sizeof(Student));
printf("Enter USN, Name, Programme, Sem, Phone:\n");
scanf("%s %s %s %d %s", newNode->usn, newNode->name, newNode-
>programme, &newNode->sem, newNode->phone);
newNode->next = head;
head = newNode;
printf("Student added at front.\n");
}

void display() {
if (!head) {
printf("List is empty.\n");
return;
}
int count = 0;
Student* temp = head;
printf("Student List:\n");
while (temp) {
printf("USN: %s, Name: %s, Programme: %s, Sem: %d, Phone: %s\n", temp-
>usn, temp->name, temp->programme, temp->sem, temp->phone);
temp = temp->next;
count++;
}
printf("Total Students: %d\n", count);
}

void endInsert() {
Student* newNode = (Student*)malloc(sizeof(Student));
printf("Enter USN, Name, Programme, Sem, Phone:\n");
scanf("%s %s %s %d %s", newNode->usn, newNode->name, newNode-
>programme, &newNode->sem, newNode->phone);
newNode->next = NULL;
if (!head) {
head = newNode;
} else {
Student* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
printf("Student added at end.\n");
}

void frontDelete() {
if (!head) {
printf("List is empty. Cannot delete.\n");
return;
}
Student* temp = head;
head = head->next;
free(temp);
printf("Student deleted from front.\n");
}
void endDelete() {
if (!head) {
printf("List is empty. Cannot delete.\n");
return;
}
if (!head->next) {
free(head);
head = NULL;
} else {
Student* temp = head;
while (temp->next->next) temp = temp->next;
free(temp->next);
temp->next = NULL;
}
printf("Student deleted from end.\n");
}

int main() {
int choice;
while (1) {
printf("\n1. Insert at Front\n2. Display & Count\n3. Insert at End\n4. Delete
from Front\n5. Delete from End\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: frontInsert(); break;
case 2: display(); break;
case 3: endInsert(); break;
case 4: frontDelete(); break;
case 5: endDelete(); break;
case 6: exit(0);
default: printf("Invalid choice. Try again.\n");
}
}
}

You might also like