Kanika0503 / DataStructures_KS

5 stars 0 forks source link

Storing and accessing student data using single linked list #2

Open Kanika0503 opened 3 months ago

Kanika0503 commented 3 months ago

You are tasked with creating a program to manage a student database for a small school. Each student record consists of the following information:

Student ID Name Age Grade Next pointer (to the next student record) Requirements: The program should allow the user to: Add a new student record, Delete a student record, Display all student records The program should use a linked list data structure to store and manage student records.

yadavikash19 commented 3 months ago

include

include

using namespace std;

struct node { string name; int age; char grade; struct node next; }start=NULL,nn,temp,*loc;

void insert_beg(string n, int v,char g) { nn = new node; nn -> name = n; nn -> age = v; nn -> grade = g; nn -> next = NULL;

if(start == NULL){
    start = nn;
}

else
{
    nn -> next = start;
    start = nn;
}

}

void insert_end(string n, int v,char g) { nn = new node; nn -> name = n; nn -> age = v; nn -> grade = g; nn -> next = NULL;

if(start == NULL){
    start = nn;
}

else
{
    temp =start;

    while(temp -> next != NULL) {
        temp = temp -> next;
    }

    temp -> next = nn;
}

}

void insert_bet(string n, int v,char g,int z){ nn = new node; nn -> name = n; nn -> age = v; nn -> grade = g; nn -> next = NULL;

if(start == NULL){
    start = nn;
}

else {
    temp = start;

    while(temp -> next -> age ==z){
        temp = temp -> next;
    }

    nn -> next = temp -> next;
    temp -> next = nn;
}

}

void delete_beg() { temp = start; start = start -> next; delete temp; }

void delete_end() { temp = start;

while(temp -> next -> next != NULL) {
    temp = temp -> next;
}

delete temp -> next;
temp->next = NULL;

}

void delete_ele(int v) { temp = start; while(temp -> next -> age != v) { temp = temp -> next; } loc = temp -> next; temp -> next = loc -> next; delete loc; }

void traverse() { temp = start;

while(temp != NULL) {
    cout<<"NAME IS "<<temp -> name<<"\t"<<"AGE IS "<<temp -> age<<"\t"<<"Grade is "<<temp -> grade<<endl;
    temp = temp -> next;
} }

int main() { int value, choice; char yeh_nah,grad; string stu;

do{
    cout<<"Enter 1 to Insert a Student data at beginning"<<endl;
    cout<<"Enter 2 to Insert a Student data at the end"<<endl;
    cout<<"Enter 3 to Insert a Student data in between"<<endl;
    cout<<"Enter 4 to Delete a Student data in beginning"<<endl;
    cout<<"Enter 5 to Delete a Student data in the end"<<endl;
    cout<<"Enter 6 to Delete any Student data"<<endl;

    cout<<"Enter Choice = ";
    cin>>choice;

    switch (choice)
    {
    case 1:
        cout<<"Enter name you want to add to data = "<<endl;
        cin>> stu;
        cout<<"Enter his/her age = "<<endl;
        cin>> value;
        cout<<"Enter his/her grade = "<<endl;
        cin>> grad;

        insert_beg(stu,value,grad);

        break;

    case 2:
        cout<<"Enter name you want to add to data = "<<endl;
        cin>> stu;
        cout<<"Enter his/her age = "<<endl;
        cin>> value;
        cout<<"Enter his/her grade = "<<endl;
        cin>> grad;

        insert_end(stu,value,grad);

        break;

    case 3:
    int value2;
        cout<<"Enter name you want to add to data = "<<endl;
        cin>> stu;
        cout<<"Enter his/her age = "<<endl;
        cin>> value;
        cout<<"Enter his/her grade = "<<endl;
        cin>> grad;
        cout<<"After what age? = "<<endl;
        cin>>value2;
        insert_bet(stu,value,grad,value2);

        break;

    case 4:
    delete_beg();

    break;

    case 5:
    delete_end();

    break;

    case 6:
    cout<<"what age of student you want to delete = ";
    cin>>value;

    delete_ele(value);

    break;

    default:

    cout<<"Wrong Choice Entered";
        break;

    }

    cout<<endl<<"Do you want to continue(Y/N)? = ";
    cin>>yeh_nah;

}while(yeh_nah == 'Y' || yeh_nah == 'y');

cout<<"Final list is - "<<endl; traverse();

return 0; }

Venukrishna007 commented 2 months ago

include

include

using namespace std;

struct node{ string name; int age; char grade; struct node next; }start=NULL,nn,temp,*loc;

void Insert(int POS, string n, int a, char g)//In POS Element is Inserted at the POS Value. { nn = new node; nn->name = n; nn->age = a; nn->grade = g;

if (start == NULL || POS == 1) {
    nn->next = start;
    start = nn;

} else {
    temp = start;
    int count = 1;

    while (temp->next != NULL && count < POS - 1) {
        temp = temp->next;
        count++;

    }

    nn->next = temp->next;
    temp->next = nn;
}

}

void Delete(int POS) { if(start==NULL) { cout<<"No Records in the List to Delete"<<endl;

}
else
{
    temp=start;
    if(POS==1)
    {
    start=temp->next;
    delete temp;
    cout<<"Record at "<<POS<<" is deleted"<<endl;
    return;
    }

   temp=start;
   int count=1;
   bool found=false;
   while(temp->next!=NULL && count < POS-1)
   {
       temp=temp->next;
       count++;
   }

   if(count <POS-1 || temp->next==NULL)
   {
       cout<<"Invalid Position"<<endl;
       return;
    }

   loc=temp->next;
   temp->next=loc->next;
   delete loc;
   found = true;
   cout<<"Record at "<<POS<<" is deleted"<<endl;
    if(!found)
    {
        cout<<"No Record found at "<<POS<<"to delete"<<endl;
    }

}

}

void Display(){ if(start==NULL) { cout<<"No Records are Present Currently"<<endl; }

else
{
temp=start;
int count=1;
while(temp!=NULL)
{

    cout<<count<<"."<<temp->name<<"\t"<<"    "<<temp->age<<"\t"<<temp->grade<<endl;
    temp=temp->next;
    count++;
}
}

}

int main() { int ch,POS,age; string name; char grade,choice; do{ cout<<"WELCOME TO THE STUDENT DATABASE"<<endl; cout<<"1.Press 1 to Insert a New Record"<<endl; cout<<"2.Press 2 to Delete an Existing Record"<<endl; cout<<"3.Press 3 to Display all Records"<<endl; cin>>ch; switch(ch) { case 1: Display(); cout<<"Enter the Position where the record should be Inserted"<<endl; cin>>POS; cout<<"Enter the Name of the student"<<endl; cin>>name; cout<<"Enter the Age of the Student"<<endl; cin>>age; cout<<"Enter the Grade of the Student"<<endl; cin>>grade; Insert(POS,name,age,grade); cout<<"The current Student Records are: "<<endl; Display(); break;

case 2: Display(); cout<<"Enter the Position where the record should be Deleted"<<endl; cin>>POS; Delete(POS); cout<<"The current Student Records are: "<<endl; Display(); break;

case 3: cout<<"The Records in the Student Database are: "<<endl; Display(); break;

default: cout<<"Invalid choice. Please Enter a Valid option."<<endl; break; } cout<<"Do you want to Insert or Delete more records?"<<endl; cout<<"Press (Y/N)"<<endl; cin>>choice; } while(choice=='Y' || choice=='y');

return 0;

}

Harshak2058 commented 2 months ago

include

using namespace std;

struct Student { int studentID; string name; int age; char grade; Student next; } start = NULL, temp, nn, *loc;

void addStudent(int id, string name, int age, char grade) { nn = new Student; nn->studentID = id; nn->name = name; nn->age = age; nn->grade = grade; nn->next = NULL;

if (start == NULL) {
    start = nn;
    return;
}
else if (id < start->studentID) {
    nn->next = start;
    start = nn;
    return;
}
else {
    temp = start;
    while (temp->next != NULL) {
        if (temp->studentID < id && (temp->next->studentID > id || temp->next->studentID == id + 1))
           /* If the current student's ID is less than the new student's ID
           and the next student's ID is greater than the new student's ID
           or one greater than the new student's ID, insert the new student
           after the current student */
            {
            nn->next = temp->next;
            temp->next = nn;
            return;
        }
        temp = temp->next;
    }
    temp->next = nn;
}

}

void deleteStudent(int id) { if (start == NULL) { cout << "Student database is empty." << endl; return; } else if (start->studentID == id) { temp = start; start = start->next; delete temp; cout << "Student record with ID " << id << " has been deleted." << endl; return; } else { loc = start; temp = start->next; while (temp != NULL) { if (temp->studentID == id) { loc->next = temp->next; delete temp; cout<<""<<endl; cout << "Student record with ID " << id << " has been deleted." << endl; cout<<""<<endl; return; } loc = temp; temp = temp->next; } } cout<<""<<endl; cout << "Student record with ID " << id << " not found." << endl; cout<<""<<endl; }

void displayStudents() { if (start == NULL) { cout << "Student database is empty." << endl; return; } else {

    cout << "Student Records:" << endl;
    cout << "-------------------------------------------------------" << endl;
    cout<<"ID"<<"------"<<"Name"<<"----------"<<"age"<<"--------"<<"grade"<<endl;
    cout << "-------------------------------------------------------" << endl;
    temp = start;
    while (temp != NULL) {

        cout << "ID: " << temp->studentID << ", Name: " << temp->name << ", Age: " << temp->age << ", Grade: " << temp->grade << endl;
        temp = temp->next;
    }
}

}

void deleteAllStudents() { while (start != NULL) { temp = start; start = start->next; delete temp; } }

int main() {

addStudent(30, "harsha", 18, 'A');
addStudent(32, "avi", 21, 'A');
addStudent(31, "kally", 19, 'B');
addStudent(143, "hk", 20, 'A');
addStudent(50, "lohi", 22, 'B');
addStudent(34, "likki", 23, 'C');
addStudent(36, "kittu", 24, 'D');

/if student get random id's and got admission. it can be sorted as per order in addStudent function else part logic/ displayStudents();

deleteStudent(2);// student id with 2 is not there so it will not be found

displayStudents();
deleteStudent(143);//now it will delete the student with id 143

//cout << "----" << endl; displayStudents(); cout << "----" << endl;

deleteAllStudents();
cout << "----" << endl;
displayStudents();
cout << "----" << endl;

return 0;

}

kassoumyeo commented 2 months ago

include

include

include

using namespace std;

struct Node { int studentID; string name; int age; int grade; Node next; }; Node head = nullptr;

Node createNode(int id, string name, int age, int grade) { Node newNode = new Node; newNode->studentID = id; newNode->name = name; newNode->age = age; newNode->grade = grade; newNode->next = nullptr; return newNode; }

void insertStudent(int id, string name, int age, int grade) { Node newNode = createNode(id, name, age, grade); if (head == nullptr) { head = newNode; } else { Node temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } }

void deleteStudent(int id) { if (head == nullptr) { cout << "Student database is empty." << endl; return; } if (head->studentID == id) { Node temp = head; head = head->next; delete temp; cout << "Student with ID " << id << " has been deleted." << endl; return; } Node prev = head; Node* curr = head->next; while (curr != nullptr) { if (curr->studentID == id) { prev->next = curr->next; delete curr; cout << "Student with ID " << id << " has been deleted." << endl; return; } prev = curr; curr = curr->next; } cout << "Student with ID " << id << " not found." << endl; }

void displayStudents() { if (head == nullptr) { cout << "Student database is empty." << endl; return; } Node* temp = head; while (temp != nullptr) { cout << "Student ID: " << temp->studentID << ", Name: " << temp->name << ", Age: " << temp->age << ", Grade: " << temp->grade << endl; temp = temp->next; } }

int main() { int choice, id, age, grade; string name;

while (true) {
    cout << "\nMenu:\n";
    cout << "1. Add a new student record\n";
    cout << "2. Delete a student record\n";
    cout << "3. Display all student records\n";
    cout << "4. Exit\n";
    cout << "Enter your choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
            cout << "Enter student ID: ";
            cin >> id;
            cout << "Enter student name: ";
            cin.ignore();
            getline(cin, name);
            cout << "Enter student age: ";
            cin >> age;
            cout << "Enter student grade: ";
            cin >> grade;
            insertStudent(id, name, age, grade);
            break;
        case 2:
            cout << "Enter student ID to delete: ";
            cin >> id;
            deleteStudent(id);
            break;
        case 3:
            cout << "Student records:\n";
            displayStudents();
            break;
        case 4:
            cout << "Exiting program." << endl;
            return 0;
        default:
            cout << "Invalid choice. Please try again." << endl;
    }

    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

return 0;

}