Colorado State University

Recitation R5 - C Programming Exercise


CS270: Computer Organization

Goals

The Assignment

  1. Make a subdirectory called R5 for the quiz; all files should reside in this subdirectory.
  2. Copy R5.c to the R5 directory, and use it as a starting point for your program.
  3. Copy R5.h to the R5 directory, it defines the structures and functions needed.
  4. Copy R5Test.c to the R5 directory, it has test code you can use to test your implementation.
  5. Copy Makefile to the R5 directory, and use it to build the program. The program will not build until you complete the implementation in R5.c.
  6. Copy data.txt to the R5 directory. It contains one line per student, with the name, id, and GPA for each student, in the format Name ID GPA, as shown below:
            Brown 888887 3.1
            Campbell 235671 2.9
        
  7. Detailed Instructions:

    1. Design the data structure Student in R5.h, which is designed to be a node in a dynamically-allocated linked list of students, and make a typedef for it.
    2. Each node should contain the name, ID, and GPA, in addition to a link (pointer) to the next Student structure.
    3. Store the name as a char *, the student ID as an int, and the GPA as a float. The name should be a pointer that gets dynamically-allocated.
    4. The name should be allocated to have exactly the correct size, and should be allocated using malloc or calloc.
    5. Implement a function called insert that allocates and inserts a node into the linked list. Initially you can insert nodes at the head or tail.
    6. Use strdup to copy name into the node.
    7. Implement a function called delete that deletes a node from the linked list, based on the name, and frees associated memory.
    8. Implement a function called iterate that iterates through the nodes in order, calling the iterate function as defined in the header.
    9. Note the use of iterate in the test program to print the entries using printf in the following format, with two digits precision for GPA:
                  Brown (888887): 3.10
                  Campbell (235671): 2.90
              
    10. Implement a function called readFile that opens data.txt and reads the file, parsing one line at a time with fscanf, calling insert for each line.
    11. Implement a function called terminate that traverses the linked list, freeing the node, and the name it contains, not necessarily in that order!
    12. Test your code to see if you can store structures and enumerate them.
    13. Now extend the insert code to maintain an ordering of nodes that is case-sensitive and increases alphabetically, based on the student name.
    14. Prototypes for the above functions are as follows:
                  void readFile(char *filename);
      
                  void insertStudent(char *name, int id, float gpa);
      
                  void removeStudent(char *name);
      
                  void iterate(iterate_function func);
      
                  void terminate();
              
    15. The header file should include all the system header files that are required.

Notes

  1. You're not allowed to look at the file and hard-code the answers. This program must work no matter what information is in data.txt. For example, the sample data.txt has twenty lines, but you must not count on that in your program.
  2. You may assume that names are, at most, 80 characters long, including the null termination.
  3. You may assume that last names are unique in the file, thus you will not receive a delete that matches multiple nodes.
  4. You may use any resource on the Internet, or previous programs written for this course.
  5. However, you cannot receive help from or give help to any other student, this quiz is to be performed individually.
  6. HINT: Do the insert and iterate functions first, then readFile, then delete, and finally terminate.
  7. Package R5.c and R5.h into R5.tar using the make package command and submit to the Canvas drop box for R5.
  8. Only one submission is allowed, and no automated grading is supplied, since you already have the test program.
  9. Final grading for Recitation 5 is as follows:



    The terminate test code runs valgrind!