Student Record Management System Using Linked List

We know that linked lists have so many applications and uses. As we can include a number of different elements in the linked list it can be used in data management as well. Lets just see an approach for student management system using linked list

Student Management System Using Linked List

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 of Student Management System Using Linked List

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 of student management system using linked list: O(1)

Space complexity of student management system using linked list: 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 of student management system using linked list: O(n), where n is the number of Student nodes in the linked list.

Space complexity of student management system using linked list: 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 of student management system using linked list: O(n), where n is the number of Student nodes in the linked list.

Space complexity of linked list to store student details: 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 of linked list to store student details: O(n), where n is the number of Student nodes in the linked list.

Space complexity of linked list to store student details: 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 of of student management system using linked list



#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.

Conclusion

Spend some time understanding the whole program on how to use linked list to store student details 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 Management System Using Linked List. I would highly recommend you to practice some problems on the linked list from PrepBytes.

Related Articles
Doubly circular linked list in data structure Application of doubly linked list
Applications of linked list Singly linked list vs doubly linked list
Advantages and disadvantages of linked list Doubly linked list all operations in C
Binary search in linked list Bubble sort linked list
Deletion in doubly linked list Delete the middle node of a linked list
Polynomial addition using linked list Find max value and min value in linked list
Insert a node at a specific position in a linked list Swap nodes in linked list
Add two numbers represented by linked lists Find starting point of loop in linked list
Merge sort linked list Delete a node from linked list at a given position
Remove duplicates from unsorted linked list Reverse a linked list in Python

FAQs

  1. What are the limitations of linked lists in the data structure?
    The use of pointers is very high, which requires more memory.

  2. Why the linked lists known as Dynamic data structure?
    Unlike arrays, the memory need not to be pre decided as the linked list can change its size anytime.

  3. Where is the linked list stored?
    The linked list is not stored in a contiguous memory-like array, it is linked through the pointers. It’s a linear data structure implemented on the heap memory.

Leave a Reply

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