CS140 Midterm 2 Makeup Coding Question


  1. You can get up to 15 points back for midterm 2 by writing a program for the following problem.
  2. To get points back, you must receive a grade of 5/15 or better.
  3. You have 1 hour and 15 minutes (1:15) to complete this problem.
  4. The problem is closed-note, closed-book, and no electronic devices are permitted.
  5. I have included a copy of the Dllist API.

Problem Description

You are given two doubly linked lists, L1 and L2, of integers, both sorted in ascending order. You should write a function named setUnion(L1, L2) that adds to L1 any element from L2 that is not a member of L1. In other words, L1 will represent the union of L1 and L2 when the function returns. For instance, if you are given the two lists:

L1: (6, 8, 10, 20, 25, 30, 41, 66)
L2: (3, 6, 10, 33, 38, 41, 55, 66, 78, 90)
then your modified L1 will be:
L1: (3, 6, 8, 10, 20, 25, 30, 33, 38, 41, 55, 66, 78, 90)
L2 will remain unchanged. Write the function setUnion(L1,L2) using my dllist library.

Here is some helpful information:

  1. L1 and L2 are pointers to Dllists.
  2. Assume that dll_val returns an int and not a void *. Hence dll_val(node) will return the integer value of that node.
  3. The function does not return anything nor does it print anything. It achieves its result by modifying the two lists.
  4. When you reach end of L1, you may not have yet reached the end of L2, and hence you may have to add the remaining elements from L2 to L1.
  5. For maximum points, you should take advantage of the fact that the elements of L1 and L2 are sorted in ascending order and traverse each list only once. If you cannot figure out how to do this, you can traverse L1 and L2 multiple times for 12 points.
  6. You need to show me your function declaration and definition. Do not show me include files, statements to read data, etc. Assume L1 and L2 have been created for you by the calling function.

Dllist API

typedef void Dllist;
typedef void Dllist_Node;

extern Dllist *new_dllist();
extern void free_dllist(Dllist *l);
extern Dllist_Node *dll_prepend(Dllist *l, void *val);
extern Dllist_Node *dll_append(Dllist *l, void *val);
extern Dllist_Node *dll_insert_before(Dllist_Node *n, void *val);
extern Dllist_Node *dll_insert_after(Dllist_Node *n, void *val);
extern void dll_delete_node(Dllist_Node *n);
extern Dllist_Node *dll_sentinel(Dllist *l);
extern Dllist_Node *dll_first(Dllist *l);
extern Dllist_Node *dll_last(Dllist *l);
extern Dllist_Node *dll_next(Dllist_Node *n);
extern Dllist_Node *dll_prev(Dllist_Node *n);
extern int dll_empty(Dllist *l);
extern void *dll_val(Dllist_Node *n);