Question 1: Linked lists [8]

  1. Define a struct type, named ListNode, with two fields - one is an int and has the field name key, the other is a pointer to a ListNode and has the field name next.
    Sample solution
    struct ListNode {
       int key;
       ListNode *next;
    };
    

  2. Write a function named print that takes a pointer to a ListNode as its only parameter, and (assuming the parameter is a pointer to the front of a list) prints the contents of the linked list, displaying each node's key on a different line.
    Sample solution
    void print(ListNode *f)
    {
       ListNode *curr = f;
       while (curr != NULL) {
          cout << curr->key << endl;
          curr = curr->next;
       }
    }