-
Notifications
You must be signed in to change notification settings - Fork 0
/
Print a linked list
56 lines (48 loc) · 1.16 KB
/
Print 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
#include <stdio.h>
#include <stdlib.h>
struct digit {
int num;
struct digit *next;
};
struct digit * createDigit(int);
struct digit * append(struct digit * end, struct digit * newDigptr);
void printNumber(struct digit *start);
int main(void) {
struct digit *start, *newDigptr, *end, *tmp;
int first = 5;
int second = 3;
int third = 7;
start = createDigit(first);
end = start;
newDigptr = createDigit(second);
end = append(end, newDigptr);
newDigptr = createDigit(third);
end = append(end, newDigptr);
printNumber(start);
tmp = start->next;
free(start);
start = tmp;
tmp = start->next;
free(start);
free(tmp);
return 0;
}
struct digit *createDigit(int dig) {
struct digit *ptr;
ptr = (struct digit *) malloc(sizeof(struct digit));
ptr->num = dig;
ptr->next = NULL;
return ptr;
}
struct digit * append(struct digit * end, struct digit * newDigptr) {
end->next = newDigptr;
return(end->next);
}
void printNumber(struct digit *start){
struct digit * ptr = start;
while (ptr!=NULL) {
printf("%d", ptr->num);
ptr = ptr->next;
}
printf("\n");
}