feat: update structure
This commit is contained in:
44
cs2106/labs/lab1/part3/lab1p3c.c
Normal file
44
cs2106/labs/lab1/part3/lab1p3c.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
int age;
|
||||
} TPerson;
|
||||
|
||||
TPerson *makeNewNode(char *name, int age) {
|
||||
TPerson *p = (TPerson *) malloc(sizeof(TPerson));
|
||||
strcpy(p->name, name);
|
||||
p->age = age;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void freeNode(TPerson *node) {
|
||||
free(node);
|
||||
}
|
||||
|
||||
|
||||
#define NUM_PERSONS 3
|
||||
int main() {
|
||||
TPerson persons[NUM_PERSONS] = {{"Tan Ah Kow", 65}, {"Sio Bak Pau", 23},
|
||||
{"Aiken Dueet", 21}};
|
||||
TPerson *list[NUM_PERSONS];
|
||||
|
||||
int i;
|
||||
|
||||
printf("ADDING PERSONS\n");
|
||||
|
||||
for(i=0; i<NUM_PERSONS; i++) {
|
||||
printf("Adding %s aged %d\n", persons[i].name, persons[i].age);
|
||||
list[i] = makeNewNode(persons[i].name, persons[i].age);
|
||||
}
|
||||
|
||||
printf("\nDELETING PERSONS\n");
|
||||
for(i=0; i<NUM_PERSONS; i++) {
|
||||
printf("Deleting %s aged %d\n", list[i]->name, list[i]->age);
|
||||
freeNode(list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user