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

Linkedlist

This C program implements a linked list to store student records with name, registration number, marks, and grade. It defines a Node struct for the list elements and includes functions to create nodes, insert nodes, display the list, search by name or grade, delete nodes, and free the allocated memory. The main function populates the initial list, provides a menu to call the functions, and cleans up before exiting.
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)
26 views4 pages

Linkedlist

This C program implements a linked list to store student records with name, registration number, marks, and grade. It defines a Node struct for the list elements and includes functions to create nodes, insert nodes, display the list, search by name or grade, delete nodes, and free the allocated memory. The main function populates the initial list, provides a menu to call the functions, and cleans up before exiting.
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/ 4

#include <stdio.

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

typedef struct Node {


char name[100];
int regNumber;
int marks;
char grade[3];
struct Node* next;
} Node;

Node* createNode(char name[], int regNumber, int marks) {


Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->name, name);
newNode->regNumber = regNumber;
newNode->marks = marks;

if (marks > 90)


strcpy(newNode->grade, "EX");
else if (marks > 80)
strcpy(newNode->grade, "A");
else if (marks > 70)
strcpy(newNode->grade, "B");
else if (marks > 60)
strcpy(newNode->grade, "C");
else
strcpy(newNode->grade, "F");

newNode->next = NULL;

return newNode;
}

void insertNode(Node** head, Node* newNode) {


if (*head == NULL || newNode->marks < (*head)->marks || (newNode->marks ==
(*head)->marks && strcmp(newNode->grade, (*head)->grade) < 0)) {
newNode->next = *head;
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL && (newNode->marks > current->next->marks ||
(newNode->marks == current->next->marks && strcmp(newNode->grade, current->next-
>grade) > 0))) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}

void displayList(Node* head) {


Node* current = head;
printf("Student List:\n");
printf("Name\t\tReg. Number\tMarks\tGrade\n");
while (current != NULL) {
printf("%s\t\t%d\t\t%d\t%s\n", current->name, current->regNumber, current-
>marks, current->grade);
current = current->next;
}
printf("\n");
}

Node* searchNodeByName(Node* head, char name[]) {


Node* current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current = current->next;
}
return NULL;
}

void searchStudentsByGrade(Node* head, char grade[]) {


Node* current = head;
printf("Students with grade %s:\n", grade);
printf("Name\t\tReg. Number\tMarks\tGrade\n");
while (current != NULL) {
if (strcmp(current->grade, grade) == 0) {
printf("%s\t\t%d\t\t%d\t%s\n", current->name, current->regNumber,
current->marks, current->grade);
}
current = current->next;
}
printf("\n");
}

void deleteNode(Node** head, char name[]) {


Node* current = *head;
Node* prev = NULL;

if (current != NULL && strcmp(current->name, name) == 0) {


*head = current->next;
free(current);
printf("Record deleted successfully!\n");
return;
}

while (current != NULL && strcmp(current->name, name) != 0) {


prev = current;
current = current->next;
}

if (current == NULL) {
printf("Record not found!\n");
return;
}

prev->next = current->next;
free(current);
printf("Record deleted successfully!\n");
}

void freeList(Node* head) {


Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
}

int main() {
Node* head = NULL;
char name[100];
int regNumber, marks;
char choice;
char grade[3];

// Input student data and create linked list


for (int i = 0; i < 4; i++) {
printf("Enter details for student %d:\n", i+1);
printf("Name: ");
scanf("%s", name);
printf("Registration Number: ");
scanf("%d", &regNumber);
printf("Marks: ");
scanf("%d", &marks);
printf("\n");

Node* newNode = createNode(name, regNumber, marks);


insertNode(&head, newNode);
}

// Display initial list


displayList(head);

do {
printf("Menu:\n");
printf("1. Search student by name\n");
printf("2. Search students by grade\n");
printf("3. Delete student record\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);

switch (choice) {
case '1':
printf("Enter student name to search: ");
scanf("%s", name);
Node* result = searchNodeByName(head, name);
if (result != NULL) {
printf("Record found!\n");
printf("Name\t\tReg. Number\tMarks\tGrade\n");
printf("%s\t\t%d\t\t%d\t%s\n", result->name, result->regNumber,
result->marks, result->grade);
} else {
printf("Record not found!\n");
}
break;

case '2':
printf("Enter grade to search: ");
scanf("%s", grade);
searchStudentsByGrade(head, grade);
break;
case '3':
printf("Enter student name to delete: ");
scanf("%s", name);
deleteNode(&head, name);
break;

case '4':
printf("Exiting program.\n");
break;

default:
printf("Invalid choice!\n");
}

printf("\n");

} while (choice != '4');

// Free allocated memory


freeList(head);

return 0;
}

You might also like