-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete a linked list
68 lines (62 loc) · 1.45 KB
/
Delete a linked list
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
#include <stdio.h>
#include <stdlib.h>
struct point{
int x;
int y;
struct point * next;
};
void printPoints(struct point *start);
struct point * createPoint(int x, int y) ;
struct point * append (struct point * end, struct point * newpt);
void freePoints(struct point * start);
int main(void) {
struct point * start, * end, * newpt;
int num, i;
int x, y;
printf("How many points? ");
scanf("%d", &num);
for (i=0; i<num; i++) {
printf("x = ");
scanf("%d", &x);
printf("y = ");
scanf("%d", &y);
newpt = createPoint(x,y);
if (i==0) {
start = end = newpt;
} else {
end = append(end,newpt);
}
}
printf("You entered: ");
printPoints(start);
freePoints(start);
return 0;
}
void printPoints(struct point *start) {
struct point * ptr;
ptr = start;
while (ptr!=NULL) {
printf("(%d, %d)\n", ptr->x, ptr->y);
ptr = ptr->next;
}
}
struct point * append (struct point * end, struct point * newpt) {
end->next = newpt;
return(end->next);
}
struct point * createPoint(int x, int y) {
struct point *ptr;
ptr = (struct point *)malloc(sizeof(struct point));
ptr->x = x;
ptr->y = y;
ptr->next = NULL;
return(ptr);
}
void freePoints(struct point * start) {
struct point * ptr = start;
while (ptr!=NULL) {
start = ptr;
ptr = ptr->next;
free(start);
}
}