环形数组逆向遍历
复制代码
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
1991 #include <stdio.h> 2 #include <string.h> 3 4 #define shell_printf_greep(format,...) printf("33[42;37m"format"33[0m", ##__VA_ARGS__) 5 #define shell_printf_red(format,...) printf("33[41;37m"format"33[0m", ##__VA_ARGS__) 6 7 typedef struct _latest_pushid{ 8 #define POOL_LATEST_PUSHID_MAX 10 9 unsigned int is_full; // 是否已完成环形数组的第一遍存储 10 unsigned int next_index; 11 unsigned int pushid[POOL_LATEST_PUSHID_MAX]; 12 }latest_pushid_t; 13 14 latest_pushid_t g_latest_pushid = {0}; 15 16 17 #if 0 18 /* 19 新来一条消息, 查询是否曾经收过该pushid消息, 未收过则将此pushid放入池中 20 return 1: 曾经收过此pushid消息 21 0: 未曾收过此pushid消息 22 */ 23 int is_message_repeat(const unsigned int pushid){ 24 int index = 0; 25 26 int index_begin = (0 >= g_latest_pushid.next_index) ? (POOL_LATEST_PUSHID_MAX - 1) : (g_latest_pushid.next_index - 1); 27 int index_end = (0 == g_latest_pushid.is_full) ? 0 : g_latest_pushid.next_index; 28 29 //printf("index_begin=%d, index_end=%drn", index_begin, index_end); 30 31 if (0 == g_latest_pushid.is_full && POOL_LATEST_PUSHID_MAX - 1 == index_begin) 32 { 33 // printf("empth arrayrn"); 34 } 35 else if (1 == g_latest_pushid.is_full && POOL_LATEST_PUSHID_MAX - 1 == index_begin) 36 { 37 for (index = POOL_LATEST_PUSHID_MAX - 1; index >=0; index--) 38 { 39 // printf("for index=%drn", index); 40 if (pushid == g_latest_pushid.pushid[index]){ 41 return 1; 42 } 43 } 44 } 45 else 46 { 47 int touch_begin = 0; 48 49 for (index = index_begin; (0 == touch_begin) || (1 == touch_begin && index >= index_end); ){ 50 // printf("for index=%d, touch_begin=%drn", index, touch_begin); 51 if (pushid == g_latest_pushid.pushid[index]){ 52 return 1; 53 } 54 55 if ( 0 == index ){ 56 touch_begin = 1; 57 } 58 59 index--; 60 if (0 > index) 61 { 62 if (0 == g_latest_pushid.is_full) 63 break; 64 else 65 index = POOL_LATEST_PUSHID_MAX - 1; 66 } 67 68 // usleep(100 * 1000); 69 70 } 71 } 72 73 g_latest_pushid.pushid[g_latest_pushid.next_index] = pushid; 74 75 g_latest_pushid.next_index++; 76 77 if(POOL_LATEST_PUSHID_MAX <= g_latest_pushid.next_index){ 78 g_latest_pushid.is_full = 1; 79 } 80 81 g_latest_pushid.next_index = g_latest_pushid.next_index % POOL_LATEST_PUSHID_MAX; 82 83 return 0; 84 } 85 86 #elif 1 // 此方法最好 87 int is_message_repeat(const unsigned int pushid){ 88 int index = 0; 89 int index_array = 0; 90 91 92 int index_max = (0 == g_latest_pushid.is_full) ? g_latest_pushid.next_index : POOL_LATEST_PUSHID_MAX; 93 for (index = 0; index < index_max; index++){ 94 index_array = (g_latest_pushid.next_index - 1 - index + POOL_LATEST_PUSHID_MAX) % POOL_LATEST_PUSHID_MAX; 95 printf("for index=%d, index_array=%drn", index, index_array); 96 if (pushid == g_latest_pushid.pushid[index_array]){ 97 return 1; 98 } 99 } 100 101 g_latest_pushid.pushid[g_latest_pushid.next_index] = pushid; 102 103 // make the next_index 104 g_latest_pushid.next_index++; 105 106 if(POOL_LATEST_PUSHID_MAX <= g_latest_pushid.next_index){ 107 g_latest_pushid.is_full = 1; 108 } 109 110 g_latest_pushid.next_index = g_latest_pushid.next_index % POOL_LATEST_PUSHID_MAX; 111 112 return 0; 113 } 114 #else 115 int is_message_repeat(const unsigned int pushid){ 116 int index = 0; 117 int index_array[POOL_LATEST_PUSHID_MAX]; 118 119 for (index = 0; index < POOL_LATEST_PUSHID_MAX; index++){ 120 index_array[index] = (g_latest_pushid.next_index - 1 - index + POOL_LATEST_PUSHID_MAX) % POOL_LATEST_PUSHID_MAX; 121 } 122 123 int end_index_of_index_array = (0 == g_latest_pushid.is_full) ? g_latest_pushid.next_index : POOL_LATEST_PUSHID_MAX; 124 for (index = 0; index < end_index_of_index_array; index++){ 125 if (pushid == g_latest_pushid.pushid[index_array[index]]){ 126 return 1; 127 } 128 } 129 130 g_latest_pushid.pushid[g_latest_pushid.next_index] = pushid; 131 132 // make the next_index 133 g_latest_pushid.next_index++; 134 135 if(POOL_LATEST_PUSHID_MAX <= g_latest_pushid.next_index){ 136 g_latest_pushid.is_full = 1; 137 } 138 139 g_latest_pushid.next_index = g_latest_pushid.next_index % POOL_LATEST_PUSHID_MAX; 140 141 return 0; 142 } 143 144 #endif 145 146 int print_circle_array(){ 147 int index = 0; 148 149 for (index = 0; index < POOL_LATEST_PUSHID_MAX; index++) 150 printf("%u, ", g_latest_pushid.pushid[index]); 151 152 printf("rn"); 153 154 } 155 156 int main(int argc, char *argv[]) 157 { 158 int index = 0; 159 160 int a[POOL_LATEST_PUSHID_MAX] = {0}; 161 162 for ( index = 100; ; index++){ 163 int is_in = 0; 164 int j = 0; 165 166 int ask = index; 167 is_in = is_message_repeat(index); 168 printf("ask=%d, is_in=", ask); 169 170 if (1 == is_in) 171 shell_printf_greep("Yes."); 172 else 173 shell_printf_red("No"); 174 175 printf("rn"); 176 177 print_circle_array(); 178 179 for (j = 0; j<POOL_LATEST_PUSHID_MAX; j++) 180 { 181 ask = g_latest_pushid.pushid[j]; 182 is_in = is_message_repeat(ask); 183 // printf("ask=%d, is_in=%s.rn", ask, 1 == is_in ? "Yes" : "No"); 184 printf("ask=%d, is_in=", ask); 185 186 if (1 == is_in) 187 shell_printf_greep("Yes."); 188 else 189 shell_printf_red("No"); 190 191 printf("rn"); 192 print_circle_array(); 193 194 } 195 196 sleep(1); 197 } 198 return 0; 199 }
转载于:https://www.cnblogs.com/LiuYanYGZ/p/10289635.html
最后
以上就是忧伤太阳最近收集整理的关于环形数组逆向遍历的全部内容,更多相关环形数组逆向遍历内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复