{ "q1": { "type": "blank", "question": "\n

\n\nComplete the following program, node.c, which is suppose to\ndeclare a struct Node that stores data, define a\nnode_create constructor function, uses this function to make a\nsingly linked list with a few values, and then loops through the\nlist and prints out the data associated with each Node.\n\n

\n\n
\n/* Node structure */\n\ntypedef struct Node Node;\nstruct Node {\n    int   data;\n    ____  next;               // 1.\n};\n\nNode *  node_create(int data, Node *next) {\n    Node * n = malloc(____);  // 2.\n    if (n) {\n        n->data = data;\n        n->next = next;\n    }\n    return n;\n}\n\n/* Main Execution */\n\nint main(int argc, char *argv[]) {\n    Node *head = node_create(7, node_create(1, node_create(5, ____)));  // 3.\n\n    for (Node *curr = ____; ____; ____ = ____) {  // 4. 5. 6. 7.\n      printf(\"%d\", ____);                         // 8.\n    }\n\n    return 0;\n}\n\n
\n" }, "q2": { "type": "multiple", "question": "\n

Given a completed version of the program node.c above, which of\nthe following statements are true (select all that apply)?

\n", "responses": { "valgrind_leak": "There is a memory leak.", "valgrind_invalid": "There is an invalid memory access.", "valgrind_init": "There is an uninitialized memory access.", "output_517": "The output of the program is 517.", "output_715": "The output of the program is 715.", "output_segfault": "The program segfaults." } }, "q3": { "type": "order", "question": "\n

Suppose we extended the program above into a new program called\nqueue.c and used the Node structure implement a\nQueue with the following code:

\n\n
\n/* Queue Structure */\n\ntypedef struct {\n    Node *head;\n} Queue;\n\nvoid\tqueue_push(Queue *q, int value) {\n      q->head = node_create(value, NULL);\n      curr->next = node_create(value, NULL);\n          curr = curr->next;\n    } else {\n      while (curr && curr->next)\n    if (!q->head) {\n    }\n      Node *curr = q->head;\n}\n\nint\tqueue_pop(Queue *q) {\n    int value = q->head->data;\n    q->head   = q->head->next;\n    return value;\n}\n
\n\n

Unfortunately, the code for queue_push is scrambled.\nReorder the following lines of code to properly implement\nqueue_push:

\n", "responses": { "a": "q->head = node_create(value, NULL);", "b": "curr->next = node_create(value, NULL);", "c": "curr = curr->next;", "d": "} else {", "e": "while (curr && curr->next)", "f": "if (!q->head) {", "g": "}", "h": "Node *curr = q->head;" } }, "q4": { "type": "multiple", "question": "\n

Given a completed version of the program queue.c above, which of\nthe following statements are true (select all that apply)?

\n", "responses": { "queue_push_time_1": "The average time complexity of queue_push is O(1).", "queue_push_time_n": "The average time complexity of queue_push is O(n).", "queue_pop_time_1": "The average time complexity of queue_pop is O(1).", "queue_pop_time_n": "The average time complexity of queue_pop is O(n).", "queue_push_segfault": "queue_push will segfault if we call it on an empty Queue.", "queue_pop_segfault": "queue_pop will segfault if we call it on an empty Queue." } } }