Homework 37

Part 1 of 2: Given the following information complete the table below:

1)  If the value of a variable is an address, simply write ADDR for the value (but still give the type)
2)  If the value of a variable is a struct, simply write STRUCT for the value (but still give the type)

Hint: Do this by hand, not by running in Visual Studio. You don't need to run/simulate the whole program above to be able to fill out the matrix, just look at the variable and figure out what its value would be.  Once a variable is initialized its value is not modified.

#include <iostream>

using namespace std;

struct myStruct {

double data;

myStruct *next;

};

int main () {

double y = 3.7;

int a = 5;

int b = 11;

double z;

myStruct *list;

myStruct firstNode;

firstNode.data = 5.3;

firstNode.next = 0;

list = &firstNode;

//complete the table below as if all of the

//instructions occurred at this point

//Assume each line of the table is the

//only line of code (ie assignments made

//based on questions in the table do not

//effect any other row of the table)

return 0;

}

 Expression Type Value &firstNode myStruct* ADDR list->next myStruct* ADDR list->data Double 5.3 z = b / a Double 2 z = b / double (a) Double 2.2 (a < 10 && b < 10) Bool False (a < 10 | | b < 10) Bool True a = 6 Int 6 b == 6 Bool False

Modify the following code so the function printList prints the data fields of the linked list from back to front.

`class Node`
`{`
`public:`
`  int data;`
`  Node* next;`
`};`

void printList(Node* p) {
if (p!=0){
cout << p ->data << endl;
printList(p->next);
}
}

Solution

void printList(Node* p)

{

if (p!=0)

{

printList(p->next); // just swap order

cout << p ->data << endl;

}

} // base case is when p==0, do nothing!