You may add comments freely if they help make your code more easily understood.
LISTNODE and TREENODE are two data types that you are going to used in answering some of the questions in this exam. These two data types are defined as follows.
struct listNode { char data; struct listNode *next; }; typedef LISTNODE struct listNode;
struct treeNode{ char data; struct treeNode *left; struct treeNode *right; }; typedef TREENODE struct treeNode;
void swap (int *a, *b) { int temp; temp = *a; *a = *b; *b = temp; }
swap(&a, &b);
#define MAX(x,y) ((x)>(y)?(x):(y))
x = 24Answer:
#define DISP(x, format) printf(#x " = " #format "\n", x);
int treeDepth(TREENODE *root);Answer:
int treeDepth(TREENODE *root) { if (root == NULL) return(-1); return(1+MAX(treeDepth(root->left), treeDepth(root->right))); }
LISTNODE *reverseList(LISTNODE *head);Note that you are only allowed to do pointer manipulation; no data manipulation is allowed.
LISTNODE *reverse(LISTNODE *head) { LISTNODE *p, *q; if (head == NULL || head->next == NULL) return(head); p = head; q = p->next; while (q->next != NULL) { p = q; q = q->next; } /* Now q is pointing to the last element of the list. */ p->next = NULL; q->next = reverse(head); return(q); }
LISTNODE *newlist (LISTNODE *head1, LISTNODE *head2) { LISTNODE *temp1, *new, *new_head, *temp2; int test = 0; /* Duplicate the first list */ new = (LISTNODE *)malloc(sizeof(LISTNODE)); new_head = new; new->data = head1->data; temp1 = head1; while (temp1->next != NULL) { new->next = (LISTNODE *)malloc(sizeof(LISTNODE)); new->next->data = temp1->->next->data; new = new->next; temp1 = temp1->next; } new->next = NULL; /* Insert the second list */ temp2 = head2; while (temp2 != NULL) { temp1 = new_head; test = 0; while (temp1 != NULL) { if ( temp2->data == temp1->data) test = 1; temp1 = temp1->next; } if (test == 0) { new->next = (LISTNODE *)malloc(sizeof(LISTNODE)); new->next->data = temp2->data; new = new->next; } temp2 = temp2->next; } new->next = NULL; return new_head; }