Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Assignment Description: Write a C++ program that creates a linked list of Project data. Each entry in the list is to have a project title,

Assignment Description:

Write a C++ program that creates a linked list of Project data. Each entry in the list is to have a project title, a project number, and a project location, and a pointer to the next project data. We will need a pointer (head) that points to the first element in the linked list (initially NULL). Also, you need to create the following methods in the LinkedList class (within LinkedList.h file):

bool LinkedList::addProject(string title, int number, string location, int index)

This method attempts to add a new project data into the linked list using the parameter values, a project title, a project number, a project location, and an index. If the index is 0, then the new element should be added as the first element of the linked list. It needs to create an object/data of the struct Project and add it to the linked in a correct location. If the index is less than 0 or beyond the number of elements in the linked list, or if there is no memory left to create a new object, the method should return false. The method should return true otherwise (the new project information was successfully added).

bool LinkedList::removeProject(string title, int number, string location)

This method attempts to remove the project with the parameter project title, project number value, and project location and should return true if it can find and remove the project entry in the linked list. It should return false otherwise (the project with the title, the number and the title does not exists in the linked list).

void LinkedList:printProjects( )

It prints all entries in the linked list in the following format (print "The list is empty " if the linked list is empty):

Project Title: Commercial Construction Project Project Number: 15 Project Location: Flagstaff Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson

LinkedList::~LinkedList( )

- This is a destructor of the LinkedList class. It should remove all elements in the linked list and perform garbage collection (free their memory). At the end, it should print: "The number of deleted projects is: XXX " where XXX is the number of deleted projects in the linked list.

int main()

- The main function should start by displaying this menu in this format:

Choice\t\t Action

------\t\t ------

A\t\t Add Project

D\t\t Display Projects

Q\t\t Quit

R\t\t Remove Project

? \t\t Display Help

Next, the following prompt should be displayed:

What action would you like to perform?

Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase.

The following is an example run (user input is in bold letter): Choice Action ------ ------ A Add Project D Display Projects Q Quit R Remove Project ? Display Help What action would you like to perform? D The list is empty What action would you like to perform? A Please enter some project information: Enter a project title: Grand Road Improvement Project Enter a project number: 3 Enter a project location: Tucson Enter an index to add: 0 The project 3 with title Grand Road Improvement Project added What action would you like to perform? D Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson What action would you like to perform? A Please enter some project information: Enter a project title: Helping Children Project Enter a project number: 11 Enter a project location: Phoenix Enter an index to add: 0 The project 11 with title Helping Children Project added What action would you like to perform? D Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson What action would you like to perform? A Please enter some project information: Enter a project title: Farming Project Enter a project number: 7 Enter a project location: Phoenix Enter an index to add: 2 The project 7 with title Farming Project added What action would you like to perform? D Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Title: Farming Project Project Number: 7 Project Location: Phoenix What action would you like to perform? A Please enter some project information: Enter a project title: Commercial Construction Project Enter a project number: 15 Enter a project location: Flagstaff Enter an index to add: 1 The project 15 with title Commercial Construction Project added What action would you like to perform? D Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Title: Commercial Construction Project Project Number: 15 Project Location: Flagstaff Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Title: Farming Project Project Number: 7 Project Location: Phoenix What action would you like to perform? A Please enter some project information: Enter a project title: Rebuilding Project Enter a project number: 21 Enter a project location: Scottsdale Enter an index to add: 5 The index is out of bounds What action would you like to perform? D Project Title: Helping Children Project Project Number: 11 Project Location: Phoenix Project Title: Commercial Construction Project Project Number: 15 Project Location: Flagstaff Project Title: Grand Road Improvement Project Project Number: 3 Project Location: Tucson Project Title: Farming Project Project Number: 7 Project Location: Phoenix What action would you like to perform? Q The number of deleted Projects is: 4

Assignment1.cpp:

#include #include #include "LinkedList.h" using namespace std; void printMenu(); int main() { // local variables, can be accessed anywhere from the main method char input1 = 'Z'; string inputInfo; string projTitle, projLocation; int projNumber; int index; string line; bool success = false; // instantiate a Linked List object LinkedList * list1 = new LinkedList(); printMenu(); do // will ask for user input { cout << "What action would you like to perform? "; cin.get(input1); input1 = toupper(input1); cin.ignore(20, ' '); //flush the buffer // matches one of the case statement switch (input1) { case 'A': //Add Project cout << "Please enter some project information: "; cout << "Enter a project title: "; getline(cin, projTitle); cout << "Enter a project number: "; cin >> projNumber; cin.ignore(20, ' '); //flush the buffer cout << "Enter a project location: "; getline(cin, projLocation); cout << "Enter an index to add: "; cin >> index; cin.ignore(20, ' '); //flush the buffer success = list1->addProject(projTitle, projNumber, projLocation, index); if (success == true) cout << "The project " << projNumber << " with title " << projTitle << " added "; else cout << "The index is out of bounds "; break; case 'D': //Display Projects list1->printProjects(); break; case 'Q': //Quit delete list1; break; case 'R': //Remove Project cout << "Please enter a project to remove: "; cout << "Enter a project title: "; getline(cin, projTitle); cout << "Enter a project number: "; cin >> projNumber; cin.ignore(20, ' '); //flush the buffer cout << "Enter a project location: "; getline(cin, projLocation); success = list1->removeProject(projTitle, projNumber, projLocation); if (success == true) cout << "The project " << projNumber << " with title " << projTitle << " removed "; else cout << "The project " << projNumber << " with title " << projTitle << " does not exist "; break; case '?': //Display Menu printMenu(); break; default: cout << "Unknown action "; break; } } while (input1 != 'Q'); return 0; } /** The method printMenu displays the menu to a user**/ void printMenu() { cout << "Choice\t\tAction "; cout << "------\t\t------ "; cout << "A\t\tAdd Project "; cout << "D\t\tDisplay Projects "; cout << "Q\t\tQuit "; cout << "R\t\tRemove Project "; cout << "?\t\tDisplay Help "; } 

LinkedList.h:

#include //to use cout #include //to format output #include //to use strings using namespace std; struct Project { string projTitle; int projNumber; string projLocation; struct Project * next; }; //class LinkedList will contains a linked list of Projects class LinkedList { private: struct Project * head; public: LinkedList(); ~LinkedList(); bool addProject(string title, int number, string location, int index); bool removeProject(string title, int number, string location); void printProjects(); }; //Constructor to initialize the linked list LinkedList::LinkedList() { head = NULL; } //Destructor //Description: ...... to be completed LinkedList::~LinkedList() { //To be completed } //Description: .... to be completed bool LinkedList::addProject(string title, int number, string location, int index) { //To be completed } //Description: .... to be completed bool LinkedList::removeProject(string title, int number, string location) { //To be completed } //Description: .... to be completed void LinkedList::printProjects() { //To be complete } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions