#define MAX (10) int main() { Init(); int i; for (i = 0; i < MAX; ++i) { Insert(i); } for (i = 0; i < MAX; ++i) { printf("Search:%d %s\n", i, Search(i) == true? "Success" : "Failure"); } printf("\n");
Delete(4); Delete(9); Delete(1); for (i = 0; i < MAX * 2; ++i) { printf("Search:%d %s\n", i, Search(i) == true? "Success" : "Failure"); }
#include <stdio.h> #include <string.h> #include <string> #include <map> #include <vector> #include <algorithm> #define MAX (100) using std::map; using std::string; using std::vector; using std::sort;
void pop() { if (nTime % 2) { while (!one.empty()) { T t = one.front(); one.pop(); if (!one.empty()) { two.push(t); } } } else { while (!two.empty()) { T t = two.front(); two.pop(); if (!two.empty()) { one.push(t); } } }
nTime = (nTime + 1) % 2; --nSize; }
T& top() { if (nTime % 2) { while (!one.empty()) { T t = one.front(); one.pop(); if (!one.empty()) { two.push(t); } else { two.push(t); nTime = (nTime + 1) % 2; return two.back(); } } } else { while (!two.empty()) { T t = two.front(); two.pop(); if (!two.empty()) { one.push(t); } else { one.push(t); nTime = (nTime + 1) % 2; return one.back(); } } } }
bool empty() { return nSize == 0; }
private: queue<T> one; queue<T> two; int nSize; int nTime; };
#define MAX 20
int main() { CStack<int> stack;
for (int i = 0; i < MAX; ++i) { stack.push(i); }
while (!stack.empty()) { printf("top: %d\n", stack.top()); stack.pop(); }