Assignment Description: Write a C++ program that creates a linked list of Projec
ID: 3875099 • Letter: A
Question
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 Action
------ ------
A Add Project
D Display Projects
Q Quit
R Remove Project
? 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:
LinkedList.h:
Explanation / Answer
LinkedList.cpp
#include <bits/stdc++.h>
#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 Action ";
cout << "------ ------ ";
cout << "A Add Project ";
cout << "D Display Projects ";
cout << "Q Quit ";
cout << "R Remove Project ";
cout << "? Display Help ";
}
LinkedList.h
#include <bits/stdc++.h>
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
Project *current = head; //pointer to head
Project *next ; //pointer to store next node
int count=0; //stores number of elements
while (current != NULL)
{
count++; //increment number of elements
next = current->next;
free(current); //free up the memory
current = next;
}
/* deref head_ref */
head = NULL;
cout<<"The number of deleted projects is: "<<count<<endl;
}
//Description: .... to be completed
bool LinkedList::addProject(string title, int number, string location, int index)
{
//To be completed
int count=0; //stores numner of elements in the LinkedList
Project *iter = head; // iterator
while (iter!=NULL){ //count the number of elemenets int he LinkedList
count++; //increment the count
iter=iter->next;
}
if (count<index || index<0) //if index is out of bound
return false;
Project *newObject ; //creating new node
newObject = new Project;
newObject->projTitle = title;
newObject->projNumber = number;
newObject->projLocation = location;
newObject->next = NULL;
if (index==0){ //adding to the first index
newObject->next = head;
head = newObject;
return true;
}
else if (index==count){ //adding to the last index
iter = head;
while (iter->next!=NULL){
iter = iter->next;
}
iter->next = newObject;
return true;
}
iter = head;
for (int i=0;i<index-1;i++){ //gives the node just previous to the index where new node has to be inserted
iter = iter->next;
}
Project *nextElement = iter->next;
iter->next = newObject;
newObject->next = nextElement;
return true;
}
//Description: .... to be completed
bool LinkedList::removeProject(string title, int number, string location)
{
//To be completed
Project *iter = head; //iterator
Project *temp = NULL; //stores the element previous to iter
while (iter!=NULL){
if (((iter->projTitle).compare(title)==0) && ((iter->projLocation).compare(location)==0) && (iter->projNumber==number)){
if (temp == NULL){ //it means the element to be removed is at the first index
head = head->next;
delete iter;
return true;
}
else{ // it means the element to be removed is inbetween the list
temp->next = iter->next;
delete iter;
return true;
}
}
temp = iter;
iter=iter->next;
}
return false; //means not found
}
//Description: .... to be completed
void LinkedList::printProjects()
{
//To be complete
Project *iter = head;
while (iter!=NULL){ //loop through all the nodes
cout<<" "<<"Project Title: "<<iter->projTitle<<" "<<"Project Number: "<<iter->projNumber<<" "<<
"Project Location: "<<iter->projLocation<<" "<<endl;
iter=iter->next;
}
}
Modified code: Only four functions in .h file (removeProject,addProject,printProjects and destructor) and two header lines include <bits/stdc++.h> have been added in both files.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.