0% found this document useful (0 votes)
12 views5 pages

Lab 7 - DSA

The document contains a C program that implements a singly linked list (SLL) for managing student data, allowing operations such as creating the list, displaying and counting nodes, inserting and deleting nodes at both ends. It defines a 'Student' structure and provides functions for various list operations, including front and end insertions and deletions. The program features a menu-driven interface for user interaction to perform these operations.

Uploaded by

Ramesh sunadholi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Lab 7 - DSA

The document contains a C program that implements a singly linked list (SLL) for managing student data, allowing operations such as creating the list, displaying and counting nodes, inserting and deleting nodes at both ends. It defines a 'Student' structure and provides functions for various list operations, including front and end insertions and deletions. The program features a menu-driven interface for user interaction to perform these operations.

Uploaded by

Ramesh sunadholi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 7

#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;
};

struct Student *head = NULL;

// Function to create a SLL with front insertion


void createFront(int n) {
int i;
struct Student *newNode;
for (i = 0; i < n; i++) {
newNode = (struct Student *)malloc(sizeof(struct Student));

printf("\nEnter USN: ");


scanf("%s", newNode->usn);
printf("Enter Name: ");
scanf(" %[^\n]", newNode->name);
printf("Enter Programme: ");
scanf("%s", newNode->programme);
printf("Enter Semester: ");
scanf("%d", &newNode->semester);
printf("Enter Phone Number: ");
scanf("%s", newNode->phone);

newNode->next = head;
head = newNode;

printf("\nStudent added to the front of the list!\n");


}
}
// Function to display the SLL and count the number of nodes
void displayAndCount() {
struct Student *temp = head;
int count = 0;

if (temp == NULL) {
printf("\nThe list is empty.\n");
return;
}

printf("\nStudents Data in SLL:\n");


while (temp != NULL) {
printf("\nUSN: %s\tName: %s\tProgramme: %s\tSemester: %d\tPhone: %s\t",
temp->usn, temp->name, temp->programme, temp->semester, temp->phone);
temp = temp->next;
count++;
}
printf("\nTotal number of nodes: %d\n", count);
}

// Function to insert a node at the end of the SLL


void insertEnd() {
struct Student *newNode = (struct Student *)malloc(sizeof(struct Student));
struct Student *temp = head;

printf("\nEnter USN: ");


scanf("%s", newNode->usn);
printf("Enter Name: ");
scanf(" %[^\n]", newNode->name);
printf("Enter Programme: ");
scanf("%s", newNode->programme);
printf("Enter Semester: ");
scanf("%d", &newNode->semester);
printf("Enter Phone Number: ");
scanf("%s", newNode->phone);

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));

printf("\nEnter USN: ");


scanf("%s", newNode->usn);
printf("Enter Name: ");
scanf(" %[^\n]", newNode->name);
printf("Enter Programme: ");
scanf("%s", newNode->programme);
printf("Enter Semester: ");
scanf("%d", &newNode->semester);
printf("Enter Phone Number: ");
scanf("%s", newNode->phone);

newNode->next = head;
head = newNode;

printf("\nStudent added to the front of the list!\n");}


// Function to delete a node from the front of the SLL (like a stack pop)
void deleteFront() {
struct Student *temp = head;

if (head == NULL) {
printf("\nThe list is empty. Nothing to delete.\n");
return;
}
head = head->next;
free(temp);

printf("\nFirst student data deleted from the list!\n");


}

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;
}

You might also like