数据结构
链表:
1 /************************************************************************* 2 > File Name: List_BaseArray.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年12月09日 星期六 15时57分11秒 6 ************************************************************************/ 7 8 #include <bits/stdc++.h> 9 using namespace std; 10 template<typename T> 11 class List{ 12 private: 13 int maxsize; 14 int cnt; 15 int cur; 16 T *listArray; 17 public: 18 void setStart(){ 19 cur = 0; 20 } 21 void setEnd(){ 22 cur = cnt; 23 } 24 void pre(){ 25 cur--; 26 } 27 void nxt(){ 28 cur++; 29 } 30 int leftLength(){ 31 return cur; 32 } 33 int rightLength(){ 34 return cnt - cur; 35 } 36 void setPos(int pos){ 37 cur = pos; 38 } 39 T getVal(){ 40 return listArray[cur]; 41 } 42 void insert(T temp){ 43 for(int i = cnt; i > cur; i--){ 44 listArray[i] = listArray[i-1]; 45 } 46 listArray[cur] = temp; 47 cnt++; 48 } 49 void append(T temp){ 50 listArray[cnt++] = temp; 51 } 52 T remove(){ 53 T temp = listArray[cur]; 54 for(int i = cur; i < cnt; i++){ 55 listArray[i] = listArray[i+1]; 56 } 57 cnt--; 58 return temp; 59 } 60 }; 61 int main(){ 62 63 }
1 /************************************************************************* 2 > File Name: List_BaseLink.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年12月09日 星期六 16时09分37秒 6 ************************************************************************/ 7 8 #include <bits/stdc++.h> 9 using namespace std; 10 11 const int maxn = 10010; 12 int ary[maxn]; 13 template<typename T> 14 class Node{ 15 public: 16 T x; 17 Node *nxt; 18 Node(T temp, Node *p = NULL){ 19 x = temp; 20 nxt = p; 21 } 22 Node(Node *p = NULL){ 23 nxt = p; 24 } 25 }; 26 template<typename T> 27 class List{ 28 private: 29 Node<T> *head, *tail, *cur; 30 int lcnt, rcnt; 31 public: 32 List(int sz = maxn){ 33 init(); 34 } 35 void init(){ 36 cur = head = tail = new Node<T>(); 37 lcnt = rcnt = 0; 38 } 39 ~List(){ 40 removeAll(); 41 } 42 void clear(){ 43 removeAll(); 44 init(); 45 } 46 void removeAll(){ 47 while(head != NULL){ 48 cur = head; 49 delete cur; 50 head = head->nxt; 51 } 52 } 53 void setStart(){ 54 cur = head; 55 rcnt += lcnt; 56 lcnt = 0; 57 } 58 void setEnd(){ 59 cur = tail; 60 lcnt += rcnt; 61 rcnt = 0; 62 } 63 int leftLength(){ 64 return lcnt; 65 } 66 int rightLength(){ 67 return rcnt; 68 } 69 T getVal(){ 70 T temp = cur->nxt->x; 71 return temp; 72 } 73 void insert(T temp){ 74 cur->nxt = new Node<T>(temp, cur->nxt); 75 if(tail == cur) tail = cur->nxt; 76 rcnt++; 77 } 78 void append(T temp){ 79 tail->nxt = new Node<T>(temp); 80 tail = tail->nxt; 81 rcnt++; 82 } 83 T remove(){ 84 T temp = cur->nxt->x; 85 Node<T> *p = cur->nxt; 86 cur->nxt = p->nxt; 87 if(tail == p){ 88 tail = cur; 89 } 90 delete p; 91 rcnt--; 92 return temp; 93 } 94 bool pre(){ 95 if(pre == head){ 96 return false; 97 } 98 Node<T> *temp = head; 99 while(temp->nxt != cur) temp = temp->nxt; 100 cur = temp; 101 lcnt--; 102 rcnt++; 103 return true; 104 } 105 bool nxt(){ 106 if(cur->nxt == tail){ 107 return false; 108 } 109 cur = cur->nxt; 110 lcnt++; 111 rcnt--; 112 return true; 113 } 114 void setPos(int pos){ 115 cur = head; 116 for(int i = 0; i < pos; i++){ 117 cur = cur->nxt; 118 } 119 rcnt = rcnt + lcnt - pos; 120 lcnt = pos; 121 } 122 }; 123 124 int main(){ 125 for(int i= 0 ; i < maxn; i++) ary[i] = rand() % 1313131; 126 while(1){ 127 cout<<"请输入链表长度:"<<endl; 128 int n; 129 cin>>n; 130 for(int i = 0; i < n; i++) cout<<ary[i]<<" "; 131 cout<<endl; 132 List<int> list; 133 list.init(); 134 list.insert(ary[0]); 135 for(int i = 1; i < n; i++){ 136 list.append(ary[i]); 137 } 138 cout<<list.leftLength()<<endl; 139 cout<<list.rightLength()<<endl; 140 cout<<list.getVal()<<" "; 141 while(list.nxt()){ 142 cout<<list.getVal()<<" "; 143 } 144 cout<<endl; 145 } 146 147 }
BST
1 /************************************************************************* 2 > File Name: bitree.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年11月12日 星期日 15时05分20秒 6 ************************************************************************/ 7 #include <iostream> 8 #include <bits/stdc++.h> 9 using namespace std; 10 template <typename T> 11 class Node{ 12 public: 13 T date; 14 Node *L, *R; 15 Node(){} 16 Node(T date, Node<T> *L = NULL, Node<T> *R = NULL) : date(date), L(L), R(R){} 17 // T& getVal(){ 18 // return date; 19 // } 20 // void setVal(T x){ 21 // date = x; 22 // } 23 // Node<T>* getLeftChild(){ 24 // return *L; 25 // } 26 // Node<T>* getRightChild(){ 27 // return *R; 28 // } 29 // void setLeftChild(Node *b){ 30 // L = b; 31 // } 32 // void setRightChild(Node *b){ 33 // R = b; 34 // } 35 // bool isLeaf(){ 36 // return L == NULL && R == NULL; 37 // } 38 }; 39 template <typename T> 40 class bitree{ 41 private: 42 Node<T> * rt; 43 public: 44 bitree(){ 45 rt = NULL; 46 } 47 Node<T>*& getRoot(){ 48 return rt; 49 } 50 Node<T>* getptrRoot(){ 51 return rt; 52 } 53 void preOrder(Node<T> *sub){ 54 if(sub == NULL){ 55 return ; 56 } 57 cout<<sub->date<<" "; 58 preOrder(sub->L); 59 preOrder(sub->R); 60 } 61 void inOrder(Node<T> *sub){ 62 if(sub == NULL) { 63 return ; 64 } 65 inOrder(sub->L); 66 cout<<sub->date<<" "; 67 inOrder(sub->R); 68 } 69 void postOrder(Node<T> *sub){ 70 if(sub == NULL){ 71 return ; 72 } 73 postOrder(sub->L); 74 postOrder(sub->R); 75 cout<<sub->date<<" "; 76 } 77 void traverse(){ 78 puts("前序遍历: "); 79 preOrder(getptrRoot()); 80 cout<<endl; 81 puts("中序遍历: "); 82 inOrder(getptrRoot()); 83 cout<<endl; 84 puts("后序遍历: "); 85 postOrder(getptrRoot()); 86 cout<<endl; 87 } 88 void addNode(Node<T> *&tp, T x){ 89 if(tp == NULL){ 90 Node<T> *p = new Node<T>(x); 91 tp = p; 92 }else if(tp->date > x){ 93 addNode(tp->L, x); 94 }else{ 95 addNode(tp->R, x); 96 } 97 } 98 void levelTraverse(){ 99 queue<Node<T> *> q; 100 cout<<"层次遍历: \n"; 101 if(rt != NULL) q.push(rt); 102 while(!q.empty()){ 103 Node<T> *p = q.front(); 104 q.pop(); 105 cout<<p->date<<" "; 106 if(p->L != NULL) q.push(p->L); 107 if(p->R != NULL) q.push(p->R); 108 } 109 cout<<endl; 110 } 111 }; 112 template <typename T> 113 void countLeaves(Node<T> *p, int &cnt){ 114 if(p->L == NULL && p->R == NULL){ 115 cnt++; 116 return ; 117 }else { 118 if(p->L != NULL) countLeaves(p->L, cnt); 119 if(p->R != NULL) countLeaves(p->R, cnt); 120 } 121 } 122 int height; 123 template <typename T> 124 void countLevel(Node<T> *p, int h){ 125 if(p->L == NULL && p->R == NULL){ 126 height = max(height, h); 127 return ; 128 }else{ 129 if(p->L != NULL) countLevel(p->L, h+1); 130 if(p->R != NULL) countLevel(p->R, h+1); 131 } 132 133 } 134 void init(){ 135 freopen("in.txt", "w", stdout); 136 for(int i = 0; i < 100; i++){ 137 int n = rand()%13 + 5; 138 cout<<n<<endl; 139 for(int j = 0; j < n; j++){ 140 cout<<rand() % 1313131<<" "; 141 } 142 cout<<endl; 143 } 144 } 145 int main(){ 146 init(); 147 freopen("in.txt", "r", stdin); 148 freopen("out.txt", "w", stdout); 149 int n; 150 int kase = 0; 151 while(cin>>n){ 152 cout<<"\nCase #"<<++kase<<":\n"; 153 bitree<int> bt; 154 for(int i = 0; i < n; i++){ 155 int x; 156 cin>>x; 157 bt.addNode(bt.getRoot(), x); 158 } 159 bt.traverse(); 160 bt.levelTraverse(); 161 int cnt = 0; 162 countLeaves(bt.getptrRoot(), cnt); 163 cout<<"叶子数: "<<cnt<<endl; 164 int h = 0; 165 height = 0; 166 countLevel(bt.getptrRoot(), h); 167 cout<<"层数: "<<height<<endl; 168 } 169 }
Heap
1 #include <iostream> 2 using namespace std; 3 4 template<typename T> 5 class Heap{ 6 public: 7 T *h; 8 int n; 9 int size; 10 public: 11 Heap(T *h, int n, int size){ 12 this.h = h; 13 this.n = n; 14 this.size = size; 15 } 16 int lson(int pos){ 17 return pos*2+1; 18 } 19 int rson(int pos){ 20 return pos*2+2; 21 } 22 int par(int pos){ 23 return (pos-1)/2; 24 } 25 bool isLeaf(int pos){ 26 return pos >= pos/2 && pos < n; 27 } 28 void siftdown(int pos){ 29 while(!isLeaf(pos)){ 30 int lc = lson(pos); 31 int rc = rson(pos); 32 if(rc < n && h[lc] < h[rc]) lc = rc; 33 if(h[pos] >= h[lc]) return; 34 swap(h[pos], h[lc]); 35 pos = lc; 36 } 37 } 38 void buildHeap(){ 39 for(int i = n/2-1; i >= 0; i--) siftdown(i); 40 } 41 42 bool removemax(T &it){ 43 if(n == 0) return false; 44 swap(h[0], h[--n]); 45 if(n != 0) siftdown(0); 46 it = h[n]; 47 return true; 48 } 49 bool insert(const T &it){ 50 if(n >= size) return false; 51 int cur = n++; 52 h[cur] = it; 53 while(cur != 0 && h[cur] > h[par(cur)]){ 54 swap(h[cur], h[par[cur]]); 55 cur = par(cur); 56 } 57 return true; 58 } 59 }; 60 61 int main(){ 62 63 }
排序
1 /************************************************************************* 2 > File Name: 8sort.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年11月14日 星期二 22时12分19秒 6 ************************************************************************/ 7 8 #include <bits/stdc++.h> 9 using namespace std; 10 const int maxn = 10000010; 11 const int mod = 13131313; 12 13 int a[maxn], c[maxn], ary[maxn]; 14 template <typename T> 15 void print(T *a, int n){ 16 for(int i = 0; i < n; i++){ 17 cout<<a[i]<<" "; 18 } 19 cout<<endl; 20 } 21 22 template <typename T> 23 void insertSort(T *a, int n){ 24 for(int i = 1; i < n; i++){ 25 for(int j = i; j > 0 && a[j] < a[j-1]; j--){ 26 swap(a[j], a[j-1]); 27 } 28 } 29 } 30 31 template <typename T> 32 void bubbleSort(T *a, int n){ 33 for(int i = 0; i < n - 1; i++){ 34 int ok = 0; 35 //0到i已经排好序 36 for(int j = n - 1; j > i; j--){ 37 if(a[j] < a[j-1]) { 38 swap(a[j], a[j-1]); 39 ok = 1; 40 } 41 } 42 if(!ok) break; 43 } 44 } 45 template <typename T> 46 void doubleSelectSort(T *a, int n){ 47 for(int i = 0; i < n; i++){ 48 //从j到n找到最小的元素和i交换 49 int min = i, max = n - 1 - i; 50 for(int j = i; j <= n - 1 - i; j++){ 51 if(a[j] < a[min]) min = j; //稳定 52 if(a[j] > a[max]) max = j; 53 } 54 swap(a[i], a[min]); 55 swap(a[n-i-1], a[max]); 56 } 57 } 58 template <typename T> 59 void selectSort(T *a, int n){ 60 for(int i = 0; i < n; i++){ 61 //从j到n找到最小的元素和i交换 62 int id = i; 63 for(int j = i + 1; j < n; j++){ 64 if(a[j] < a[id]) id = j; //稳定 65 } 66 swap(a[i], a[id]); 67 } 68 } 69 70 template <typename T> 71 void insertSort(T *a, int n, int from, int incr){ 72 for(int i = from + incr; i < n; i += incr){ 73 for(int j = i; j > from && a[j] < a[j-incr]; j -= incr){ 74 swap(a[j], a[j-incr]); 75 } 76 } 77 } 78 template <typename T> 79 void shellSort(T *a, int n){ 80 //分 81 for(int i = n / 2; i >= 1; i /= 2){ 82 for(int j = 0; j < i; j++){ 83 insertSort(a, n, j, i); 84 } 85 } 86 } 87 88 template <typename T> 89 void quickSort(T *a, int s, int e, int n){ 90 if(s >= e) return ; 91 int id = s; 92 T temp = a[id]; 93 int i = s, j = e; 94 while(i < j){ 95 while(j > i && a[j] >= temp) j--; 96 if(i < j){ 97 a[i++] = a[j]; 98 } 99 while(i < j && a[i] < temp) i++; 100 if(i < j) { 101 a[j--] = a[i]; 102 } 103 } 104 a[i] = temp; 105 quickSort(a, s, i-1, n); 106 quickSort(a, i+1, e, n); 107 return ; 108 } 109 template <typename T> 110 void mergeSort(T *a, int l, int r, T *c){ 111 if(r - l > 1) { 112 int m = l + (r - l) / 2; 113 int p = l, q = m, i = l; 114 mergeSort(a, l, m, c); 115 mergeSort(a, m, r, c); 116 while(p < m || q < r){ 117 if(q >= r || (p < m && a[p] <= a[q])) c[i++] = a[p++]; 118 else c[i++] = a[q++]; 119 } 120 for(int i = l; i < r; i++) a[i] = c[i]; 121 } 122 } 123 124 //template <typename T> 125 //class MaxHeap{ 126 // public: 127 // int cnt; 128 // T heap[maxn]; 129 // 130 // MaxHeap(){} 131 // MaxHeap(T *a, int n){ 132 // init(a, n); 133 // } 134 // void init(T *a, int n){ 135 // cnt = 0; 136 // for(int i = 0; i < n; i++) heap[++cnt] = a[i]; 137 // for(int i = cnt / 2; i >= 1; i--) siftdown(i); 138 // } 139 // void init(){ 140 // cnt = 0; 141 // } 142 // 143 // void buildHeap(T *a, int n){ 144 // for(int i = 0; i < n; i++) heap[++cnt] = a[i]; 145 // for(int i = cnt / 2; i >= 1; i--) siftdown(i); 146 // } 147 // void siftdown(int pos){ 148 // while(pos <= cnt/2){ 149 // int ls = pos * 2; 150 // int rs = pos * 2 + 1; 151 // if(rs <= cnt && heap[ls] < heap[rs]){ 152 // ls = rs; 153 // } 154 // if(heap[pos] >= heap[ls]) return ; 155 // swap(heap[pos], heap[ls]); 156 // pos = ls; 157 // } 158 // } 159 // bool insert(T x){ 160 // int cur = ++cnt; 161 // heap[cur] = x; 162 // while(cur != 1 && heap[cur] > heap[cur/2]){ 163 // swap(heap[cur], heap[cur/2]); 164 // cur /= 2; 165 // } 166 // return 1; 167 // } 168 // T removeMax(){ 169 // swap(heap[1], heap[cnt--]); 170 // if(cnt != 1) siftdown(1); 171 // return heap[cnt+1]; 172 // } 173 // //? 174 // void remove(int pos, T &it){ 175 // swap(heap[pos], heap[cnt--]); 176 // while(pos != 1 && heap[pos] > heap[pos / 2]){ 177 // swap(heap[pos], heap[pos / 2]); 178 // pos /= 2; 179 // } 180 // siftdown(pos); 181 // it = heap[cnt+1]; 182 // } 183 // void print(){ 184 // for(int i = 1; i <= cnt; i++) cout<<heap[i]<<" "; 185 // cout<<endl; 186 // } 187 //}; 188 //MaxHeap<int> H; 189 // 190 // 191 //template <typename T> 192 //void heapSort(T *a, int n){ 193 // //MaxHeap<T> H(a, n); 194 // //MaxHeap<T> H; 195 // //H.init(); 196 // //for(int i = 0; i < n; i++) H.insert(a[i]); 197 // //H.print(); 198 // H.init(a, n); 199 // for(int i = 0; i < n; i++){ 200 // a[n-i-1] = H.removeMax(); 201 // } 202 //} 203 void siftdown(int cur, int n){ 204 while(cur <= n/2){ 205 int ls = cur * 2; 206 int rs = cur * 2 + 1; 207 if(rs <= n && a[ls] < a[rs]) ls = rs; 208 if(a[cur] >= a[ls]) break; 209 swap(a[cur], a[ls]); 210 cur = ls; 211 } 212 } 213 214 template <typename T> 215 void buildHeap(T *a, int n){ 216 for(int i = n / 2; i >= 1; i--){ 217 siftdown(i, n); 218 } 219 } 220 221 template <typename T> 222 void heapSort(T *a, int n){ 223 for(int i = n; i > 0; i--) a[i] = a[i-1]; 224 buildHeap(a, n); 225 for(int i = n; i >= 1; i--){ 226 swap(a[1], a[i]); 227 siftdown(1, i - 1); 228 } 229 for(int i = 0; i < n; i++){ 230 a[i] = a[i+1]; 231 } 232 } 233 234 235 template <typename T> 236 void binSort(T *a, int n, T *c){ 237 for(int i = 0; i < mod; i++){ 238 c[i] = 0; 239 } 240 for(int i = 0; i < n; i++){ 241 c[a[i]]++; 242 } 243 int cur = 0; 244 for(int i = 0; i < mod; i++){ 245 for(int j = 0; j < c[i]; j++){ 246 a[cur++] = i; 247 } 248 } 249 } 250 251 template <typename T> 252 int maxBit(T *a, int n){ 253 int d = 1; 254 int p = 10; 255 for(int i = 0; i < n; i++){ 256 while(a[i] >= p){ 257 p *= 10; 258 d++; 259 } 260 } 261 return d; 262 } 263 template <typename T> 264 void radixSort(T *a, int n, T *c){ 265 int d = maxBit(a, n); 266 int radix = 1; 267 int *cnt = new int[10]; 268 for(int k = 1; k <= d; k++){ 269 for(int i = 0; i < 10; i++){ 270 cnt[i] = 0; 271 } 272 for(int i = 0; i < n; i++){ 273 int k = (a[i] / radix) % 10; 274 cnt[k]++; 275 } 276 for(int i = 1; i < 10; i++){ 277 cnt[i] = cnt[i-1] + cnt[i]; 278 } 279 for(int i = n - 1; i >= 0; i--){ 280 int k = (a[i] / radix) % 10; 281 c[--cnt[k]] = a[i]; 282 } 283 for(int i = 0; i < n; i++){ 284 a[i] = c[i]; 285 } 286 radix *= 10; 287 } 288 delete[] cnt; 289 } 290 291 template <typename T> 292 void Sort(T *a, int n, T *c){ 293 double st, ed; 294 for(int i = 0; i < n; i++){ 295 a[i] = ary[i]; 296 } 297 st = clock(); 298 insertSort(a, n); 299 ed = clock(); 300 cout<<"插入排序:"<<ed-st<<endl; 301 for(int i = 0; i < n; i++){ 302 a[i] = ary[i]; 303 } 304 st = clock(); 305 bubbleSort(a, n); 306 ed = clock(); 307 cout<<"冒泡排序:"<<ed-st<<endl; 308 for(int i = 0; i < n; i++){ 309 a[i] = ary[i]; 310 } 311 st = clock(); 312 selectSort(a, n); 313 ed = clock(); 314 cout<<"选择排序:"<<ed-st<<endl; 315 for(int i = 0; i < n; i++){ 316 a[i] = ary[i]; 317 } 318 st = clock(); 319 shellSort(a, n); 320 ed = clock(); 321 cout<<"希尔排序:"<<ed-st<<endl; 322 for(int i = 0; i < n; i++){ 323 a[i] = ary[i]; 324 } 325 st = clock(); 326 quickSort(a, 0, n-1, n); 327 ed = clock(); 328 cout<<"快速排序:"<<ed-st<<endl; 329 //print(a, n); 330 for(int i = 0; i < n; i++){ 331 a[i] = ary[i]; 332 } 333 st = clock(); 334 mergeSort(a, 0, n, c); 335 ed = clock(); 336 //print(a, n); 337 cout<<"归并排序:"<<ed-st<<endl; 338 for(int i = 0; i < n; i++){ 339 a[i] = ary[i]; 340 } 341 st = clock(); 342 heapSort(a, n); 343 ed = clock(); 344 cout<<"堆排序:"<<ed-st<<endl; 345 //for(int i = 0; i < n; i++){ 346 // cin>>a[i]; 347 //} 348 //st = clock(); 349 //binSort(a, n, c); 350 //ed = clock(); 351 //cout<<"箱排序:"<<ed-st<<endl; 352 for(int i = 0; i < n; i++){ 353 a[i] = ary[i]; 354 } 355 st = clock(); 356 radixSort(a, n, c); 357 ed = clock(); 358 cout<<"基数排序:"<<ed-st<<endl; 359 } 360 int main(){ 361 for(int i = 0; i < maxn; i++) ary[i] = rand() % mod; 362 while(1){ 363 cout<<"请输入数据规模:\n"; 364 int n; 365 cin>>n; 366 if(n == 0) break; 367 Sort(a, n, c); 368 } 369 return 0; 370 }
哈希
1 /************************************************************************* 2 > File Name: hash.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年12月17日 星期日 18时37分05秒 6 ************************************************************************/ 7 8 #include <bits/stdc++.h> 9 using namespace std; 10 11 const int maxn = 10010; 12 int ary1[maxn], ary2[maxn]; 13 int randarray[maxn]; 14 15 int testcnt; 16 17 template <typename Key, typename Val> 18 class Node{ 19 private: 20 Key key; 21 Val val; 22 public: 23 Node(){} 24 Node(Key key, Val val) : key(key), val(val){} 25 void set(Key tk,Val tv){ 26 key = tk; 27 val = tv; 28 } 29 void setKey(Key k){ 30 key = k; 31 } 32 Key getKey(){ 33 return key; 34 } 35 Val getVal(){ 36 return val; 37 } 38 }; 39 40 template <typename Key, typename Val> 41 class HashTable{ 42 private: 43 Node<Key, Val>* HT; 44 int M; //size of HashTable 45 int cur; 46 Key EMPTY; 47 int p(Key k, int i, int flag){ 48 if(flag == 0) return linear_probing(k, i); 49 if(flag == 1) return quadratic_probing(k, i); 50 return random_probing(k, i); 51 } 52 int linear_probing(Key k, int i){ 53 return i; 54 } 55 //平方探测 56 int quadratic_probing(Key k, int i){ 57 return i * i; 58 } 59 //随机探测 60 int random_probing(Key k, int i){ 61 return randarray[i]; 62 } 63 64 int h(int x){ 65 return x % M; 66 } 67 int h(char* s){ 68 int i = 0, sum = 0; 69 for(; s[i] != '\n'; i++){ 70 sum += (int)s[i]; 71 } 72 return sum % M; 73 } 74 Val hashSearch(Key& k){ 75 int home; 76 int pos = home = h(k); 77 for(int i = 1; k != HT[pos].getKey() && EMPTY != HT[pos].getKey(); i++){ 78 pos = (home + p(k, i)) % M; 79 testcnt++; 80 } 81 if(k == HT[pos].getKey()){ 82 return HT[pos].getVal(); 83 } 84 return EMPTY; 85 } 86 void hashInsert(Key& k, Val& v, int flag){ 87 int home; 88 int pos = home = h(k); 89 for(int i = 1; EMPTY != HT[pos].getKey(); i++){ 90 pos = (home + p(k, i, flag)) % M; 91 testcnt++; 92 } 93 HT[pos].set(k, v); 94 } 95 96 public: 97 HashTable(){} 98 HashTable(int sz, Key k){ 99 init(sz, k); 100 } 101 ~HashTable(){ 102 delete HT; 103 } 104 void init(int sz, Key k){ 105 M = sz; 106 HT = new Node<Key, Val> [sz]; 107 for(int i = 0; i < sz; i++){ 108 HT[i].setKey(EMPTY); 109 } 110 } 111 Val find(Key& k){ 112 testcnt = 0; 113 return hashSearch(k); 114 } 115 int size(){ 116 return cur; 117 } 118 void insert(Key& k, Val& v, int flag){ 119 testcnt = 0; 120 hashInsert(k, v, flag); 121 cur++; 122 } 123 }; 124 HashTable<int, int> hs; 125 126 127 int main(){ 128 for(int i = 0 ; i < maxn; i++) { 129 ary1[i] = rand() % 131313; 130 ary2[i] = rand() % 1313131313 * 131 % 131313; 131 } 132 sort(ary1, ary1 + maxn); 133 int m = unique(ary1, ary1 + maxn) - ary1; 134 int n, slot; 135 cout<<"请输入数据个数以及slot数"<<endl; 136 cin>>n>>slot; 137 int cnt = rand() % 13131313 + 134 ; 138 for(int i = 0; i < slot; i++) randarray[i] = slot - i; 139 for(int i = 0; i < cnt; i++){ 140 next_permutation(randarray, randarray + slot); 141 } 142 for(int i = 0; i < slot; i++) cout<<randarray[i]<< " "; 143 cout<<endl; 144 testcnt = 0; 145 hs.init(slot, 0x3f3f3f3f); 146 cout<<"依次输入元素键值对为:"<<endl; 147 for(int i = 0; i < n; i++){ 148 hs.insert(ary1[i], ary2[i], 0); 149 } 150 cout<<"线性探测碰撞次数:"<<testcnt<<endl; 151 testcnt = 0; 152 hs.init(slot, 0x3f3f3f3f); 153 for(int i = 0; i < n; i++){ 154 hs.insert(ary1[i], ary2[i], 1); 155 } 156 cout<<"平方探测碰撞次数:"<<testcnt<<endl; 157 testcnt = 0; 158 hs.init(slot, 0x3f3f3f3f); 159 for(int i = 0; i < n; i++){ 160 hs.insert(ary1[i], ary2[i], 2); 161 } 162 cout<<"随机探测碰撞次数:"<<testcnt<<endl; 163 }
乱七八糟
1 /************************************************************************* 2 > File Name: base_array_childandbrother.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年11月16日 星期四 21时00分41秒 6 7 8 基于数组的左儿子右兄弟!! 9 ************************************************************************/ 10 #include <bits/stdc++.h> 11 using namespace std; 12 const int maxn = 10010; 13 template <typename T> 14 class Tree{ 15 public: 16 T date[maxn]; 17 int head[maxn], nxt[maxn]; 18 int sz; 19 Tree(){ 20 init(); 21 } 22 void init(){ 23 sz = 0; 24 memset(head, -1, sizeof(head)); 25 memset(nxt, -1, sizeof(nxt)); 26 } 27 void build(int pre, int flag, int u){ 28 T s; 29 if(cin>>s){ 30 if(s == ')'){ 31 return ; 32 }else{ 33 if(flag == 1) head[pre] = u; 34 else if(flag == 0) nxt[pre] = u; 35 date[u] = s; 36 if(head[u] == -1) build(u, 1, ++sz); 37 int id; 38 for(int i = u; ~i; i = nxt[i]){ 39 id = i; 40 } 41 build(id, 0, ++sz); 42 } 43 } 44 } 45 void preOrder(int u){ 46 if(~u){ 47 cout<<date[u]<<" "; 48 preOrder(head[u]); 49 if(head[u]) preOrder(nxt[u]); 50 } 51 } 52 void postOrder(int u){ 53 if(~u){ 54 postOrder(head[u]); 55 cout<<date[u]<<" "; 56 if(head[u]) postOrder(nxt[u]); 57 } 58 } 59 void print(int u){ 60 cout<<"前序遍历:\n"; 61 preOrder(u); 62 cout<<endl; 63 cout<<"后序遍历:\n"; 64 postOrder(u); 65 cout<<endl; 66 } 67 }; 68 int main(){ 69 //输入: 70 //abej))f)g))chk)))di)))) 71 freopen("in.txt", "r", stdin); 72 freopen("out1.txt", "w", stdout); 73 Tree<char> tree; 74 tree.build(0, 2, 0); 75 tree.print(0); 76 //输出: 77 //前序遍历: 78 //a b e j f g c h k d i 79 //后序遍历: 80 //j e f g b k h c i d a 81 }
1 /************************************************************************* 2 > File Name: base_list_childandbrother.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年11月16日 星期四 13时58分57秒 6 7 8 基于链表的左儿子右兄弟!!! 9 ************************************************************************/ 10 #include <bits/stdc++.h> 11 using namespace std; 12 int cnt; 13 const int maxn = 100010; 14 template <typename T> 15 class Node{ 16 private: 17 T date; 18 Node<T> *head, *nxt; 19 public: 20 Node<T>(){ 21 head = NULL; 22 nxt = NULL; 23 } 24 Node<T>(T date, Node<T> *head = NULL, Node<T> *nxt = NULL) : date(date), head(head), nxt(nxt){} 25 void setVal(T x){ 26 date = x; 27 } 28 T getVal(){ 29 return date; 30 } 31 Node<T>* getptrHead(){ 32 return head; 33 } 34 Node<T>*& getHead(){ 35 return head; 36 } 37 void setHead(Node<T> *temp){ 38 head = temp; 39 } 40 Node<T>* getptrNxt(){ 41 return nxt; 42 } 43 Node<T>*& getNxt(){ 44 return nxt; 45 } 46 void setNxt(Node<T> * temp){ 47 nxt = temp; 48 } 49 bool isLeaf(){ 50 return head == NULL; 51 } 52 }; 53 template <typename T> 54 class Tree{ 55 private: 56 Node<T> *rt; 57 public: 58 Tree<T>(){ 59 rt = NULL; 60 } 61 Node<T>* getptrRoot(){ 62 return rt; 63 } 64 Node<T>*& getRoot(){ 65 return rt; 66 } 67 //void build(){ 68 // T u, v; 69 // cin>>u>>v; 70 // Node<T> * q[maxn]; 71 // int front = 0, rear = 0; 72 // while(v != '#'){ 73 // Node<T> *temp = new Node<T>(v); 74 // if(u == '#'){ 75 // rt = temp; 76 // q[(rear++) % maxn] = temp; 77 // }else{ 78 // Node<T> *tp = q[front]; 79 // while(tp->getVal() != u){ 80 // front = (front + 1) % maxn; 81 // tp = q[front]; 82 // } 83 // if(tp->getVal() == u){ 84 // if(tp->getHead() == NULL){ 85 // tp->setHead(temp); 86 // }else{ 87 // tp = tp->getHead(); 88 // while(tp->getNxt()){ 89 // tp = tp->getNxt(); 90 // } 91 // tp->setNxt(temp); 92 // } 93 // } 94 // if(rear + 1 % maxn != front) { 95 // q[(rear++) % maxn] = temp; 96 // } 97 // } 98 // cin>>u>>v; 99 // } 100 //} 101 void buildPreOrder(Node<T> *&cur){ 102 T s; 103 if(cin>>s){ 104 Node<T> *temp = new Node<T>(); 105 if(s == ')'){ 106 cur = NULL; 107 }else{ 108 temp->setVal(s); 109 cur = temp; 110 buildPreOrder(cur->getHead()); 111 buildPreOrder(cur->getNxt()); 112 } 113 } 114 } 115 void preOrder(Node<T> *temp){ 116 if(temp != NULL){ 117 cout<<temp->getVal()<<" "; 118 preOrder(temp->getptrHead()); 119 preOrder(temp->getptrNxt()); 120 } 121 } 122 void postOrder(Node<T> *temp){ 123 if(temp != NULL){ 124 postOrder(temp->getptrHead()); 125 cout<<temp->getVal()<<" "; 126 postOrder(temp->getptrNxt()); 127 } 128 } 129 void levelOrder(){ 130 queue<Node<T> *> q; 131 q.push(rt); 132 while(!q.empty()){ 133 Node<T> *temp = q.front(); 134 cout<<temp->getVal()<<" "; 135 q.pop(); 136 for(Node<T> *it = temp->getptrHead(); it != NULL; it = it->getptrNxt()){ 137 q.push(it); 138 } 139 } 140 cout<<endl; 141 } 142 void print(Node<T> *temp){ 143 cout<<"前序遍历:\n"; 144 preOrder(temp); 145 cout<<endl; 146 cout<<"后序遍历:\n"; 147 postOrder(temp); 148 cout<<endl; 149 cout<<"层次遍历:\n"; 150 levelOrder(); 151 } 152 }; 153 int main(){ 154 freopen("in.txt", "r", stdin); 155 //freopen("out.txt","w", stdout); 156 //输入: 157 //abej))f)g))chk)))di)))) 158 Tree<char> tree; 159 //输入以右括号标记子树的结束,输入前序 160 tree.buildPreOrder(tree.getRoot()); 161 tree.print(tree.getptrRoot()); 162 //输出: 163 //前序遍历: 164 //a b e j f g c h k d i 165 //后序遍历: 166 //j e f g b k h c i d a 167 //层次遍历: 168 //a b c d e f g h i j k 169 }
1 /************************************************************************* 2 > File Name: base_list_childlist1.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年11月16日 星期四 23时12分14秒 6 7 链表方式实现的第一种孩子列表!!!!! 8 ************************************************************************/ 9 10 11 #include <bits/stdc++.h> 12 using namespace std; 13 template <typename T> 14 class Node{ 15 public: 16 T date; 17 Node<T> *par; 18 vector<Node<T> *> child; 19 public: 20 Node(){ 21 par = NULL; 22 child.clear(); 23 } 24 Node(T date){ 25 this->date = date; 26 par = NULL; 27 child.clear(); 28 } 29 void init(){ 30 par = NULL; 31 child.clear(); 32 } 33 T getVal(){ 34 return date; 35 } 36 void setVal(T x){ 37 date = x; 38 } 39 Node<T>* getPar(){ 40 return par; 41 } 42 void setPar(Node<T> *temp){ 43 par = temp; 44 } 45 bool isLeaf(){ 46 return child.size() == 0; 47 } 48 Node<T>*& getHead(){ 49 if(child.size() == 0) { 50 Node<T> *temp = NULL; 51 child.push_back(temp); 52 } 53 return child[0]; 54 } 55 Node<T>*& getNxt(int &pos){ 56 int sz = par->child.size(); 57 for(int i = 0; i < sz; i++){ 58 if(par->child[i]->date == date) { 59 pos = i + 1; 60 if(pos == sz) { 61 Node<T> *temp = NULL; 62 par->child.push_back(temp); 63 } 64 return par->child[i+1]; 65 } 66 } 67 } 68 void setHead(Node<T> *temp){ 69 child.push_back(temp); 70 } 71 void setNxt(Node<T> *temp){ 72 child.push_back(temp); 73 } 74 void removeHead(){ 75 child.erase(child.begin()); 76 } 77 void removeNxt(){ 78 int pos; 79 getNxt(pos); 80 //删除整棵子树 81 par->child.erase(par->child.begin() + pos); 82 } 83 }; 84 85 template <typename T> 86 class Tree{ 87 public: 88 Node<T> *rt; 89 void init(){ 90 //rt->init(); 91 rt = NULL; 92 } 93 Tree(){ 94 init(); 95 } 96 Node<T> *& getRoot(){ 97 return rt; 98 } 99 Node<T>* find(T name, Node<T> *temp){ 100 Node<T> *res = NULL; 101 if(temp != NULL){ 102 if(temp->getVal() == name){ 103 res = temp; 104 }else{ 105 for(int i = 0; i < temp->child.size() && !res; i++){ 106 res = find(name, temp->child[i]); 107 } 108 } 109 } 110 return res; 111 } 112 void build(){ 113 T x; 114 int cnt; 115 while(cin>>x){ 116 cin>>cnt; 117 Node<T> *temp = find(x, rt); 118 if(temp == NULL){ 119 temp = new Node<T>(x); 120 rt = temp; 121 } 122 for(int i = 0; i < cnt; i++){ 123 cin>>x; 124 Node<T> *tp = new Node<T>(x); 125 tp->setPar(temp); 126 temp->child.push_back(tp); 127 } 128 } 129 } 130 //有问题!! 131 //void buildPreOrder(Node<T> *&cur){ 132 // T s; 133 // if(cin>>s){ 134 // cout<<s<<endl; 135 // Node<T> *temp = new Node<T>(); 136 // if(s == ')'){ 137 // cur = NULL; 138 // }else{ 139 // temp->setVal(s); 140 // cur = temp; 141 // buildPreOrder(cur->getHead()); 142 // int pos; 143 // buildPreOrder(cur->getHead()->getNxt(pos)); 144 // } 145 // } 146 // 147 //} 148 void print(){ 149 cout<<"层次遍历:"<<endl; 150 queue<Node<T> *> q; 151 q.push(rt); 152 while(!q.empty()){ 153 Node<T> * temp = q.front(); 154 q.pop(); 155 cout<<temp->date<<" "; 156 for(int i = 0; i < temp->child.size(); i++){ 157 q.push(temp->child[i]); 158 } 159 } 160 cout<<endl; 161 } 162 }; 163 int main(){ 164 // freopen("in.txt", "r", stdin); 165 // freopen("out2.txt", "w", stdout); 166 //输入: 167 //aA 4 g cC z BBbb 168 //z 2 f i 169 //g 1 d 170 //d 3 x e j 171 Tree<string> tree; 172 tree.init(); 173 tree.build(); 174 tree.print(); 175 Node<string> * temp = tree.rt; 176 cout<<"删除根的第一个儿子及子树\n"; 177 temp->removeHead(); 178 tree.print(); 179 cout<<"删除根的第三个儿子及子树\n"; 180 temp = temp->getHead(); 181 temp->removeNxt(); 182 tree.print(); 183 184 //输出: 185 //层次遍历: 186 //aA g cC z BBbb d f i x e j 187 //删除根的第一个儿子及子树 188 //层次遍历: 189 //aA cC z BBbb f i 190 //删除根的第三个儿子及子树 191 //层次遍历: 192 //aA cC BBbb 193 }
1 /************************************************************************* 2 > File Name: base_list_childlist2.cpp 3 > Author: yijiull 4 > Mail: 1147161372@qq.com 5 > Created Time: 2017年11月17日 星期五 19时24分04秒 6 7 链表方式实现的第二种孩子列表!!!!! 8 ************************************************************************/ 9 10 #include <bits/stdc++.h> 11 using namespace std; 12 template <typename T> class Node_P; 13 template <typename T> 14 class Node_E{ 15 public: 16 T date; 17 Node_E<T> *par; 18 Node_P<T> *head; 19 Node_E(){} 20 Node_E(T date) : date(date){ 21 par = NULL; 22 head = NULL; 23 } 24 T getVal(){ 25 return date; 26 } 27 void setPar(Node_E<T> *temp){ 28 par = temp; 29 } 30 void setHead(Node_P<T> *temp){ 31 head = temp; 32 } 33 Node_P<T>* getRightSon(){ 34 Node_P<T> *cur = par->head; 35 cout<<"getson "<<cur->node->date<<endl; 36 while(cur->nxt) cur = cur->nxt; 37 return cur; 38 } 39 }; 40 template <typename T> 41 class Node_P{ 42 public: 43 Node_E<T> *node; 44 Node_P<T> *nxt; 45 Node_P(Node_E<T> *node = NULL, Node_P<T> *nxt = NULL) : node(node), nxt(nxt){} 46 void setNxt(Node_P<T> *temp){ 47 nxt = temp; 48 } 49 }; 50 51 template <typename T> 52 class Tree{ 53 public: 54 Node_E<T> *rt; 55 Tree(){ 56 rt = NULL; 57 } 58 Node_E<T>* find(T name, Node_E<T> *temp){ 59 Node_E<T> *res = NULL; 60 if(temp != NULL){ 61 if(temp->date == name){ 62 res = temp; 63 }else{ 64 Node_P<T> *it = temp->head; 65 while(!res && it){ 66 res = find(name, it->node); 67 it = it->nxt; 68 } 69 } 70 } 71 return res; 72 } 73 void build(){ 74 T x; 75 while(cin>>x){ 76 int cnt; 77 cin>>cnt; 78 Node_E<T> *temp = find(x, rt); 79 if(temp == NULL){ 80 temp = new Node_E<T>(x); 81 rt = temp; 82 } 83 for(int i = 0; i < cnt; i++){ 84 cin>>x; 85 Node_E<T> *tp = new Node_E<T>(x); 86 tp->setPar(temp); 87 if(temp->head == NULL){ 88 Node_P<T> *t1 = new Node_P<T>(tp); 89 temp->setHead(t1); 90 }else{ 91 Node_P<T> *t1 = temp->head; 92 while(t1->nxt) t1 = t1->nxt; 93 Node_P<T> *t2 = new Node_P<T>(tp); 94 t1->setNxt(t2); 95 } 96 } 97 } 98 } 99 void print(){ 100 cout<<"层次遍历:\n"; 101 queue<Node_E<T>*> q; 102 q.push(rt); 103 while(!q.empty()){ 104 Node_E<T> *temp = q.front(); 105 q.pop(); 106 cout<<temp->date<<" "; 107 int z = 0; 108 for(Node_P<T> *it = temp->head; it != NULL; it = it->nxt){ 109 q.push(it->node); 110 } 111 } 112 } 113 }; 114 int main(){ 115 freopen("in.txt", "r", stdin); 116 freopen("out2.txt", "w", stdout); 117 //输入: 118 //aA 4 g cC z BBbb 119 //z 2 f i 120 //g 1 d 121 //d 3 x e j 122 Tree<string> tree; 123 tree.build(); 124 tree.print(); 125 //输出: 126 //层次遍历: 127 //aA g cC z BBbb d f i x e j 128 }