linked list讲解、辅导C++、讲解GitHub留学生、C++程序语言调试 讲解留学生Prolog|讲解数据库SQL

- 首页 >> OS编程
linked list.
Running valgrind on your program will show that the current program has memory leaks. Fix the program
such that it has no memory leaks.
Files that need to be on GitHub:
linked_list.h
linked_list.c
find_last_node.c
Makefile
Demo:
Show your code to your TA to show that you have implemented find_last (1 mark)
Compile and run your code (0.5 marks)
Run valgrind on your code to show that you have no memory leaks (0.5 marks)
Write a program called student_class.c that keeps track of students in a class. The program allows the
users to add or delete students. The program uses the following struct:
typedef struct {
int studentID;
char name[20];
} Student;
To keep track of students in the class, you will create an array of Student pointers. This array must be
dynamically allocated. Use malloc to initially allocate enough space for 2 students. If a new student
needs to be entered into the class (beyond the two initial students), you will need to use realloc to
increase the size of the array to accomodate the new student. A class has a maximum of 5 students.
The program prompts the user for the operation to perform: 1 if they want to add a student, 2 if they want to
delete a student, and 0 if they want to exit. When the program is done, it prints the current student list. You can
assume that only existing student IDs will be deleted and only unique IDs will be added.
When a new student is added, you need to (1) dynamically allocate space for that Student struct, (2) find
the first index in the array that has a free pointer (i.e., one that is NULL ), and (3) change that pointer to point
to the new student. At any point in the program, a student may be deleted from the list. In that case, you need
to search for the student and properly free the memory allocated for its struct. In that case, that array index
Part 2becomes "available" since the pointer stored in it is NULL . The following shows example program behavior:
$ ./student_class
Enter operation: 1
Enter ID to add (5 spots available): 1234
Enter student name: Alice
Enter operation: 1
Enter ID to add (4 spots available): 345
Enter student name: Bob
Enter operation: 0
You have 2 students in the class:
1234, Alice
345, Bob
$ ./student_class
Enter operation: 1
Enter ID to add (5 spots available): 1234
Enter student name: Alice
Enter operation: 1
Enter ID to add (4 spots available): 345
Enter student name: Bob
Enter operation: 1
Enter ID to add (3 spots available): 98
Enter student name: John
Enter operation: 2
Enter ID to drop: 345
Enter operation: 1
Enter ID to add (3 spots available): 100
Enter name: Mary
Enter operation: 0
You have 3 students in the class:
1234, Alice
100, Mary
345, John$ ./student_class
Enter operation: 1
Enter ID to add (5 spots available): 1234
Enter student name: Alice
Enter operation: 1
Enter ID to add (4 spots available): 345
Enter student name: Bob
Enter operation: 1
Enter ID to add (3 spots available): 98
Enter student name: John
Enter operation: 1
Enter ID to add (2 spots available): 100
Enter name: Mary
Enter operation: 1
Enter ID to add (1 spots available): 200
Enter name: Rob
You have reached the maximum capacity of the class. You have 5 students in the class:
1234, Alice
345, Bob
98, John
100, Mary
200, Rob
Your program must not have any memory leaks. You are encouraged to divide up your program into
functions for better modularity. You are encouraged to create a header file that contains your function
prototypes, as needed.
Files that need to be on GitHub:
student_class.c
any header files used
Makefile
Demo:
Show your code to your TA to show that you dynamically allocate memory for both the array elements and
the Student structs (1 mark)
Compile and run your code (1.5 marks)
Run valgrind on your code to show that you have no memory leaks (0.5 marks)

站长地图