In: Computer Science
Understand the code and explain the code and answer the questions. Type your answers as comments.
#include #include using namespace std; // what is Color_Size and why it is at the end? enum Color { Red, Yellow, Green, Color_Size }; // what is Node *next and why it is there? struct Node { Color color; Node *next; }; // explain the code below void addNode(Node* &first, Node* &last, const Color &c) { if (first == NULL) { first = new Node; first->color = c; first->next = NULL; last = first; } else { last->next = new Node; last->next->color = c; last->next->next = NULL; last = last->next; } } // explain the code below string colorToString(const Color &c) { if (c == Red) return "Red"; else if (c == Yellow) return "Yellow"; else if (c == Green) return "Green"; } // explain the code below Color intToColor(const int &i) { if (i == 0) return Red; else if (i == 1) return Yellow; else if (i == 2) return Green; else return Color_Size; } // explain the code below and why the number 48 is there? Color charToColor(const char &c) { if (c >= 48 && c <= (48 + Color_Size - 1)) { return intToColor(c - 48); } else { return Color_Size; } } // explain the code below string printNodes(Node* &first) { if (first == NULL) return ""; Node *n = first; string text = "List: "; while (n != NULL) { if (n->color == Red) text += colorToString(Red); else if (n->color == Yellow) text += colorToString(Yellow); else if (n->color == Green) text += colorToString(Green); if (n->next != NULL) text += ", "; n = n->next; } return text; } // explain the code below string printMenu() { string text = "Choose Color to Add to the List:\n"; for (int i = 0; i < Color_Size; i++) { text += to_string(i) + "> "; text += colorToString(intToColor(i)); text += "\n"; } text += to_string(Color_Size) + "> " + "Print List"; return text; } // explain the code below bool invalidInput(const char &input) { return (input < 48) || (input > (48 + Color_Size)); } // explain the code below void freeMemory(Node* &first) { if (first == NULL) return; Node *n = first; while (n != NULL) { first = first->next; delete n; n = first; } } int main() { bool loopIt = true; char input; Node *first, *last; first = NULL; last = NULL; while (loopIt) { cout << printMenu() << endl << endl; cout << "Input: "; cin >> input; cout << endl; if (invalidInput(input)) { cout << "invalid input! the app will close . . ." << endl; loopIt = false; } else { if (charToColor(input) == Color_Size) { cout << printNodes(first) << endl << endl; } else { addNode(first, last, charToColor(input)); } } } freeMemory(first); system("pause"); return 0; } |
#include
#include
using namespace std;
// what is Color_Size and why it is at the end?
/* Color_Size is part of the enum, enum is used to assign integer values to strings, i.e in our case Red will have value 0, Yellow will have value 1, Green will have value 2 and Color_Size will have value 3. Color_Size is at the end so that it corresponds to the number of colors the enum have. */
enum Color
{
Red, Yellow, Green, Color_Size
};
// what is Node *next and why it is there?
/* Node* next is a pointer pointing to the next node in the list */
struct Node
{
Color color;
Node *next;
};
// explain the code below
/* This code is adding a node at the end of the linked list having colors. If the list is empty initially it will add the first node, otherwise it will add the node to end of the linked list. */
void addNode(Node* &first, Node* &last, const Color &c)
{
if (first == NULL)
{
first = new Node;
first->color = c;
first->next = NULL;
last = first;
}
else
{
last->next = new Node;
last->next->color = c;
last->next->next = NULL;
last = last->next;
}
}
// explain the code below
/* This code is converting the enum color to string value and returning it. The enum is actually integer values as stated above. So it is converting those integer values to strings. */
string colorToString(const Color &c)
{
if (c == Red) return "Red";
else if (c == Yellow) return "Yellow";
else if (c == Green) return "Green";
}
// explain the code below
/* This code is converting the integer values back to the enum values and returning it. */
Color intToColor(const int &i)
{
if (i == 0) return Red;
else if (i == 1) return Yellow;
else if (i == 2) return Green;
else return Color_Size;
}
// explain the code below and why the number 48 is there?
/* This code is converting a character variable to it's corresponding enum value and returning it. The number 48 is here because 48 is the ASCII value of the number '0' so its using 48 to convert the character to the integer */
Color charToColor(const char &c)
{
if (c >= 48 && c <= (48 + Color_Size - 1))
{
return intToColor(c - 48);
}
else
{
return Color_Size;
}
}
// explain the code below
/* This code is returning a string which contains the colors of all the nodes one by one. It traverses all the nodes of the linked list and add the color of the node to the string and returns that string. */
string printNodes(Node* &first)
{
if (first == NULL) return "";
Node *n = first;
string text = "List: ";
while (n != NULL)
{
if (n->color == Red) text += colorToString(Red);
else if (n->color == Yellow) text += colorToString(Yellow);
else if (n->color == Green) text += colorToString(Green);
if (n->next != NULL) text += ", ";
n = n->next;
}
return text;
}
// explain the code below
/* It will print a menu which will give options to add any color to the list or print the list.*/
string printMenu()
{
string text = "Choose Color to Add to the List:\n";
for (int i = 0; i < Color_Size; i++)
{
text += to_string(i) + "> ";
text += colorToString(intToColor(i));
text += "\n";
}
text += to_string(Color_Size) + "> " + "Print List";
return text;
}
// explain the code below
/* This will check if the input by the user greater than the number of colors present in the enum. It will return true in that case.*/
bool invalidInput(const char &input)
{
return (input < 48) || (input > (48 + Color_Size));
}
// explain the code below
/* This will delete our linked list of nodes and free the memory taken up by the nodes.*/
void freeMemory(Node* &first)
{
if (first == NULL) return;
Node *n = first;
while (n != NULL)
{
first = first->next;
delete n;
n = first;
}
}
int main()
{
bool loopIt = true;
char input;
Node *first, *last;
first = NULL;
last = NULL;
while (loopIt)
{
cout << printMenu() << endl << endl;
cout << "Input: ";
cin >> input;
cout << endl;
if (invalidInput(input))
{
cout << "invalid input! the app will close . . ." << endl;
loopIt = false;
}
else
{
if (charToColor(input) == Color_Size)
{
cout << printNodes(first) << endl << endl;
}
else
{
addNode(first, last, charToColor(input));
}
}
}
freeMemory(first);
system("pause");
return 0;
}