Final Exam for C Programming Language (CS2402)

Instructor: Roger Jang


This exam is graded according to
  1. Correctness
  2. Efficiency
  3. Clarity
  4. Brevity

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;
  1. (10%) Suppose there are two integers a and b in a C program. We want to exchange the values of these two integer by a function call.
    1. Write a function swap() to accomplish the task.
    2. How do you invoke the function?
  2. (10%) Write a one-line macro MAX(x, y) that gives the maximum of two numbers.
  3. (10%) Write a one-line macro DISP(x, format) that prints a variable in an appropriate format. For instance, if x is an integer with 24 as its value, DISP(x, %d) should print out
    	x = 24
    	
  4. (20%) Write a recursive function that returns the depth of a given node. Note that a terminal node should have a depth of zero. The prototype of the desired function looks like this:
    	int treeDepth(TREENODE *root);
    	
  5. (25%) Write a recursive function to reverse a given list. The prototype of the function looks like this:
    	LISTNODE *reverseList(LISTNODE *head);
    	
    Note that you are only allowed to do pointer manipulation; no data manipulation is allowed.
  6. (25%) A set can be implemented as a linked list. Write a function that returns a new list that represents the union of two given lists. Note that: