# 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
}
```

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
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
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
cout<<"Record is empty\n";
return;
}

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

hr();

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
}

// to display data of one student
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
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();

// 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()){
hr();
goto again;
}

hr();
break;
}

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

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

hr();
break;
}

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

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

// To handle invalid choice
default: {
hr();