In: Computer Science
//those who say that "whether the student wants to understand or debug the code, IT IS IN THE COMMENTS, it says solve and that is all that is given for the code and no other instructions
it says //SOLVE
// the code already works you just have to finish the void functions that are given and run it and see if it works or not are those in the main are the test cases to test whether they work or not
#include
#include
using namespace std;
class NodeType {
public:
NodeType(int = 0); // constructor with default value for
// info field
int info; // data
NodeType *nextPtr; // pointer to next node in the list
};
// Constructor
NodeType::NodeType(int data) {
info = data;
nextPtr = 0;
}
void printmenu(void) {
cout << "N: new item" << endl;
cout << "P: print list" << endl;
cout << "F: find item" << endl;
cout << "D: delete head item" << endl;
cout << "V: delete value" << endl;
cout << "S: recursively sum values" << endl;
cout << "O: recursively prod values" << endl;
cout << "X: recursively find max" << endl;
cout << "G: recursive negative item on list" << endl;
cout << "Q: quit" << endl;
}
void printList(NodeType *currentPtr) {
//solve
return 0;
int MaxList(NodeType *currentPtr) {
//solve
return 0;
}
int sumList(NodeType *currentPtr) {
return 0;
//solve
}
int prodList(NodeType *currentPtr) {
//solve
return 0;
}
bool negList(NodeType *currentPtr) {
//solve
\ return 0;
}
NodeType *insertHead(NodeType *head, int value) {
//solve
NodeType *newptr;
return (newptr);
}
void findValue(NodeType *head, int value) {
//solve
}
NodeType *deleteFront(NodeType *head) {
// deletes the first item and returns the new head pointer
NodeType *tempPtr;
//solve
return tempPtr;
}
NodeType *deleteValue(NodeType *currentPtr, int value) {
NodeType *tempPtr = NULL;
//solve
return tempPtr;
}
int main(void) {
NodeType *head = NULL;
int value;
char input;
printmenu();
cout << "what is your selection?";
cin >> input;
while (input != 'Q' && input != 'q') {
switch (input) {
case 'n':
case 'N':
cout << "N" << endl;
break;
case 'p':
case 'P':
cout << "P" << endl;
break;
case 'o':
case 'O':
cout << "O" << endl;
break;
case 's':
case 'S':
cout << "S" << endl;
break;
case 'x':
case 'X':
cout << "X" << endl;
break;
case 'g':
case 'G':
cout << "G" << endl;
break;
case 'f':
case 'F':
cout << "What value would you like to find?" << endl;
cin >> value;
cout << endl << "You are finding: " << value << endl;
break;
case 'd':
case 'D':
cout << "Deleting head entry if present" << endl;
break;
case 'v':
case 'V':
cout << "What value would you like to delete?" << endl;
cin >> value;
cout << "You are deleting " << value << endl;
break;
default:
cout << "What happened?" << endl;
}
printmenu();
cout << "what is your selection?";
cin >> input;
}
system("pause");
}
THE CODE IS:
#include<iostream>
#include<stdlib.h>
using namespace std;
class NodeType {
public:
NodeType(int = 0);
int info;
NodeType * nextPtr;
};
NodeType::NodeType(int data) {
info = data;
nextPtr = NULL;
}
void printmenu(void) {
cout << "N: new item" << endl;
cout << "P: print list" << endl;
cout << "F: find item" << endl;
cout << "D: delete head item" << endl;
cout << "V: delete value" << endl;
cout << "S: recursively sum values" << endl;
cout << "O: recursively prod values" << endl;
cout << "X: recursively find max" << endl;
cout << "G: recursive negative item on list" <<
endl;
cout << "Q: quit" << endl;
}
void printList(NodeType * currentPtr) {
// traversing through the list and printing each value
while (currentPtr != NULL) {
cout << currentPtr -> info << " ";
currentPtr = currentPtr -> nextPtr;
}
cout << endl;
}
int MaxList(NodeType * currentPtr) {
// if single element in list return the item
if (currentPtr -> nextPtr == NULL)
return currentPtr -> info;
// else return the max value among the current value and next
value
return max(currentPtr -> info, MaxList(currentPtr ->
nextPtr));
}
int sumList(NodeType * currentPtr) {
// if single element in list return that element
if (currentPtr -> nextPtr == NULL)
return currentPtr -> info;
// else return sum of current value and the next value
return (currentPtr -> info + sumList(currentPtr ->
nextPtr));
}
int prodList(NodeType * currentPtr) {
// if single element in list return that element
if (currentPtr -> nextPtr == NULL)
return currentPtr -> info;
// else return product of current value and the next value
return (currentPtr -> info * prodList(currentPtr ->
nextPtr));
}
bool negList(NodeType * currentPtr) {
// empty list return false
if (currentPtr == NULL)
return false;
// if single element in list
if (currentPtr -> nextPtr == NULL)
return currentPtr -> info < 0;
// else check the rest of the list
if (currentPtr -> info < 0)
return true;
return negList(currentPtr -> nextPtr);
}
NodeType * insertHead(NodeType * head, int value) {
// creating a new node
NodeType * newptr = new NodeType(value);
// assigning the next of new ptr to head node
newptr -> nextPtr = head;
// return the new pointer
return (newptr);
}
void findValue(NodeType * head, int value) {
// flag to store if value is found
bool found = false;
// traversing through all the items in the list
while (head != NULL) {
// value found then set flag to true
if (head -> info == value) {
found = true;
break;
}
head = head -> nextPtr;
}
// printing the appropriate message
if (found)
cout << "Value found!" << endl;
else
cout << "Value not found!" << endl;
}
NodeType * deleteFront(NodeType * head) {
// checking if head is null or not
if(head == NULL)
return NULL;
// else return the new head i.e. the position to which head
pointer points to
NodeType * tempPtr = head -> nextPtr;
delete(head);
// return the new head position
return tempPtr;
}
NodeType * deleteValue(NodeType * currentPtr, int value) {
NodeType * tempPtr = NULL;
// if current pointer is null return null
if (currentPtr == NULL) {
return NULL;
}
// if value found in the current node then delete the current
node
// and return the position of the next location
if (currentPtr -> info == value) {
tempPtr = currentPtr -> nextPtr;
delete(currentPtr);
return tempPtr;
}
// else find for the value in rest of list and delete it
currentPtr -> nextPtr = deleteValue(currentPtr -> nextPtr,
value);
return currentPtr;
}
int main(void) {
NodeType * head = NULL;
int value;
char input;
printmenu();
cout << "what is your selection?";
cin >> input;
while (input != 'Q' && input != 'q') {
switch (input) {
case 'n':
case 'N':
cout << "What value would you like to enter?" <<
endl;
cin >> value;
head = insertHead(head, value);
break;
case 'p':
case 'P':
cout << "The list is:" << endl;
printList(head);
break;
case 'o':
case 'O':
cout << "The product of the list is: " <<
prodList(head) << endl;
break;
case 's':
case 'S':
cout << "The sum of the list is: " << sumList(head)
<< endl;
break;
case 'x':
case 'X':
cout << "The maximum value of the list is: " <<
MaxList(head) << endl;
break;
case 'g':
case 'G':
if (negList(head))
cout << "There is/are negative items on list." <<
endl;
else
cout << "There is no negative items on list." <<
endl;
break;
case 'f':
case 'F':
cout << "What value would you like to find?" <<
endl;
cin >> value;
cout << endl << "You are finding: " << value
<< endl;
findValue(head, value);
break;
case 'd':
case 'D':
cout << "Deleting head entry if present" << endl;
head = deleteFront(head);
break;
case 'v':
case 'V':
cout << "What value would you like to delete?" <<
endl;
cin >> value;
cout << "You are deleting " << value <<
endl;
head = deleteValue(head, value);
break;
default:
cout << "What happened?" << endl;
} // end switch
printmenu();
cout << "what is your selection?";
cin >> input;
} // end while
system("pause");
} // end main
SAMPLE INPUT AND OUTPUT: