-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity: append a node with student data
114 lines (91 loc) · 3.98 KB
/
Activity: append a node with student data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* In this task you will continue working on the linked list of students in which you would like to store, for each student, their name and age. As before you are provided with some code that you should not modify:
A structure definition for the storage of each student's information.
A main() function to test your code.
Prototypes for the functions createStudent() (from the previous task) and append() (from the current task).
You will need the function definition (from the previous task) for createStudent(), as well as any other functions you added, such as copyStr() for example. If you were unable to solve the previous task you have the option to be given the code for the createStudent() function (see the quiz preceding this task) so that you can work on the current task.
Your new task is to write a function append() which takes as input two pointers: the first pointer holds the address of the current end of the linked list of students, the second pointer points to a newly created student. Your function should append this new student to the linked list and return the address (a struct student *) of the new end of the list.
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[50];
int age;
struct student *next;
};
struct student *createStudent(char studentName[], int studentAge);
struct student *append(struct student * end, struct student * newStudptr);
// add other prototypes here if needed
int main(void) {
struct student *start, *newStudptr, *end, *tmp;
int ageP, ageR, ageM;
scanf("%d %d %d", &ageP, &ageR, &ageM);
start = createStudent("Petra", ageP);
end = start;
newStudptr = createStudent("Remi", ageR);
end = append(end, newStudptr);
newStudptr = createStudent("Mike", ageM);
end = append(end, newStudptr);
printf("%s is %d years old.\n", start->name, start->age);
printf("%s is %d years old.\n", start->next->name, start->next->age);
printf("%s is %d years old.\n", start->next->next->name, start->next->next->age);
tmp = start->next;
free(start);
start = tmp;
tmp = start->next;
free(start);
free(tmp);
return 0;
}
/* Place your function definitions here. Be sure to include the definition for
createStudent() and any other functions you created for the previous task. */
*/
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[50];
int age;
struct student *next;
};
struct student *createStudent(char studentName[], int studentAge);
struct student *append(struct student * end, struct student * newStudptr);
/* add other prototypes here if needed */
int main(void) {
struct student *start, *newStudptr, *end, *tmp;
int ageP, ageR, ageM;
scanf("%d %d %d", &ageP, &ageR, &ageM);
start = createStudent("Petra", ageP);
end = start;
newStudptr = createStudent("Remi", ageR);
end = append(end, newStudptr);
newStudptr = createStudent("Mike", ageM);
end = append(end, newStudptr);
printf("%s is %d years old.\n", start->name, start->age);
printf("%s is %d years old.\n", start->next->name, start->next->age);
printf("%s is %d years old.\n", start->next->next->name, start->next->next->age);
tmp = start->next;
free(start);
start = tmp;
tmp = start->next;
free(start);
free(tmp);
return 0;
}
/* Place your function definitions here. Be sure to include the definition for
createStudent() and any other functions you created for the previous task. */
struct student *createStudent(char studentName[], int studentAge){
struct student *ptr;
ptr = (struct student *)malloc(sizeof(struct student));
int i=0;
while(studentName[i]!='\0'){
ptr->name[i]=studentName[i];
i++;
}
//*ptr->name = studentName;
ptr->age = studentAge;
ptr->next = NULL;
return(ptr);
}
struct student *append(struct student * end, struct student * newStudptr){
end->next = newStudptr;
end = newStudptr;
return(end);
}