Challenge 47

Description

The purpose of this challenge is to manually create a linked list to understand its basic structure.

Requirements

  1. Write a struct called StateInfo. This struct will be used as a linked list node.
    struct StateInfo
    {
      string abbv;
      string name;
      int population;
      StateInfo * next;
    };
    
  2. Create a function void show(StateInfo * ptr). This function will display each node’s information. This function shows the contents of all the nodes in the linked list. In this function,
    1. ptr will contain the address of the head (the first node in the linked list).
    2. Declare a pointer, StateInfo * curr. This pointer will start at the head of the list and will be used to traverse to the end of the list.
  3. Create a function int totalpop(StateInfo * ptr). This function will calculate the total population represented in the nodes of the linked list. The return value is the calculated total. In this function,
    1. ptr will contain the address of the head (the first node in the linked list).
    2. Declare a pointer, StateInfo * curr. This pointer will start at the head of the list and will be used to traverse to the end of the list.
  4. In main(), create a pointer StateInfo, StateInfo * head. This pointer will not be instantiated to a new StateInfo object. It will only be a pointer.
  5. In main(), create and instantiate a pointer variable, StateInfo * state1 = new StateInfo;
  6. Set state1’s properties to the following information: abbv: “CA”, name: “California”, population: 39400000
  7. Create and instantiate two more pointer variables, state2 and state3.
  8. Set state2’s properties to the following information: abbv: “OR”, name: “Oregon”, population: 4300000
  9. Set state3’s properties to the following information: abbv: “WA”, name: “Washington”, population: 7800000
  10. state1, state2 and state3 will represent 3 nodes of a  linked list. Use each of their respective next properties to link them in the order of state1-state2-state3. Remember that state3->next will point to NULL. Remember to point head to state1. 
  11. Call the show() function passing in head. You should see the 3 nodes’ information.
  12. Rearrange and reconnect the nodes in the order state3-state1-state2. Make sure that head points to the first node and the last node points to NULL
  13. Call the show() function passing in head. You should see the 3 nodes’ information in the rearranged order
  14. Call the totalpop() function passing in head to display the total population.
  15. Don’t forget to delete state1, state2 and state3. You do not need to delete head as it only held the address; it didn’t point to an instance of its own.

Sample Interaction

CA-California (population 39400000)
OR-Oregon (population 4300000)
WA-Washington (population 7800000)

WA-Washington (population 7800000)
CA-California (population 39400000)
OR-Oregon (population 4300000)

Total population: 51500000

CATALOG ID: CPP-CHAL00047

Print Requirements