-
Notifications
You must be signed in to change notification settings - Fork 26
/
linkedListAppendNode.c
46 lines (43 loc) · 1.17 KB
/
linkedListAppendNode.c
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
#include <stdio.h>
#include <stdlib.h>
struct digit {
int num;
struct digit * next;
};
struct digit * createDigit(int dig);
struct digit * append(struct digit * end, struct digit * newDigitptr);
int main(void) {
//! stack=showMemory(start=65520,cursors=[start,newDigitptr,end,tmp])
struct digit *start, *newDigitptr, *end, *tmp;
int first = 5;
int second = 3;
int third = 7;
start = createDigit(first);
end = start;
newDigitptr = createDigit(second);
end = append(end, newDigitptr);
newDigitptr = createDigit(third);
end = append(end, newDigitptr);
// free needs to be added here
tmp = start->next;
free(start);
start = tmp;
tmp = start->next;
free(start);
free(tmp);
return 0;
}
struct digit * append(struct digit * end, struct digit * newDigitptr) {
//! heap=showMemory(start=260, cursors=[end,newDigitptr])
end->next = newDigitptr;
end = newDigitptr;
return(end);
}
struct digit * createDigit(int dig) {
//! heap=showMemory(start=260, cursors=[ptr])
struct digit *ptr;
ptr = (struct digit *) malloc(sizeof(struct digit));
ptr->num = dig;
ptr->next = NULL;
return ptr;
}