Homework 5 Solutions

1. Suppose you are given the following declarations:
```      Jval val;
int age;
double salary;
char *name;
struct node *ptr;
```
1. Write a statement that assigns the variable age to val
```        val.i = age;
```
2. Write a statement that assigns the variable salary to val
```        val.d = salary;
```
3. Write a statement that assigns the variable name to val
```        val.s = name; // use the char * field that Jval makes available
```
4. Write a statement that assigns the variable ptr to val
```        val.v = ptr;  // now we must use the generic pointer field in a Jval
```
5. Write a statement that assigns the value in val to ptr
```        ptr = (struct node *)val.v;  // remember to do the cast
```

2. Write a union that contains an integer field named age, a pointer to a character string named address, and a three element integer array named test_scores.
```	union Item {
int age;
int test_scores[3];
};
```

3. The personnel department at State University has to keep track of three types of people: students, professors, and staff. The information it must keep about these groups is as follows:

1. All three groups of people have an integer social security number, a name of at most 20 characters, and a single character code that denotes whether they are male or female.

2. Students have a floating point gpa, a floating point fee balance, and a pointer to a list of courses they are taking. The pointer points to a struct course node.

3. Professors have a floating point salary, a single character code that denotes their rank, a 10 character string that denotes their office, and a pointer to a list of courses they are teaching. The pointer points to a struct course node.

4. Staff have a floating point hourly rate, an integer that denotes their job classification, and a 10 character string that denotes their office.

• Write a union declaration that allows a student, professor, and staff member to share the same memory space. The union declaration should only contain the information unique to each group. You will have to use structs to represent the unique information for each of the three groups.
```	struct student {
float gpa;
float balance;
struct course *course_list;
};
struct professor {
float salary;
char rank;
char office[11];  // need 11 characters to hold a 10 char string
struct course course_list;
};
struct staff {
float hr_rate;
int job_type;
char office[11];
};
typedef union {
struct student stud;
struct professor prof;
struct staff stf;
} employee_info;
```

• Write a struct named employee that includes the union, the additional shared fields, and a type indicator for the type of person being stored.
```	struct employee {
int record_type;  // indicates whether prof, student, or staff
int ssn;
char name[21];    // need 21 chars to represent a 20 char string
char sex;
employee_info extra_info;
};
```

```Sllist extract_list(Sllist L, Sllist P) {
Sllist new_list;  // new list to be created
Sllist P_ptr, L_ptr; // temp ptrs for traversing P and L lists
int index;   // index of next node to be extracted from L
int L_count = 0; // position of current node in L
Sllist last_node; // pointer to last node in new_list

new_list = new_sllist();
last_node = new_list;
L_ptr = L;
for (P_ptr = sll_first(P); P_ptr != sll_nil(P); P_ptr = sll_next(P_ptr)) {
index = P_ptr->val.i;
// traverse thru L until we reach the index position
while (L_count < index) {
L_ptr = sll_next(L_ptr);
L_count++;
}
// append the value in L to the end of the new list. sll_insert_after
// returns a pointer to the created node, which is the new last
// node in the list.
last_node = sll_insert_after(last_node, new_jval_i(L_ptr->val.i));
}
return new_list;
}
```

5. Write a function called sll_delete_node that deletes a node from an Sllist. You should use the book's solution of keeping a pointer to the previous node as you search the list for the node to delete.
```void sll_delete_node(Sllist list, Sllist node_to_delete) {
Sllist prev_node;
Sllist tmp_ptr;

// find the node that precedes the node to be deleted
sll_traverse(tmp_ptr, node_to_delete) {
if (tmp_ptr == node_to_delete)
break;
else
prev_node = tmp_ptr;
}
// delete the node only if we found it
if (tmp_ptr != sll_nil(list)) {