int main() { int i, x; list *first = slist_init(); list *second = slist_init(); list *m; slist_push_back(first, 0); slist_push_back(first, 2); slist_push_back(first, 4); slist_push_back(first, 6); slist_push_back(first, 8); slist_show(first, mydump); slist_push_back(second, 1); slist_push_back(second, 3); slist_push_back(second, 5); slist_push_back(second, 7); slist_push_back(second, 9); slist_show(second, mydump); m = merge(first, second); slist_show(m, mydump); return 0; }
int main() { int data[LOOP]; int i, res; int* p; struct slist_t* sl; sl = slist_init(); assert(sl); for (i = 0; i < LOOP; ++ i) { data[i] = rand() % LOOP; res = slist_push_front(sl, &data[i]); assert(0 == res); res = slist_push_back(sl, &data[i]); assert(0 == res); } printf("list count=%d\n", slist_count(sl)); for (i = LOOP-1; i >= 0; -- i) { res = slist_find(sl, &data[i]); assert(0 == res); p = slist_pop_front(sl); assert(p == &data[i]); p = slist_pop_back(sl); assert(p == &data[i]); res = slist_find(sl, &data[i]); assert(res < 0); } printf("list count=%d\n", slist_count(sl)); slist_release(sl); sl = 0; return 0; }
int test_base_slist(const char* param) { struct slist_t* sl = slist_create(); if (!sl) { fprintf(stderr, "slist create fail\n"); return -1; } rand_seed(time(NULL)); int loop = param ? atoi(param) : 32; int data[loop]; for (int i = 0; i < loop; ++ i) { data[i] = (int)(rand_gen()) % loop; int res = slist_push_front(sl, &data[i]); CHECK(sl, res == 0, "slist push front fail"); res = slist_push_back(sl, &data[i]); CHECK(sl, res == 0, "slist push back fail"); } CHECK(sl, slist_size(sl) == 2 * loop, "slist size fail"); for (int i = loop - 1; i >= 0; -- i) { int res = slist_find(sl, &data[i]); CHECK(sl, res == 0, "slist find fail"); void* p = slist_pop_front(sl); CHECK(sl, p == &data[i], "slist pop front fail"); p = slist_pop_back(sl); CHECK(sl, p == &data[i], "slist pop back fail"); res = slist_find(sl, &data[i]); CHECK(sl, res < 0, "slist find fail"); } CHECK(sl, slist_size(sl) == 0, "slist size fail"); slist_release(sl); return 0; }
list *merge(list *first, list *second) { list *m; link *fl, *sl; if (slist_empty(first)) return second; if (slist_empty(second)) return first; m = slist_init(); fl = first->front; sl = second->front; while (fl && sl) { if (fl->value < sl->value) { slist_push_back(m, fl->value); fl = fl->next; } else if (fl->value == sl->value) { slist_push_back(m, fl->value); slist_push_back(m, sl->value); fl = fl->next; sl = sl->next; } else { slist_push_back(m, sl->value); sl = sl->next; } } if (fl) { while (fl) { slist_push_back(m, fl->value); fl = fl->next; } } else if (sl) { while (sl) { slist_push_back(m, sl->value); sl = sl->next; } } return m; }