联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2019-03-28 08:57

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 2

becomes "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)


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp