void testList(int testSize) { printf("\n ==== Test %2d. Generate two lists each of size %d by random insertions\n", testID++, testSize); List<T> La; randomList(La, testSize); PRINT(La); List<T> Lb; randomList(Lb, testSize); PRINT(Lb); printf("\n ==== Test %2d. Call list members by rank (with high complexity)\n", testID++); for (int i = 0; i < La.size(); i++) print(La[i]->data); printf("\n"); for (int i = 0; i < Lb.size(); i++) print(Lb[i]->data); printf("\n"); printf("\n ==== Test %2d. Concatenation\n", testID++); PRINT(La); PRINT(Lb); while (0 < Lb.size()) La.insertAsLast(Lb.remove(Lb.first())); PRINT(La); PRINT(Lb); printf("\n ==== Test %2d. Increase\n", testID++); PRINT(La); increase(La); PRINT(La); printf("\n ==== Test %2d. Copy\n", testID++); PRINT(La); List<T> Ld(La); PRINT(Ld); printf("\n ==== Test %2d. Trim by random deletions\n", testID++); PRINT(Ld); while (testSize/4 < Ld.size()) { int N = rand() % Ld.size(); printf("removing L[%d]=", N); ListNodePosi(T) p = Ld.first(); while (0 < N--) p = p->succ; print(p->data); printf(" ...\n"); Ld.remove(p); PRINT(Ld); } printf("\n ==== Test %2d. Copy\n", testID++); PRINT(La); List<T> Le(La); PRINT(Le); printf("\n ==== Test %2d. FIND in\n", testID++); PRINT(Le); for (int i = 0; i <= testSize*2; i++) { //逐一测试[0, 2n]中的所有可能 ListNodePosi(T) p = Le.find((T) i); printf("Looking for "); print((T)i); printf(": "); if (p) { printf(" found with"); print(p->data); } else printf(" not found"); printf("\n"); } //正确的结构应该是大致(n+1次)失败、(n次)成功相间 printf("\n ==== Test %2d. Sort\n", testID++); PRINT(La); La.sort(); PRINT(La); printf("\n ==== Test %2d. SEARCH in\n", testID++); PRINT(La); for (int i = 0; i <= testSize*2; i++) { //逐一测试[0, 2n]中的所有可能 ListNodePosi(T) p = La.search((T) i); printf("Looking for "); print((T)i); printf(": "); printf(" stopped at"); print(p->data); if ((T) i == p->data) printf(" and found"); printf("\n"); } //正确的结构应该是大致(n+1次)失败、(n次)成功相间 printf("\n ==== Test %2d. Remove redundancy in\n", testID++); PRINT(La); printf("%d node(s) removed\n", La.uniquify()); PRINT(La); printf("\n ==== Test %2d. Remove redundancy in\n", testID++); PRINT(Le); printf("%d node(s) removed\n", Le.deduplicate()); PRINT(Le); printf("\n ==== Test %2d. Sort\n", testID++); PRINT(Le); Le.sort(); PRINT(Le); return; }
void randomList(List<T> & list, int n) { //在[0, 2n)中选择n个偶数,随机插入列表 ListNodePosi(T) p = (rand() % 2) ? list.insertAsLast(rand() % (T)n*2) : list.insertAsFirst(rand() % (T)n*2); for (int i=1; i<n; i++) p = rand() % 2 ? list.insertBefore(p, rand() % (T)n*2) : list.insertAfter(p, rand() % (T)n*2); }
// MARK: ListNode模板类 typedef int Rank; // 秩 #define ListNodePosi(T) ListNode<T>* // 列表节点位置 template <typename T> // 元素类型 class ListNode { public: // 成员 T data; // 数值 ListNodePosi(T) pred; // 前驱指针 ListNodePosi(T) succ; // 后继指针 // 构造函数 ListNode() {} // 针对header和trailer的构造 ListNode(T e, ListNodePosi(T) p = NULL, ListNodePosi(T) s = NULL):data(e), pred(p), succ(s) {} // 默认构造器 // 操作接口 ListNodePosi(T) insertAsPred(T const& e); // 紧靠当前节点之前插入新节点 ListNodePosi(T) insertAsSucc(T const& e); // 紧随当前节点之后插入新节点 }; // MARK: List模板类 //#include "lishNode.h" template <typename T> // 元素类型 class List { // 列表模板类 private: int _size; // 规模
ListNodePosi(T) insertAsSucc ( T const& e ){ ListNodePosi(T) x = new ListNode ( e, this, succ ); //创建新节点 succ->pred = x; succ = x; //设置逆向链接 return x; //返回新节点的位置 }