Question

In: Computer Science

Can't figure out how to do this code with seperate main.cpp, inventory.cpp, inventory.h files, if you...

Can't figure out how to do this code with seperate main.cpp, inventory.cpp, inventory.h files, if you know how please help!

in C++

Functional requirements: Create a linked list that holds a series of primary colors. The user inputs which colors are used and in which order.  

Programming Requirements:

Design your own dynamic linked list class (using pointers) to hold a series of primary colors The class should have the following member functions:

append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges the nodes in the list so that their order is reversed), and search (which returns the position of a specific value in the list. Assume the first node is position 0. If the items is not found, then return a -1 which will tell the calling function that that value does not exist)

Use the NumberList program from Chapter 18 (edition 9 textbook) as an example. You have to create your own linked list. You may not use any C++ STD list functions.

Create a menu-driven program to demonstrate your linked list capabilities.

High level validation

Input from the user should be validated for being a primary color (red, yellow or blue).

High level validation is not required for the existence of a node. But you must handle the situation. See example below.

Low level validation

1. Mutator functions which are given a node value that is not a primary color should have an exit_failure.

2. Mutator functions which are told to access a node that doesn't exist should return a -1 so that the calling function can handle the problem. (No exit_failure used in this case b/c the calling function can't know if the node is there or not.) See example below.

Example of missing node:

If a call to a mutator returns a -1 it will indicate that the node is not found. Display an appropriate error message to the user and then cycle back to the menu again. For example:

1. Append

2. Insert

3. Delete

4. Print

5. Reverse

6. Search

Please choose a menu option:

2

What value do you wish to insert?

red

What position to do wish to insert?

3

**I'm sorry but there is no position 3 in the linked list.

1. Append

2. Insert

3. Delete

4. Print

5. Reverse

6. Search

Please choose a menu option:

***********

Make sure that you have proper constructors and destructors (a destructor must delete every node). Make your input and output professional. Break your code down into functions to maintain modular design. No global variables.

Extra credit: offer an additional menu item that will display the list with secondary colors indicated where possible. For example: red red PURPLE blue GREEN yellow yellow yellow. 5 points EC.  

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
class Node
{
   public:
   string data;
   Node *next;
};

Node* getNode(string data)
{
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void append(Node** head_ref, string new_data)
{
   Node* new_node = new Node();

   Node *last = *head_ref;

   new_node->data = new_data;

   new_node->next = NULL;


   if (*head_ref == NULL)
   {
       *head_ref = new_node;
       return;
   }
   while (last->next != NULL)
       last = last->next;


   last->next = new_node;
   return;
}

void printList(Node *node)
{
   while (node != NULL)
   {
       cout<<node->data<<" ";
       node = node->next;
   }
}
int countList(Node *node)
{ int c=0;
   while (node != NULL)
   {c++;
   node = node->next;
   }
   return c;
}
void deleteNode(struct Node **head_ref, int position)
{
if (*head_ref == NULL)
return;
struct Node* temp = *head_ref;
if (position == 0)
{
*head_ref = temp->next;
free(temp);
return;
}

for (int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;

if (temp == NULL || temp->next == NULL)
return;

struct Node *next = temp->next->next;

free(temp->next);

temp->next = next;
}
bool sear(Node* head, string x)
{
Node* current = head;
while (current != NULL)
{
if (current->data == x)
return true;
current = current->next;
}
return false;
}
void insertPos(Node** current, int pos, string data)
{
int size=countList(*current);
if (pos < 1 || pos > size + 1)
cout << "Invalid position!" << endl;
else {
while (pos--) {

if (pos == 0) {
Node* temp = getNode(data);
temp->next = *current;
*current = temp;
}
else
current = &(*current)->next;
}

}
}

void rev(Node **head_ref) {
Node *temp = NULL;
Node *prev = NULL;
Node *current = (*head_ref);
while(current != NULL) {
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
(*head_ref) = prev;
}

int main()
{
int n,a;
Node* head = NULL;
int z=1;
while(z==1){
cout<<"1. Append\n2. Insert\n3. Delete\n4. Print\n5. Reverse\n6. Search\n";

string s;
cin>>n;
switch(n)
{

case 1:cout<<"Enter the data to append into the list"<<endl;
cin>>s;
append(&head,s);
break;
case 2:cout<<"Enter the value to insert"<<endl;
cin>>s;
cout<<"Insert the position where to append"<<endl;
cin>>a;
if(countList(head)>=a )
{insertPos(&head,a,s);}
else
cout<<"I'm sorry but there is no position "<<a<<" in the linked list."<<endl;
break;
case 3:cout<<"enter the position you want to delete"<<endl;
int b;
cin>>b;
b=b-1;
if(countList(head)>=b && b>=0)
{
deleteNode(&head,b);
}
else
cout<<"I'm sorry but there is no position "<<b+1<<" in the linked list."<<endl;
break;
case 4:cout<<"Created Linked list is: "<<endl;
printList(head);
cout<<endl;

break;
case 5:rev(&head);
printList(head);
cout<<endl;
rev(&head);
break;

case 6:
cout<<"Enter the data you want to search"<<endl;
cin>>s;
if(sear(head,s))
{
cout<<s<<" is present in the linked list.\n"<<endl;
}
else{
cout<<s<<" is not present in the linked list.\n"<<endl;
}
break;


default:cout<<"You are moving out of the menu"<<endl;
z=0;
break;

}

}
   cout<<"Created Linked list is: ";
   printList(head);

   return 0;
}


Related Solutions

I am supposed to map out the following and can't figure out how to do it!...
I am supposed to map out the following and can't figure out how to do it! Can somebody help? The experiment has to do with determining the simplest formula of potassium chlorate and to determine the original amount of potassium chlorate in a potassium chlorate-potassium chloride mixture by measuring the oxygen lost from decomposition. The chemical reaction is 2KClO3(s) ------> 2KCL(s) + 3 O2(g) I am supposed to map out 1. Mass of oxygen lost in the first part 2....
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the main module that creates and manipulates CVehicle objects cars.dat -- a text file that contains name data for the main module 4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in This was the test drive: carOne = Hyundai Sonata carTwo = Hyundai Sonata carThree = Enter the make and model of a vehicle: toyota...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
I can't figure out how to create the decision tree with the sending a messaged situation!...
I can't figure out how to create the decision tree with the sending a messaged situation! The crew of Endurance can visit two planets (Mann’s and Edmunds’). They can choose to visit neither planets, one of the two planets, or both planets. The characteristics of Mann’s planet are below: • 30% chance of finding a perfectly habitable planet • can support all of Earth’s current population if it is • can support none of Earth’s population if it is not...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how to find it correctly for me?
How do you figure out the ideal gas equation in a lab experiment?
How do you figure out the ideal gas equation in a lab experiment?
This is a question from Intro to Bayesian Statistics but I can't figure out what is...
This is a question from Intro to Bayesian Statistics but I can't figure out what is expected here. Derive the posterior distribution of obtaining heads in coin flips. The prior has p(θ = .25) = .25, p(θ = .50) = .50, and p(θ = .75) = .25. The data consist of a specific sequence of flips with 3 heads and 9 tails, so p(Data|θ) = θ^3 (1 − θ) ^9.
I can't figure out this math equation for "The Importance of the Price Elasticity of Demand:...
I can't figure out this math equation for "The Importance of the Price Elasticity of Demand: MC=P(1+(1/Ed)) My marginal cost(MC) is $50/month and that the price elasticity of demand (Ed) is -2.5, what price will maximize my firm's profits? $50=P(1+(1/-2.5))
What is the computations for the following answers? I can't figure this one out. The following...
What is the computations for the following answers? I can't figure this one out. The following information relates to Hudson City for its fiscal year ended December 31, 2017. • During the year, retailers in the city collected $1,700,000 in sales taxes owed to the city. As of December 31, retailers have remitted $1,100,000. $200,000 is expected in January 2018, and the remaining $400,000 is expected in April 2018. • On December 31, 2016, the Foundation for the Arts pledged...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT