CS140--Homework 5

  1. You are given the following declarations:
    char from_city[30];
    char to_city[30];
    char *search_city;
    
    1. Write a statement to copy the string in to_city to from_city.
    2. Write a statement to copy the string in to_city to search_city.
    3. Write a statement to copy the string in search_city to from_city.

  2. You are given a singly linked list, L, and another singly linked list, P, containing integers sorted in ascending order. You are to write a function named extract_list that will find the elements in L that are in the positions specified by P and copy their contents to a new list that you will create and return to the user. For example, if P = 1, 3, 4, 6, the first, third, fourth, and sixth elements of L will be copied to the new list. Your function should return a pointer to the new list. Since the elements of P are sorted you should only have to make one pass through L in order to create your new list. You do not have to compile or run your program. Just make a good faith effort to write the function.

    Problem Requirements:

    1. Assume that the nodes for both L and P have the following declaration:
      typedef struct node {
        int value;
        struct node *next;
      } Node;
      
    2. The function should have the following declaration:
      Node *extract_list(Node *L, Node *P);
      
    3. You should assume that L and P start with sentinel nodes.

    4. The list you return should start with a sentinel node.

  3. Write a function that given two sorted lists of integers, L1 and L2, prints the integers that are common to both lists. For example, if L1 contains the integers 1, 3, 6, 10, 12 and L2 contains the integers 2, 3, 10, 12, 15 then your program should print:
            3
            10
            12
            
    You should assume that you have the following definition for a list node:
            typedef struct node {
                struct node *next;
                int value;
            } Node;
    
    The lists do not have sentinel nodes so the pointers passed to your function point directly to the first nodes on their respective lists. The last element of each list will point to null (0). Your solution should take advantage of the fact that the two lists are sorted and traverse each list only once.
            void print_common_elements(Node *L1, Node *L2) {
            
  4. Write a function called reverse that takes a dlist as input and returns a new dlist with the elements of the first dlist reversed. The first dlist should be left unaltered. The value fields of the dlist will be char * pointers and you want to copy the strings, not the pointers to the new dlist. For example, if an element of the first dlist points to the string "jaguar", then you want to create a copy of "jaguar" and assign it to an element in the new dlist.

    Problem Requirements:

    1. The dlist that is passed as a parameter to reverse uses a sentinel node and the dlist that you return should have a sentinel node.

    2. The function's declaration is:
              Node *reverse(Node *input_list);
              
    3. You need to come up with a typedef to declare Node.