We talked about linked lists and other C data structures in class in Lecture 17 Data Structures. This lab provides an example of how we might use a singly linked list data structure. We are demonstrating a tug-of-war game.
We have two teams playing... a red team and a blue team. Each team starts out with four players, and each player has a specific weight (assigned randomly). When a tug of war occurs, the weights of all players on a team are added together, and the team with the largest total weight always wins the tug of war. The winning team then has a banquet (and gains some weight), but the winning team must transfer a randomly chosen player to the losing team. The teams then train for another month. During the training, players often gain weight. But the more weight a player gains, the more health problems he or she has, so heavier players get dropped from the game (with a random probability that increases as the weight goes up.) After a month the teams have another tug-of-war, and the process continues until one of the teams loses all it's players.
What does this have to do with linked lists? Every month, a player has to switch teams. If we kept the team list in an array, that would require lots of moving players around in the array. If we keep teams as linked lists instead, keeping track of the players is a lot easier.
In fact, we keep two linked lists... one that keeps track of all players, independent of what team they are on. This is so we can free up all the memory malloc'ed for players without having to keep track of what team they are on. There is a special head pointer called population that keeps track of the entire population, and the next pointer points at the next player in the population. A second linked list is kept for each team. There is a team structure that contains the head pointer for the team linked list, and a team pointer that points at the next person on the team.
The entire program is too complicated to code in a single lab period, so I'm providing most of the code for you. I've just left a few blank functions for you to code and test.
Download lab10.zip, move it to your CS211 directory, and from that directory, run the following command:
This will create a lab10 sub-directory of the current directory. The lab10 sub-directory has several files:
Once you have coded the missing functions in team.c, compile and test your code. Does it work correctly? Run it a few times. (Because of the random choices, you should get different results each time.)
Go to MyCourses, and open the CS-211 page, and choose the "Content" hyperlink on the left list of options. You will see a "Lab 10 Submission" icon to upload your team.c file. Make this submission before the due date of Friday, November 17, at 11:59 PM. You may submit as many times as you want before the due date, but only the latest submission will be graded.
This lab is worth 10 points. You will get deductions as follows: