Last Updated on March 16, 2023 by Harsh Yadav
A linked list is a fundamental data structure used in computer science and programming. It is a dynamic data structure that consists of a sequence of elements, each of which contains a reference to the next element in the sequence.Linked lists have many applications in computer science and programming. One common application of linked lists is in the implementation of data structures such as stacks, queues, and hash tables.In this section we will discuss the 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
#includeusing 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.
FAQs
-
What are the limitations of linked lists in the data structure?
The use of pointers is very high, which requires more memory. -
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. -
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. -
How can a student management system using linked lists be accessed and updated?
A student management system using linked lists can be accessed and updated through a software interface, which allows authorized users to view and edit student information. The interface can be designed to include features such as search and filter options, data entry forms, and reports. -
What are some potential challenges of implementing a student management system using linked lists?
One potential challenge of implementing a student management system using linked lists is ensuring data accuracy and consistency. This can be addressed through careful design of the data entry and validation processes, as well as by implementing appropriate data security measures. Another challenge is ensuring that the system is scalable and can handle large volumes of student data, which may require optimization of the data storage and retrieval algorithms.