C Linked List Student Data Management Systems

Posted by Anonymous and classified in Computers

Written on in English with a size of 14.42 KB

C Linked List Implementations for Student Data

This document presents two distinct C language implementations for managing student records using linked lists: a singly linked list and a circular linked list. Both examples demonstrate fundamental data structure operations such as adding, deleting, searching, and displaying student information.

1. Singly Linked List Implementation

This section details a student management system built using a singly linked list. Students are stored in ascending order of their roll numbers, and duplicate roll numbers are prevented.

Core Structure and Global Head

The Student structure defines the data for each node, including roll number, total marks, average, and a pointer to the next student. The head pointer always points to the first node in the list.

#include <stdio.h>
#include <stdlib.h>

typedef struct Student {
    int rollnumber;
    int totalMarks;
    float average;
    struct Student* next;
} Student;

Student* head = NULL;

Function: Create Student Node

The createStudentNode function allocates memory for a new student node and initializes its fields.

Student* createStudentNode(int rollnumber, int totalMarks, float average) {
    Student* newNode = (Student*)malloc(sizeof(Student));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    newNode->rollnumber = rollnumber;
    newNode->totalMarks = totalMarks;
    newNode->average = average;
    newNode->next = NULL;
    return newNode;
}

Function: Add Student (Singly Linked List)

The addStudentSingly function inserts a new student into the list while maintaining sorted order by roll number and checking for duplicates.

void addStudentSingly(int rollnumber, int totalMarks, float average) {
    Student* newNode = createStudentNode(rollnumber, totalMarks, average);

    // Case 1: List is empty or new student should be at the beginning
    if (!head || rollnumber < head->rollnumber) {
        if (head && head->rollnumber == rollnumber) {
            printf("Duplicate rollnumber %d\n", rollnumber);
            free(newNode); // Free the newly created node
            return;
        }
        newNode->next = head;
        head = newNode;
        printf("Student with rollnumber %d is added\n", rollnumber);
        return;
    }

    // Case 2: Insert in the middle or at the end
    Student* current = head;
    while (current->next && current->next->rollnumber < rollnumber) {
        current = current->next;
    }

    // Check for duplicate before insertion
    if (current->next && current->next->rollnumber == rollnumber) {
        printf("Duplicate rollnumber %d\n", rollnumber);
        free(newNode);
        return;
    }

    newNode->next = current->next;
    current->next = newNode;
    printf("Student with rollnumber %d is added\n", rollnumber);
}

Function: Delete Student (Singly Linked List)

The deleteStudentSingly function removes a student node based on their roll number.

void deleteStudentSingly(int rollnumber) {
    if (!head) {
        printf("Empty list\n");
        return;
    }

    // Case 1: Head node needs to be deleted
    if (head->rollnumber == rollnumber) {
        Student* temp = head;
        head = head->next;
        free(temp);
        printf("Student with roll number %d is deleted\n", rollnumber);
        return;
    }

    // Case 2: Search for the node to delete
    Student* current = head;
    while (current->next && current->next->rollnumber != rollnumber) {
        current = current->next;
    }

    // If student not found
    if (!current->next) {
        printf("Student with rollnumber %d not found\n", rollnumber);
        return;
    }

    // Delete the node
    Student* temp = current->next;
    current->next = current->next->next;
    free(temp);
    printf("Student with roll number %d is deleted\n", rollnumber);
}

Function: Search Student (Singly Linked List)

The searchStudentSingly function finds and displays the details of a student by their roll number.

void searchStudentSingly(int rollnumber) {
    if (!head) {
        printf("Empty list\n");
        return;
    }

    Student* current = head;
    while (current && current->rollnumber != rollnumber) {
        current = current->next;
    }

    if (!current) {
        printf("Student with rollnumber %d not found\n", rollnumber);
    } else {
        printf("%d---%d---%.2f\n", current->rollnumber, current->totalMarks, current->average);
    }
}

Function: Display All Students (Singly Linked List)

The displayStudentsSingly function prints the details of all students in the list.

void displayStudentsSingly() {
    if (!head) {
        printf("Empty list\n");
        return;
    }

    Student* current = head;
    while (current) {
        printf("%d---%d---%.2f\n", current->rollnumber, current->totalMarks, current->average);
        current = current->next;
    }
}

Main Function for Singly Linked List Operations

This main function provides a menu-driven interface to interact with the singly linked list student management system.

int main_singly() {
    int choice;
    printf("\n--- Singly Linked List Student Management ---\n");
    printf("1. Add Student\n");
    printf("2. Delete Student\n");
    printf("3. Search Student\n");
    printf("4. Display All Students\n");
    printf("5. Exit\n");

    while (scanf("%d", &choice) != EOF) {
        if (choice == 1) {
            int rollnumber, totalMarks;
            float average;
            printf("Enter roll number, total marks, and average: ");
            scanf("%d %d %f", &rollnumber, &totalMarks, &average);
            addStudentSingly(rollnumber, totalMarks, average);
        } else if (choice == 2) {
            int rollnumber;
            printf("Enter roll number to delete: ");
            scanf("%d", &rollnumber);
            deleteStudentSingly(rollnumber);
        } else if (choice == 3) {
            int rollnumber;
            printf("Enter roll number to search: ");
            scanf("%d", &rollnumber);
            searchStudentSingly(rollnumber);
        } else if (choice == 4) {
            printf("\n--- Student List ---\n");
            displayStudentsSingly();
        } else if (choice == 5) {
            printf("Exiting Singly Linked List program.\n");
            break;
        } else {
            printf("Invalid choice\n");
        }
        printf("\nEnter your choice: ");
    }
    return 0;
}

2. Circular Linked List Implementation

This section presents an alternative student management system using a circular linked list. In this structure, the last node points back to the first node, forming a circle. This implementation also maintains sorted order by roll number and handles duplicates.

Core Structure and Global Head

Similar to the singly linked list, but the next pointer of the last node will point to the head.

#include <stdio.h>
#include <stdlib.h>

typedef struct Student {
    int rollnumber;
    int totalMarks;
    float average;
    struct Student* next;
} Student;

Student* head = NULL;

Function: Create Student Node

The createStudentCircular function allocates and initializes a new student node.

Student* createStudentCircular(int roll, int total, float avg) {
    Student* newStudent = (Student*)malloc(sizeof(Student));
    if (newStudent == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    newStudent->rollnumber = roll;
    newStudent->totalMarks = total;
    newStudent->average = avg;
    newStudent->next = NULL;
    return newStudent;
}

Function: Check for Existence

The exists function checks if a student with a given roll number already exists in the circular list.

int exists(int roll) {
    if (head == NULL) return 0;
    Student* temp = head;
    do {
        if (temp->rollnumber == roll) return 1;
        temp = temp->next;
    } while (temp != head);
    return 0;
}

Function: Add Student (Circular Linked List)

The addStudentCircular function inserts a new student into the circular list, maintaining sorted order and preventing duplicates.

void addStudentCircular(int roll, int total, float avg) {
    if (exists(roll)) {
        printf("Duplicate rollnumber %d\n", roll);
        return;
    }
    Student* newStudent = createStudentCircular(roll, total, avg);

    // Case 1: List is empty
    if (head == NULL) {
        head = newStudent;
        head->next = head; // Point to itself for a single node circular list
    } 
    // Case 2: New student should be the new head (smallest roll number)
    else if (roll < head->rollnumber) {
        Student* temp = head;
        while (temp->next != head) temp = temp->next; // Find the last node
        temp->next = newStudent;
        newStudent->next = head;
        head = newStudent;
    } 
    // Case 3: Insert in the middle or at the end
    else {
        Student* current = head;
        while (current->next != head && current->next->rollnumber < roll)
            current = current->next;
        newStudent->next = current->next;
        current->next = newStudent;
    }
    printf("Student with rollnumber %d is added\n", roll);
}

Function: Delete Student (Circular Linked List)

The deleteStudentCircular function removes a student node by their roll number from the circular list.

void deleteStudentCircular(int roll) {
    if (head == NULL) {
        printf("Empty list\n");
        return;
    }

    Student *current = head, *prev = NULL;
    do {
        if (current->rollnumber == roll) {
            // Case 1: Only one node in the list
            if (current == head && current->next == head) {
                free(current);
                head = NULL;
            } 
            // Case 2: Deleting the head node (more than one node)
            else if (current == head) {
                Student* temp = head;
                while (temp->next != head) temp = temp->next; // Find the last node
                head = head->next;
                temp->next = head;
                free(current);
            } 
            // Case 3: Deleting a node in the middle or end
            else {
                prev->next = current->next;
                free(current);
            }
            printf("Student with rollnumber %d is deleted\n", roll);
            return;
        }
        prev = current;
        current = current->next;
    } while (current != head);
    printf("Student with rollnumber %d not found\n", roll);
}

Function: Search Student (Circular Linked List)

The searchStudentCircular function locates and displays a student's details by their roll number in the circular list.

void searchStudentCircular(int roll) {
    if (head == NULL) {
        printf("Empty list\n");
        return;
    }
    Student* temp = head;
    do {
        if (temp->rollnumber == roll) {
            printf("%d---%d---%.2f\n", temp->rollnumber, temp->totalMarks, temp->average);
            return;
        }
        temp = temp->next;
    } while (temp != head);
    printf("Student with rollnumber %d not found\n", roll);
}

Function: Display All Students (Circular Linked List)

The displayStudentsCircular function iterates through and prints the details of all students in the circular list.

void displayStudentsCircular() {
    if (head == NULL) {
        printf("Empty list\n");
        return;
    }
    Student* temp = head;
    do {
        printf("%d---%d---%.2f\n", temp->rollnumber, temp->totalMarks, temp->average);
        temp = temp->next;
    } while (temp != head);
}

Main Function for Circular Linked List Operations

This main function provides a menu-driven interface for the circular linked list student management system.

int main_circular() {
    int choice;
    printf("\n--- Circular Linked List Student Management ---\n");
    printf("1. Add Student\n");
    printf("2. Delete Student\n");
    printf("3. Search Student\n");
    printf("4. Display All Students\n");
    printf("5. Exit\n");

    while (scanf("%d", &choice) != EOF) { // Changed from while(scanf("%d", &choice)) to handle EOF
        if (choice == 1) {
            int roll, total;
            float avg;
            printf("Enter roll number, total marks, and average: ");
            scanf("%d %d %f", &roll, &total, &avg);
            addStudentCircular(roll, total, avg);
        } else if (choice == 2) {
            int roll;
            printf("Enter roll number to delete: ");
            scanf("%d", &roll);
            deleteStudentCircular(roll);
        } else if (choice == 3) {
            int roll;
            printf("Enter roll number to search: ");
            scanf("%d", &roll);
            searchStudentCircular(roll);
        } else if (choice == 4) {
            printf("\n--- Student List ---\n");
            displayStudentsCircular();
        } else if (choice == 5) {
            printf("Exiting Circular Linked List program.\n");
            break;
        } else {
            printf("Invalid choice\n");
        }
        printf("\nEnter your choice: ");
    }
    return 0;
}

How to Compile and Run

To compile and run these examples, save the code for each implementation (e.g., singly_list.c and circular_list.c) separately. You can then compile them using a GCC compiler:

  • For Singly Linked List: gcc singly_list.c -o singly_list
  • For Circular Linked List: gcc circular_list.c -o circular_list

Then, run the executables:

  • ./singly_list
  • ./circular_list

Follow the on-screen prompts to interact with the student management system.

Related entries: