Lab 7 - DSA
Lab 7 - DSA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char usn[20];
char name[50];
char programme[50];
int semester;
char phone[15];
struct Student *next;
};
newNode->next = head;
head = newNode;
if (temp == NULL) {
printf("\nThe list is empty.\n");
return;
}
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
printf("\nStudent added to the end of the list!\n");
}
// Function to delete a node from the end of the SLL
void deleteEnd() {
struct Student *temp = head, *prev = NULL;
if (head == NULL) {
printf("\nThe list is empty. Nothing to delete.\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
} else {
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
}
printf("\nLast student data deleted from the list!\n");
}
// Function to insert a node at the front of the SLL (like a stack push)
void insertFront() {
struct Student *newNode = (struct Student *)malloc(sizeof(struct Student));
newNode->next = head;
head = newNode;
if (head == NULL) {
printf("\nThe list is empty. Nothing to delete.\n");
return;
}
head = head->next;
free(temp);
int main() {
int choice, n;
while (1) {
printf("\nMenu:\n");
printf("1. Create SLL of N Students Data (front insertion)\n");
printf("2. Display SLL and Count Nodes\n");
printf("3. Insert at End\n");
printf("4. Delete from End\n");
printf("5. Insert at Front (Stack Demonstration)\n");
printf("6. Delete from Front (Stack Demonstration)\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);
createFront(n);
break;
case 2:
displayAndCount();
break;
case 3:
insertEnd();
break;
case 4:
deleteEnd();
break;
case 5:
insertFront();
break;
case 6:
deleteFront();
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}