In: Computer Science
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.
#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;
}