Linked List quiz

Answer each question to see whether you got it right and why. Your answers are saved in this browser.

0 correct · 0 / 48 answered
  1. Q1Linked list basics

    What is a linked list?

  2. Q2Linked list basics

    In a singly linked list, the last node's next pointer is nullptr.

  3. Q3Linked list basics

    Given the declarations below, which expression reads the count of the node that p points to?

    struct elType { std::string word; int count; };
    struct node { elType e; node* next; };
    node* p = new node;
  4. Q4Linked list basics

    After p = new node;, p->next is automatically nullptr.

  5. Q5Linked list basics

    What is the main advantage of a linked list over an array?

  6. Q6Insertion

    Which pair of statements correctly inserts node p at the head of the list, in the right order?

  7. Q7Insertion

    What goes wrong in this insert-at-head code?

    head = p;
    p->next = head;
  8. Q8Insertion

    To insert p after cursor, the first statement must be:

  9. Q9Insertion

    Inserting at the head works on an empty list without any special case.

  10. Q10Insertion

    Run this code on head -> 3 -> 5. Type the keys of the final list in order, separated by spaces.

    Node* a = new Node{1, head};
    head = a;
    Node* b = new Node{9, a->next};
    a->next = b;
  11. Q11Deletion

    Which sequence deletes the head node safely?

  12. Q12Deletion

    delete head; head = head->next; is a safe way to remove the first node.

  13. Q13Deletion

    Why does deleting a non-head node in a singly linked list need its predecessor?

  14. Q14Deletion

    In 4 -> 8 -> 12 -> 16, cursor points at 8. After this code runs, type the keys of the list in order, separated by spaces.

    Node* q = cursor->next;
    cursor->next = q->next;
    delete q;
  15. Q15Deletion

    A node created with new node should be freed with delete[].

  16. Q16Variants

    In a circular singly linked list, tail->next == head.

  17. Q17Variants

    Why does while (p != nullptr) { ...; p = p->next; } never finish on a circular list?

  18. Q18Variants

    In a doubly linked list, which statement moves the cursor to the previous node (slides notation)?

  19. Q19List ADT

    Why is the node class declared in the private part of List?

  20. Q20List ADT

    What is the purpose of the prev pointer in the List class?

  21. Q21List ADT

    toFirst() on an empty list leaves the current position empty.

  22. Q22List ADT

    When search(k) does not find the key, what happens?

  23. Q23List ADT

    After deleteEnd(), what is the current position?

  24. Q24List ADT

    Where does this loop leave cursor?

    toFirst();
    if (!listIsEmpty())
        while (cursor->next != NULL)
            advance();
  25. Q25List ADT

    What does listSize() return for the list 7 -> 2 -> 9?

    NodePointer q = head;
    int count = 0;
    while (q != NULL) {
        count++;
        q = q->next;
    }
    return count;
  26. Q26List ADT

    What happens if ~List() is empty?

  27. Q27List ADT

    After insertFirst(k, d), which node is current?

  28. Q28List ADT

    According to the lecture slides, insertAfter leaves the current position unchanged.

  29. Q29Pointers and lists

    A function must insert at the head and change the caller's head. Which parameter does it need?

  30. Q30Insertion and deletion

    Start with head -> A(4) -> B(8) -> C(12), tail at C. Insert D(6) after A, delete B, insert E(2) at the head, then append F(14). Type the node names in order, separated by spaces. (Practice sheet Q11)

  31. Q31Insertion and deletion

    Appending F after C when the list tracks head and tail: which assignments are needed? (Practice sheet Q11)

  32. Q32Insertion and deletion

    The list is A -> D -> B -> C. Which single assignment unlinks B (before freeing it)? (Practice sheet Q11)

  33. Q33Deletion

    A list holds 7 -> 3 -> 7. Using this removeFirst, after the first call removeFirst(7), type the keys of the list in order, separated by spaces. (Practice sheet Q12)

    bool removeFirst(int key) {
        Node* prev = nullptr;
        Node* node = head;
        while (node && node->key != key) {
            prev = node;
            node = node->next;
        }
        if (!node) return false;
        if (prev) prev->next = node->next;
        else head = node->next;
        if (node == tail) tail = prev;
        delete node;
        return true;
    }
  34. Q34Deletion

    After the second call removeFirst(7) on 3 -> 7, what key does tail point at? (Practice sheet Q12)

  35. Q35Deletion

    After removeFirst(3) on the one-node list 3, what are head and tail? (Practice sheet Q12)

  36. Q36Deletion

    In removeFirst, why is if (node == tail) tail = prev; needed? (Practice sheet Q12)

  37. Q37Insertion

    In this orderedInsert, the loop condition is next && next->key <= key. What does <= guarantee? (Practice sheet Q13)

    void orderedInsert(int key) {
        Node* prev = nullptr;
        Node* next = head;
        while (next && next->key <= key) {
            prev = next;
            next = next->next;
        }
        Node* node = new Node{key, next};
        if (prev) prev->next = node;
        else head = node;
        if (!next) tail = node;
    }
  38. Q38Insertion

    In 4 -> 9a -> 9b -> 15, where does the new node 9c go? (Practice sheet Q13)

  39. Q39Insertion

    Start with 4 -> 9a -> 9b -> 15. Insert 2, then 9c, then 20. Type the final list in order, separated by spaces. (Practice sheet Q13)

  40. Q40Insertion

    In orderedInsert, tail must be updated when the loop ends with next == nullptr. (Practice sheet Q13)

  41. Q41List ADT

    The list is 10 -> 20 -> 30, current is at 20. Using the sheet's rules, after insertAfter(25) which key does current hold? (Practice sheet Q14)

  42. Q42List ADT

    Continuing: current is at 25 in 10 -> 20 -> 25 -> 30. After advance(), which key does current hold? (Practice sheet Q14)

  43. Q43List ADT

    Continuing: current is at 30. After deleteEnd(), what key does tail hold? (current is cleared.) (Practice sheet Q14)

  44. Q44List ADT

    List 10 -> 20 -> 25, current is at 25 (after toEnd()). deleteHead() clears current. (Practice sheet Q14)

  45. Q45Variants

    Circular doubly list A <-> B <-> C <-> D, head = A. Which assignments remove A and make B the head? (Practice sheet Q15)

  46. Q47Variants

    After inserting X between D and B (head = B), type the first five nodes visited going forward from head, separated by spaces. (Practice sheet Q15)

  47. Q48Variants

    Same list: type the first five nodes visited going backward (prev) from head, separated by spaces. (Practice sheet Q15)