Final Exam for C Programming Language
(CS2402)
This exam is graded according to
- Correctness
- Efficiency
- Clarity
- 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;
- (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.
- Write a function swap() to accomplish the task.
- How do you invoke the function?
- (10%) Write a one-line macro MAX(x, y) that gives the maximum
of two numbers.
- (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
- (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);
- (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.
- (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:
- Since the list represents a set, there is no special
ordering and no duplicated items in the given and
returned lists.
- The function should not change either of the given lists.