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

Need to modify the the C++ program to maintains a linked-list based bag structur

ID: 3753110 • Letter: N

Question

Need to modify the the C++ program to maintains a linked-list based bag structure for cars. Each car has price, color, VIN, year, and owner data:

price: positive whole number, between 0-9999. Randomly generated. Color: a string, out of red, blue, white, black, gray, green, yellow. Randomly selected. VIN: positive whole number, 5 chars, should have two A-to-Z chars. Randomly generated. Year: between, 1950 and 2017. Randomly generated. Owner: One word string, having only a-z and A-Z. The first letter is capitalized, the rest are lowercase, randomly generated. program should automatically add 1000 cars to the bag once the program is run.

should write a class to maintain a bag and have the following functions:

Create a bag, Add a new car (all data pieces will be randomly created), Search a car with VIN, List VINs of the cars what are specified by a color. (for instance, VINs of all white cars), List VINs of the cars what are specified by owner name. (search by owner name), Remove a car with VIN, Remove the last car (the one just added), Remove all cars (do not forget to free structs from memory), Get the number of cars in the bag. Below is an example template I have so far to modify from:

#include <stdio.h>

#include <stdlib.h>

#include <iostream>

#include <string>

using namespace std;

class BagDyn

{

public:

BagDyn(); // construction

bool Add(int n); // Add, true if successfully added

bool Remove(int n); // Removes an item from the bag

unsigned int getCapacity(); // return the capacity, the max number of items to be held

unsigned int getSize(); // current number of items in the bag

bool Search(int s); // search for an item

bool isEmtpy(); // Checks if the bag is empty

void ListAll(); // displays all items of the bag

private:

int * data ;

unsigned int size;

unsigned int capacity;

};

BagDyn::BagDyn() // construction

{

data = NULL;

cout << "Enter the capacity of the bag:" ;

cin >> capacity;

size = 0;

data = new int[capacity];

if (data == NULL)

{

cout << "An error occured, bag cannot be allocated" << endl;

}

else

{

cout << data;

cout << "A bag to keep " << capacity << " items is created and ready to use" << endl;

}

}

// returns the capacity, the max number of items to be held

unsigned int BagDyn::getCapacity()

{

return capacity;

}

// number of items in the bag

unsigned int BagDyn::getSize()

{

return size;

}

// search for an item

bool BagDyn::Search(int s)

{

for (int i = 0; i < size; i++) // i < size or i <= (size-1)

{

if (data[i] == s) return true;

}

return false;

}

// Checks if the bag is empty

bool BagDyn::isEmtpy()

{

if (size == 0) return true;

return false;

}

// Adds an items, true if successfully added

bool BagDyn::Add(int n)

{

if (size == capacity) return false;

for (int i = 0; i < size; i++) // i < size or i <= (size-1)

{

if (data[i] == n) return false;

}

// data[size] = n; size++;

data[size++] = n;

return true;

}

// displays all items of the bag

void BagDyn::ListAll()

{

if (size == 0) { cout << "Bag is empty" << endl; return; }

for (int i = 0; i < size; i++) // i < size or i <= (size-1)

{

cout << data[i] << ", ";

}

cout << endl;

}

// Removes an item from the bag

bool BagDyn::Remove(int n)

{

if (isEmtpy() == true) return false;

for (int i = 0; i < size; i++) // search for the item

{

if (data[i] == n)

{

data[i] = data[--size];

return true;

}

}

return false;

}

// Demonstrate the usage of the bag

int main()

{

BagDyn aBag;

aBag.ListAll(); aBag.Add(5); aBag.Add(7); aBag.Add(1);

aBag.ListAll(); aBag.Add(8); aBag.Add(17); aBag.Add(5);

aBag.ListAll();

if (aBag.Search(8) == true) cout << "8 is in the bag" << endl;

if (aBag.Search(17) == true) cout << "17 is in the bag" << endl;

if (aBag.Search(3) == true) cout << "3 is in the bag" << endl;

else cout << " 3 is not in the bag" << endl;

aBag.Remove(17); aBag.ListAll();

aBag.Remove(5); aBag.ListAll();

cout << "Size of the bag is " << aBag.getSize() << endl;

system("pause");

}

Explanation / Answer

The template provided has been modified to implement a linked list version with all the features and functions required. The code for that is :

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

struct CarNode {
Car carObject;
struct CarNode * nextCar;
};

class Car {
private:
string colorOptions[] = {
"red",
"blue",
"white",
"black",
"gray",
"green",
"yellow"
};
string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lowerCase = "abcdefghijklmnopqrstuvwxyz";
string numbers = "0123456789";
int price = 0;
string color;
string vin;
string owner;
int year;

void setColor() {
int option = rand() * 7;
this.color = colorOptions[option];
}
void setPrice() {
this.price = rand() * 10000;
}
void setOwner() {
string nameVal = upperCase[(rand() * 26)];
int nameSize = (rand() * 50) + 10;
for (int i = 0; i < nameSize; i++) {
int index = rand() * 26;
nameVal = nameVal + lowerCase[index];
}
this.name = nameVal;
}
void setVin() {
string vinVal = number[(rand() * 10)];
for (int i = 0; i < 5; i++) {
int index = rand() * 26;
vinVal = vinVal + upperCase[index]
}
}
void setYear() {
this.year = (rand() * 68) + 1950;
}

public:
Car() {
this.setYear();
this.setPrice();
this.setVin();
this.setOwner();
this.setColor();
}
string getOwner() {
return this.owner;
}
string getColor() {
return this.color;
}
string getVin() {
return this.vin;
}
int getYear() {
return this.year;
}
int getPrice() {
return this.price;
}

};

class BagDyn

{

public:

BagDyn(); // construction

bool Add(int n); // Add, true if successfully added

bool Remove(int n); // Removes an item from the bag

unsigned int getCapacity(); // return the capacity, the max number of items to be held

unsigned int getSize(); // current number of items in the bag

bool Search(int s); // search for an item

bool isEmtpy(); // Checks if the bag is empty

void ListAll(); // displays all items of the bag

private:

CarNode * head = NULL;

unsigned int size;

unsigned int capacity;
};

BagDyn::BagDyn() // construction

{

cout << "Enter the capacity of the bag:";

cin >> capacity;

size = 0;

// data = new Car[capacity];

// if (data == NULL)

// {

// cout << "An error occured, bag cannot be allocated" << endl;

// }

// else

// {

// cout << data;

// cout << "A bag to keep " << capacity << " items is created and ready to use" << endl;

}

}

// returns the capacity, the max number of items to be held

unsigned int BagDyn::getCapacity()

{

return capacity;

}

// number of items in the bag

unsigned int BagDyn::getSize()

{

return size;

}

// search for an item

bool BagDyn::searchByVin(string vin) {
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getVin() == vin) {
return true;
}
}
return false
}
void BagDyn::listVinByColor(string color) {
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getColor() == color) {
cout << temp.carObject.getVin() << endl;
}
}

}

void BagDyn::listVinByOwner(string owner) {
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getOwner() == owner) {
cout << temp.carObject.getVin() << endl;
}
}
}

// Checks if the bag is empty

bool BagDyn::isEmtpy()

{

if (size == 0) return true;

return false;

}

// Adds an items, true if successfully added

bool BagDyn::Add(CarNode node)

{

if (size == capacity) return false;

CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getVin() == node.carObject.getVin()) {
return false;
}
temp = temp - > nextCar;
}

CarNode * newNode = new CarNode();
newNode.carObject = new Car();
newNode.nextCar = NULL;
size++;

}

// data[size] = n; size++;

// displays all items of the bag

void BagDyn::ListAll()

{
CarNode * temp = this.head;
if (size == 0) {
cout << "Bag is empty" << endl;
return;
}

while (temp != NULL) // i < size or i <= (size-1)

{

cout << "Owner Name: " << temp.carObject.getOwner() << endl;
cout << "Car Color: " << temp.carObject.getColor() << endl;
cout << "Vin: " << temp.carObject.getVin() << endl;
cout << "Price: " << temp.carObject.getPrice() << endl;
cout << "Year: " << temp.carObject.getYear() << endl;

cout << endl << endl;
temp = temp - > nextCar;

}

cout << endl;

}

// Remove car by by Vin

bool BagDyn::removeCarByVin(string vinVal) {
CarNode * temp = this.head;
while (temp != NULL) {
CarnNode * prev = temp;
if (temp.carObject.getVin() == vinVal) {
prev - > nextCar = temp - > nextCar;
temp.nextCar = NULL;
CarNode * junkNode = temp;
free(junkNode);
size--;
return true;
}
}
return false;
}

bool BagDyn::removeLastCar(string vinVal) {
CarNode * temp = this.head;
CarNode * prev = NULL;
while (temp) {
prev = temp;
temp = temp - > nextCar;
}
if (prev != NULL) {
prev - > nextCar = NULL;
free(temp);
size--;
return true;
}
return false;
}

void BagDyn::removeAllCars() {
CarNode * temp = this.head;
CarNode * next;
while (temp != NULL) {
next = current - > next;
current - > next = NULL;
free(current);
current = next;
}
head = null;

}

// Demonstrate the usage of the bag

int main()

{

BagDyn aBag = new BagDyn();
// Create your bag here.

system("pause");

}

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