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

For this lab, you will write a program to manage a simple database. This databas

ID: 3839397 • Letter: F

Question

For this lab, you will write a program to manage a simple database. This database will store Records (from your previous lab) stored as a Linked List (code provided).

You will design an efficient text-based menu to allow users to

add a new record,

delete a record,

search for an existing record, and

print out the records.

User Menu

Create a new file Project5.cpp that will contain your main() function. Create a new instance of LinkedList. This will create an (initially) empty list. Next, create a text-based user menu. The menu should repeat until the user chooses to quit. Your menu will provide the following options:

Insert

Delete

Search

Print

Quit

Insert an element - create a new record, populate it with user provided data (i.e., call input() function), and add it to the linked list.

Delete an element - prompt the user to enter an ID number. Delete the record from the linked list.

Search for an element - prompt the user to enter an ID number. Search the list for the element. If found, print it. Otherwise, print a message that the item was not found.

Print out the database (all records) - iterate over the linked list and print out each record one by one (i.e., call your display() function). You will need to create a new function printAll() in the class Linked List (see below).

Quit - exits your program.

Linked List

In this lab, you will make use of a Data Structure called a Linked List. This is an important data structure you will implement in 211. For now, I have provide you most all of the code you will need to use a Linked List. In the version I give you, the list maintains a sorted list of Records, based on the Record ID. I have linked a short video below that explains how linked list work.

Download the provided .cpp and .h files. Study the provided files until you understand how the linked list works. Notice I have provided functions to insert, remove, and search the list. I also provide a function print(), which prints out in order the IDs in the list. Add another function to the LinkedList class called printRecords(). This function will be very similar to print(), except instead of the printing only the IDs it will print out the entire Record (i.e., calls your display() function).

Makefile

Write a Makefile to compile all of your Project5 code files.

Text Input File

In order to help test your file, create a file called "input.txt" that contains text input to your program.

You must be able to run it as

./project5 < input.txt

In the above line, we are using unix to "pipe" in data from the file directly into standard in (cin). Do not use file streams (fstreams).

In this file, you will provide the input for FIVE records. That means you will choose menu option "1" (input) and provide the values for a new Record field by field (in proper order). Then repeat this for four more records.

For example, a file for a Book might look like the following:

1 // Menu option to choose "Insert"
100 // Answer to prompt for ID
Fahrenheit 451 // Answer to prompt for Name
Ray Bradbury // .....................Author
1953 // .....................Year
153 // .....................Number of pages
Ballantine Books   // .....................Publisher
978-0-7432-4722-1 // .....................ISBN
1   // Menu option to choose "Insert"
// continues for four more records

5 // Exit menu

Note: the C++ style comments provided for explanation only and would not be included in your file.

Make sure to test your input.txt file. If you get a repeating menu, add the command to exit (5) at the end.

---------------------------------------------------------------------------

This is the LinkedList.cpp file:

Explanation / Answer

Answer:

Note: Implemented the Project5.cpp. Also implemented the printRecords() in LinkedList class. Records.h is not given. So i am unable to run the code.

File Name:LinkedList.h

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include<iostream>

#include "Record.h"

using namespace std;

/*

* Node represents a spot in the linked list

* It contains a Record (the data to store) and

* a pointer to the next node in the list (the link).

*/

struct node

{

     Record data;

     node *next;    

};

/*

* A LinkedList stores pointer to first node (head).

* From there, we can "walk" along a chain of

* next pointers to any node in the list.

*/

class LinkedList

{

private:

     // The first element of the list

     node *head;    

public:

     LinkedList();

     // Print out the list (useful for debugging)

     void print();

     // Find an element of the list

     node *search(int ID);

        // Insert a new Record

     void insert(Record r);

     // Remove a Record from the list

     void remove(int ID);

     void printRecords();

};

#endif

File Name: LinkedList.cpp

#include<iostream>

#include "LinkedList.h"

#include "Record.h"

using namespace std;

// Flag to turn on/off the printing about the list operations

// 1 = ON; 0 = OFF

const int DEBUG_MODE = 1;

/*

* Constructor - creates a new empty linked list

*/

LinkedList::LinkedList()

{

     if (DEBUG_MODE){

          cout << "New (empty) Linked List created." << endl;

     }

     // list is empty

     head=NULL;

}

/*

* Prints the linked list to standard out

*/

void LinkedList::print()

{

     // start at the first node (head)

     node *temp=head;

cout<<"-------------------------------------------------- ";

     // loop until end of list (i.e., when node is NULL)

     while(temp!=NULL)

     {

          // print out the record id

          cout << temp->data.getID() << " ";

               

          // move to next node

          temp=temp->next;

     }

cout<<" -------------------------------------------------- ";

}

               

               

/*

* Search for a specific ID in the sorted list.

* Returns the node PRECEDING the spot where the

* ID should be (useful for insert() function).

*/    

node* LinkedList::search(int ID)

{

     node *temp=head;

     // loop until end of list or found place in list

     while(temp->next!=NULL && temp->next->data.getID()<ID )

     {              

          // move to next node

          temp=temp->next;

     }

     return temp;

}      

/*

* Insert a new record into the sorted linked list

*/

void LinkedList::insert(Record record)

{

     // create a new node

     node *temp = new node;

     temp->data = record;

     temp->next = NULL;

       

     // Next adjust pointers to add to list

     if (head == NULL || head->data.getID() >= record.getID())

     {      

// if the list is empty or record will be added at beginning of list

          // add and make node the head

          temp->next = head;

          head = temp;

          temp = NULL;

     }

     else

     {

          // Otherwise, find the proper place in the list and

          // adjust pointers to add the new node

          node* prev = search(record.getID());

          temp->next = prev->next;

          prev->next = temp;

     }      

     if (DEBUG_MODE)

     {

          cout << "Inserted record " << record.getID() << endl;

     }

}

       

/*

* Remove an element from the linked list

*/

void LinkedList::remove(int ID)

{

          // find the node in the list

          node* prev = search(ID);

         if (prev==head && prev->data.getID()==ID)

          {

              // if the element is first in the list

              head = head->next;

              if (DEBUG_MODE){

cout << "Deleted record " << ID << "." << endl;        

              }

         }

          else if ( prev->next->data.getID() == ID)

          {

              // otherwise, delete the record by adjusting

              // the next pointers to "skip" over the node

              prev->next = prev->next->next;        

              if (DEBUG_MODE){

              cout << "Deleted record " << ID << "." << endl;         

              }

          }

          else

          {

              // the ID was not found in the list

              if (DEBUG_MODE){

cout << "Record " << ID << " not found. Could not delete." << endl;

              }

         }

}

void LinkedList::printRecords()

{

        // start at the first node (head)

        node *temp=head;

cout<<"-------------------------------------------------- ";

        // loop until end of list (i.e., when node is NULL)

        while(temp!=NULL)

        {

                // print out the record

                cout << temp->data.display() << endl;

               

                // move to next node

                temp=temp->next;

        }

cout<<" -------------------------------------------------- ";

}

File Name: Project5.cpp

#include<iostream>

#include "LinkedList.h"

#include "Record.h"

using namespace std;

int main

{

     string name;

     int ID;

     string genre;

     int age;

     string birthplace;

     LinkedList myLst;

     node *tmp=NULL;

     int usChoice;

     while(1)

     {

          cout<<"MENU"<<endl;

cout<<"1. Insert 2. Delete 3. Search 4. Print 5. Quit"<<endl;

          cout<<"Enter your choice:";

          cin>>usChoice;

          if(usChoice==5)

              break;

          switch(usChoice)

          {

          case 1:

              cout<<"Enter the ID:";

              cin>>ID;

              cout<<"Enter the name:";

              getline(cin,ID);

              cout<<"Enter the genre:";

              getline(cin,genre);

              cout<<"Enter the age:";

              cin>>age;

              cout<<"Enter the birth place:";

              getline(cin,birthplace);

              Record rr(name,ID, genre,age,birthplace);

              myLst.insert(rr);

              break;

          case 2:

              cout<<"Enter the ID:";

              cin>>ID;

              myLst.remove(ID);

              break;

          case 3:

              cout<<"Enter the ID:";

              cin>>ID;

              tmp=NULL;

              tmp=myLst.search(ID);

              if(tmp!=NULL)

                   tmp->data.display();

              else

                   cout<<"Record not found"<<endl;

              break;

          case 4:

              myLst.printRecords();

              break;

          default:

              cout<<"INVALID CHOICE"<<endl;

          }

          cout<<" Do u want to continue(1/2)?";

          cin>>usChoice;

          if(usChoice==2)

              break;

     }

     system("pause");

     return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote