Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Objective : Build a c++ program to solve a problem using linked list OR linea

ID: 3833886 • Letter: 1

Question

1) Objective :
Build a c++ program to solve a problem using linked list OR linear list. The program shall have all basic list operations including searching and sorting.

2) Instruction :
a) Write a program to represent any record keeping system (e.g Stock, Students Records, Employee Records, Song List, Video Rental, etc ) using list concept.
Your program should have the following functionality: Login & Password Add Record Delete Record Display Records Search Record Sort Record
b) The record should have more than one data field e.g Student Record (Name, ID, Age, GPA, etc.)
c) Your program should have menu display to allow user control. Examples of menu items : Add new record Delete record Display record by name Display record by xxxx Monthly Sales Report Sales by product Search product by ID Sort product by ID Exit from program

Explanation / Answer

/* Assuming there is a user login-password file(password.txt) with entries as login password. */

#include<iostream.h>
#include<string>
#include<vector>

using nmamespace std;

struct Student {
    int id;
    String name;
    struct Student *next;
};

bool login(string name, string password){
    ifstream infile;
    int found;
    string stu_name, stu_pass;
    infile.open("password.txt");
    found = 0;
    while (infile >> name >> password) {
        if (stu_name == name){
           if (stu_pass == password)
              return true;
           else
        }
    }
    infile.close();
    return false;
}


void addRecord(struct Student *start, struct Student record){
     struct Student *p;
     p = start;
     if (p == NULL){
         p = new(struct Student);
         *p = reord;
         p->next = NULL;
     }
     while (p->next != NULL)
         p = p->next;
     p->next = new(struct Student);
     p = p->next;
     *p = record;
     p->next = NULL;  
}

void deleteRecord(struct Student *start, int id){
     struct Student *p, *q;
     p = start;
     if (p == NULL){
        cout << "List is empty" << endl
        return;
     }
     if (p->id == id){
        start = NULL;
        return;
     }
     while (p != NULL){
         if (p->id == id)
            break;
         p = p->next;
     }
     if (p == NULL)
        cout << "No records with the ID" << endl;
     else {
        q = start;
        while (q-next != p)
              q = q->next;
        q->next = p->next;
        cout << "Element deleted" << endl;  
     }
}

void searchRecord(struct Student *start, int id){
     struct Student *p, *q;
     p = start;
     if (p == NULL){
        cout << "List is empty" << endl
        return;
     }
     while (p != NULL){
         if (p->id == id){
            cout << "ID:" << p->id << endl;
            cout << "Name:" << p->name; << endl;
            break;
         }
         p = p->next;
     }
}

void display(struct Student *start){
     struct Student *p;
     p = start;
     while (p != NULL){
            cout << "ID:" << p->id << endl;
            cout << "Name:" << p->name; << endl;   
            p = p->next;   
     }
}

void sortbyID(struct Studen *start){
     struct Student *p, *q, *temp;
     int count;
     count = 0;
     p = start;
     while (p != NULL){
         p = p->next;
         count++;
     }
     if (count == 1 || count == 0)
        return;
     for (int i=0; i<count; i++){
        p = start;
        q = p->next;
        while (q != NULL){
            if (p->id > q->id){
               temp = new(struct Student);
               temp->id = p->id;
               temp->name = p->name;
               p->id = q->id;
               p->name = q->name;
               q->id = temp->id;
               q->name = temp->name;
            }
            p = p->next;
            q = q->next;
        }
     }

}


void main() {

   struct Student *start;
   struct Student data;
   start = NULL;
   char ch;
   string name;
   string password;
  
   ch = 'p';
   while (ch != '2'){
       cout << "1.Login" << endl;
       cout << "2.Quit" << endl;
       cin >> ch;
       if (ch == '1'){
          cout << "Enter name:" << endl;
          cin >> name;       
          cout << "Enter password:" << endl;
          cin >> password;
          if (login(name,password)){
             ch = '7';
             while (ch != '5') {
                 cout << "1.Add a record" << endl;
                 cout << "2.Delete a rtecord" << endl;
                 cout << "3.Search a record by ID" << endl;
                 cout << "4.Sort by ID" << endl;
                 cout << "5.Display list" << endl;
                 cout << "6.Quit" << endl;
                 cout << "Enter choice:" << endl;
                 cin >> ch;
                 switch (ch) {
                     case '1':
                        cout << "Enter ID:" << endl;
                        cin >> data.id;
                        cout << "Enter name: << endl
                        cin >> data.name;
                        addRecord(start, data);
                     case '2':
                        cout << "Enter ID:" << endl;
                        cin >> data.id;
                        deleteRecord(start, id);
                     case '3':
                        cout << "Enter ID:" << endl;
                        cin >> data.id;
                        searchRecord(start, id);
                     case '4':
                        sort(start);
                        cout << "Sorting done" << endl;
                     case '5':
                        display(start);
                       
                 }
                 
          
          }
       }
}