SLL Without Comments
SLL Without Comments
h>
#include <stdlib.h>
#include <string.h>
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");
}
}
}