Question

In: Computer Science

//those who say that "whether the student wants to understand or debug the code, IT IS...

//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");

}

Solutions

Expert Solution

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:


Related Solutions

A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in different courses have similar stress levels. The following table shows a random sample of students in different courses and their stress levels (n= 178178). Use the table to answer the questions which follow. Stress Levels by Enrolment Enrolment Low Normal High Arts 33 39 26 Science 23 31 26 (1 mark) What proportion of the students in the study have normal stress level? (3dp)...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in different courses have similar stress levels. The following table shows a random sample of students in different courses and their stress levels (n= 200200). Use the table to answer the questions which follow. Stress Levels by Enrolment Enrolment Low Normal High Social Science 40 42 43 Engineering 22 31 22 (1 mark) What proportion of the students in the study have normal stress level?...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in different courses have similar stress levels. The following table shows a random sample of students in different courses and their stress levels (n= 208208). Use the table to answer the questions which follow. Stress Levels by Enrolment Enrolment Low Normal High Law 24 48 49 Arts 25 34 28 (1 mark) What proportion of the students in the study have normal stress level? (3dp)...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in...
A student counsellor is concerned about students’ stress levels. S/he wants to understand whether students in different courses have similar stress levels. The following table shows a random sample of students in different courses and their stress levels (n= 214214). Use the table to answer the questions which follow. Stress Levels by Enrolment Enrolment Low Normal High Human Sciences 40 43 46 Science 25 38 22 (1 mark) What proportion of the students in the study have normal stress level?...
Into to PythonInstructions: • In this lab you are to debug the code found at...
Into to PythonInstructions: • In this lab you are to debug the code found at the end of these instructions: Find and correct all problems found in the code. Create comments in the code explaining what you found and how you corrected it • The purpose of this program is to calculate car insurance rates using the following rules: The base rate of insurance is $50 a month, Males pay a 25% premium over the base rate, Drivers in Michigan...
this is for those who did Arduino projects this code is supposed to work this way,...
this is for those who did Arduino projects this code is supposed to work this way, there is a temperature sensor in the Arduino Uno along with a motor. the motor should turn on only when the temperature in the sensor exceeds 23 degrees Celcius. Here is the code float AnalogToVolts(int reading); void setup() { Serial.begin(9600); pinMode(13, OUTPUT); } void loop() { int reading; float volts; float temperature_in_celsius; reading = analogRead(A1); volts = AnalogToVolts(reading); //Function call if ( temperature_in_celsius >...
Lets say you are a person who wants to persue a career in project management but...
Lets say you are a person who wants to persue a career in project management but you are not sure if that's really what you want and you are given and chance to meet with a project manager. Please compile 10 questions to use in an interview with a project manager. These questions must give a clear understanding whether you want to pursue this project management career or not
What do you say to those who argue that discriminating against LGBT people (not serving, not...
What do you say to those who argue that discriminating against LGBT people (not serving, not hiring…) is permissible and should be legal if your religious belief tells you to do so?
What would you say to those who argue that establishing attractive career programs for employees will...
What would you say to those who argue that establishing attractive career programs for employees will only enhance their own marketability and enable them to leave the organization faster?
There is error in this java code. Pls debug. // Prompt user for value to start...
There is error in this java code. Pls debug. // Prompt user for value to start // Value must be between 1 and 20 inclusive // At command line, count down to blastoff // With a brief pause between each displayed value import java.util.*; public class DebugSix3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String userNumString; int userNum, val; final int MIN = 1; final int MAX = 20; System.out.println("Enter a number between " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT