Student Record Management System Using Linked List

Problem statement

Implement a student record management system using a linked list.
The program should be able to store the following details of the student:
1) Registration number
2) Name
3) Branch
4) Contact

And offer the following functionalities:
1) Create a new student record
2) Find a record for a given student’s reg. No.
3) Delete a record
4) Show all student records
5) Exit the program

Approach

This problem will be a very good way to understand how a linked list can be used in real-world applications.

Before moving ahead you must know how basic operations such as insertion, deletion of nodes, and iteration performed on a linked list. A student’s record can be stored in a node and such nodes can be connected together to form a linked list.

The node needs to be defined to store all the required details of a student as follows:


struct Student {
    string reg;
    string name;
    string branch;
    int contact;

    Student* next;

    Student(string name, string reg, string branch, int contact){
        this->name = name;
        this->reg  = reg;
        this->branch  = branch;
        this->contact = contact;
    }
};

Here we also defined a constructor to be able to create a Student node by passing their values together.

1. Function to add a new Student

We will simply add a new Student node to the front of the linked list.



// to add a new student’s record
void create(Student** head, string name, string reg, string branch, int contact){
    // creating a new node
    Student* new_student = new Student(name, reg, branch, contact);

    // inserting the new node at the beginning
    new_student->next = *head;
    *head = new_student;
}

Time complexity: O(1)
Space complexity: O(1)

2. Function to find a student using Registration Number

Simply iterate through the linked list and find the matching Student node with the given registration number. If found, print the data in it.



// to display data of one student
void showOne(Student* head, string reg){
    Student* i = head;
    while(i!=NULL){
        if(i->reg == reg){
            cout<<"Reg\t"<reg<<"\n";
            cout<<"Name\t\t"<name<<"\n";
            cout<<"Branch\t"<branch<<"\n";
            cout<<"Contact\t"<contact<<"\n";
            return;
        }
    }
    cout<<"No record found for reg.no. "<




						 

Time complexity: O(n), where n is the number of Student nodes in the linked list.

Space complexity: O(1)

3. Function to delete a student’s record with a given Registration Number

Iterate through the linked list and find the matching node with the given registration number. Simply remove that node from the linked list.



// to delete a record
void remove(Student** head, string reg){
    Student *i = *head, *prev = NULL;
    Student* temp;
    while(i!=NULL){
        if(i->reg == reg){
            cout<<"Record with reg.no. "<next;
                delete temp;
                return;
            }

            temp = i;
            prev->next = i->next;
            delete temp;
            return;
        }
        i = i->next;
    }
    cout<<"No record found for reg.no. "<



						 

Time complexity: O(n), where n is the number of Student nodes in the linked list.

Space complexity: O(1)

4. Function to display all Student's record

One by one iterate in the linked list and print the data contained within the nodes.



// to display all data of all the students
void showAll(Student* head){
    if(head==NULL){
        cout<<"Record is empty\n";
        return;
    }

    cout<<"Reg"<<"\t| ";
    cout<<"Name"<<"\t\t| ";
    cout<<"Branch"<<"\t| ";
    cout<<"Contact"<<"\t\n";

    hr();

    Student* i = head;
    while(i!=NULL){
        cout<reg<<"\t| ";
        cout<name<<"\t\t| ";
        cout<branch<<"\t\t| ";
        cout<contact<<"\t\n";

        i = i->next;
    }
}

Time complexity: O(n), where n is the number of Student nodes in the linked list.

Space complexity: O(1)

We can organize these functions and add user interaction by allowing the users to choose the operations to perform by taking some user input.

All these functions working together along with the user interaction can be implemented as:

Implementation



#include
using namespace std;

struct Student {
    string reg;
    string name;
    string branch;
    int contact;

    Student* next;

    Student(string name, string reg, string branch, int contact){
        this->name = name;
        this->reg  = reg;
        this->branch  = branch;
        this->contact = contact;
    }
};

void hr(){
    cout<<"----------------------------------------------------------------------";
    cout<<"\n";
}

// to add a new student’s record
void create(Student** head, string name, string reg, string branch, int contact){
    // creating a new node
    Student* new_student = new Student(name, reg, branch, contact);

    // inserting the new node at the beginning
    new_student->next = *head;
    *head = new_student;
}


// to display data of one student
void showOne(Student* head, string reg){
    Student* i = head;
    while(i!=NULL){
        if(i->reg == reg){
            cout<<"Reg\t"<reg<<"\n";
            cout<<"Name\t\t"<name<<"\n";
            cout<<"Branch\t"<branch<<"\n";
            cout<<"Contact\t"<contact<<"\n";
            return;
        }
    }
    cout<<"No record found for reg.no. "<reg<<"\t| ";
        cout<name<<"\t\t| ";
        cout<branch<<"\t\t| ";
        cout<contact<<"\t\n";

        i = i->next;
    }
}


// to delete a record
void remove(Student** head, string reg){
    Student *i = *head, *prev = NULL;
    Student* temp;
    while(i!=NULL){
        if(i->reg == reg){
            cout<<"Record with reg.no. "<next;
                delete temp;
                return;
            }

            temp = i;
            prev->next = i->next;
            delete temp;
            return;
        }
        i = i->next;
    }
    cout<<"No record found for reg.no. "<>task;

        validate_input();

        switch (task){

            // To add a new student
            case 1: {

                again:
                cout<<"Enter student name: ";
                cin>>name;
                cout<<"Enter registration number: ";
                cin>>reg;
                cout<<"Enter branch name: ";
                cin>>branch;
                cout<<"Enter contact number: ";
                cin>>contact;

                if(!validate_input()){
                    cout<<"Please re-enter the details\n";
                    hr();
                    goto again;
                }


                create(&head, name,reg,branch,contact);
                hr();
                break;
            }

            // To search a student
            case 2: {
                cout<<"Enter registration number: ";
                cin>>reg;
                hr();
                showOne(head, reg);
                hr();
                break;
            }

            // To remove a student
            case 3: {
                cout<<"Enter registration number: ";
                cin>>reg;

                remove(&head, reg);
                hr();
                break;
            }

            // To view all the records
            case 4: {
                cout<<"All students: \n";
                hr();
                showAll(head);
                hr();
                break;
            }

            // To exit
            case 5: {
                exit = true;
                hr();
                hr();
                break;
            }

            // To handle invalid choice
            default: {
                hr();
                cout<<"Please enter a valid choice\n";
                hr();
                break;
            }
        }
    }
}

I have made the output look good by adding some horizontal lines. Also, I have handled the error caused when we are taking input for a variable of type int and entering something other than int as input. I did so by implementing a validate_input() function which returns true only if values are entered as expected else returns false.

Spend some time understanding the whole program and I would encourage you to write one such on your own from scratch. It would be really helpful. Through this article, we learned how to implement a Student Record Management System Using Linked List. I would highly recommend you to practice some problems on the linked list from Linked List.

Previous post Given a Linked List which is sorted, how to insert in a sorted way
Next post Print Nodes Of Linked List At Given indexes

Leave a Reply

Your email address will not be published. Required fields are marked *