TOPIC 16.4.1
Analysis of the Doubly-linked List Program


The program uses the structure batter_node as the dynamic element in the linked list. Each node contains a string and a batter_node pointer, *next. The pointer, *next, is used to point to the next batter in the line up or the first batter if the batter is at the end of the list. The program also uses the batter_node pointers *first and *current_batter. The pointer *first is used to point to the beginning of the list and *current_batter is used as a temperary pointer to move through the batting list.

Four functions are used in the implementation of the batting program. Get_batter_name() is used to retrieve the name of a batter, if no name is specified the function returns 0. Add_node uses the new command to add the name passed into the linked list. Do_rotation begins by setting the pointer current_batter to the beginning of the list. The name of the current batter is then printed to the screen. The current_batter is then advanced to the next node. If Enter is pressed the process is again repeated until q or Q is entered. The last function is used to clean up the dynamically allocated linked list. The function begins by setting the current_batter pointer to the beginning of the list. The program then transverses the linked list while deleting each node. This continues until the first batter is found.

By using the four functions the main function become much simpler. Main begins by getting the first batters name. If no name is entered the program ends. Once a name is entered, the first node is created and the pointers first and current_batter are set to point to it. After the first node has been set up the functions get_batter_name and add_node can be used in a loop to add multiple batters to the lineup. Once all batters have been added into the line up, do_rotation can be called to display the lineup, and delete_list can be used to free the memory used by the linked list.