-
Notifications
You must be signed in to change notification settings - Fork 0
/
48.c
105 lines (88 loc) · 2.17 KB
/
48.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
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
#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 *);
void freeNumber(struct digit *);
struct digit *readNumber(void);
int divisibleByThree(struct digit * start);
int changeThrees(struct digit * start);
int countRedun(struct digit *start);
int main(void) {
struct digit *start;
start = readNumber();
printf("The number ");
printNumber(start);
printf("contains %d redundancies.\n", countRedun(start));
freeNumber(start);
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");
}
void freeNumber(struct digit *start) {
struct digit * ptr = start;
struct digit * tmp;
while (ptr!=NULL) {
tmp = ptr->next;
free(ptr);
ptr = tmp;
}
}
struct digit *readNumber(void) {
char c;
int d;
struct digit *start, *end, *newptr;
start = NULL;
scanf("%c", &c);
while (c != '\n') {
d = c-48;
newptr = createDigit(d);
if (start == NULL) {
start = newptr;
end = start;
} else {
end = append(end, newptr);
}
scanf("%c", &c);
}
return(start);
}
// countRedun() function here
int countRedun(struct digit *start) {
struct digit *ptr1 = start;
struct digit *ptr2;
int redun = 0;
while (ptr1 != NULL) {
ptr2 = ptr1->next;
while (ptr2 != NULL) {
if (ptr1->num == ptr2->num) {
redun += 1;
break; // Break out of the inner loop once a redundancy is found
}
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
return redun;
}