数据结构之栈和队列
1.栈
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 const int stack_size = 10; 5 const int ex_size = 1; 6 typedef struct{ 7 int *base, *top; 8 int size; 9 }stack; 10 void init(stack &s){ 11 s.base = s.top = new int[stack_size]; 12 s.size = stack_size; 13 } 14 int length(stack &s){ 15 return s.top - s.base; 16 } 17 void ex_stack(stack &s){ 18 s.size += ex_size; 19 int *p = s.base, *t = new int[s.size]; 20 int len = length(s); s.base = t; 21 for(int i = 0; i < len; ++i) *t++ = p[i]; 22 s.top = t; 23 delete[] p; 24 } 25 void destroy(stack &s){ 26 delete[] s.base; 27 s.base = s.top = NULL; 28 s.size = -1; 29 } 30 void clear(stack &s){ 31 s.top = s.base; 32 } 33 bool empty(stack &s){ 34 if(s.top == s.base) return true; 35 return false; 36 } 37 void push(stack &s, int x){ 38 if(length(s) == s.size) ex_stack(s); 39 *s.top++ = x; 40 } 41 void pop(stack &s){ 42 if(empty(s)) return; 43 --s.top; 44 } 45 int *get(stack &s){ 46 if(empty(s)) return NULL; 47 return s.top-1; 48 } 49 void display(stack &s){ 50 if(empty(s)) cout << "stack is empty!"; 51 else for(int *p = s.base; p != s.top; ++p) cout << *p << " "; 52 cout << endl; 53 } 54 int main(){ 55 stack s; init(s); 56 for(int i = 0; i < 10; ++i) push(s,i); 57 display(s); push(s,10); display(s); 58 pop(s); display(s); cout << *get(s) << endl; 59 clear(s); cout << "len = " << length(s) << endl; 60 destroy(s); if(s.size == -1 && !s.base && !s.top) cout << "Successful destruction." << endl; 61 return 0; 62 }
2.队列
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 typedef struct Node{ 5 int x; 6 struct Node *next; 7 }node; 8 typedef struct{ 9 node *front, *rear; 10 int length; 11 }queue; 12 void init(queue &q){ 13 node *t = new node; 14 t->x = 0; 15 t->next = NULL; 16 q.front = q.rear = t; 17 q.length = 0; 18 } 19 void del(queue &q, node *h){ 20 if(h == NULL) return; 21 del(q,h->next); 22 delete h; 23 h = NULL; 24 } 25 void destroy(queue &q){ 26 del(q,q.front); 27 q.front = q.rear = NULL; 28 q.length = -1; 29 } 30 void clear(queue &q){ 31 q.rear = q.front; 32 q.length = 0; 33 } 34 bool empty(queue &q){ 35 if(q.length) return false; 36 return true; 37 } 38 int *get(queue &q){ 39 if(empty(q)) return NULL; 40 return &(q.front->x); 41 } 42 void push(queue &q, int x){ 43 q.rear->x = x; 44 ++q.length; 45 if(q.rear->next == NULL){ 46 node *t = new node; 47 t->x = 0; t->next = NULL; 48 q.rear->next = t; 49 } 50 q.rear = q.rear->next; 51 } 52 void pop(queue &q){ 53 node *p = q.front->next; 54 delete q.front; 55 q.front = p; 56 --q.length; 57 } 58 void display(queue &q){ 59 if(empty(q)) cout << "Queue is empty!"; 60 else for(node *p = q.front; p != q.rear; p = p->next) cout << p->x << " "; 61 cout << endl; 62 } 63 int main(){ 64 queue q; init(q); 65 for(int i = 0; i < 10; ++i) push(q,i); 66 display(q); cout << *get(q) << endl; 67 pop(q); display(q); 68 clear(q); cout << q.length << endl; 69 destroy(q); if(q.length == -1 && !q.front && !q.rear) cout << "Successful destruction." << endl; 70 return 0; 71 }
3.循环队列
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 const int maxn = 10;//仅能装下maxn - 1个元素。 5 typedef struct{ 6 int *base; 7 int front, rear; 8 }squeue; 9 void init(squeue &q, int max_len){ 10 int *p = new int[max_len]; 11 if(!p) return; 12 q.base = p; 13 q.front = q.rear = 0; 14 } 15 int length(squeue &q){ 16 return (q.rear - q.front + maxn) % maxn; 17 } 18 bool empty(squeue &q){ 19 if(q.rear == q.front) return true; 20 return false; 21 } 22 bool full(squeue &q){ 23 if((q.rear + 1) % maxn == q.front) return true; 24 return false; 25 } 26 void push(squeue &q, int x){ 27 if(full(q)) return; 28 q.base[q.rear] = x; 29 q.rear = (q.rear + 1) % maxn; 30 } 31 void pop(squeue &q){ 32 if(empty(q)) return; 33 q.front = (q.front + 1) % maxn; 34 } 35 int get(squeue &q){ 36 return q.base[q.front]; 37 } 38 void next(int &i){ 39 ++i; 40 if(i == maxn) i = 0; 41 } 42 void display(squeue &q){ 43 if(empty(q)) return; 44 for(int i = q.front; i != q.rear; next(i)) cout << q.base[i] << " "; 45 cout << endl; 46 } 47 int main(){ 48 squeue q; init(q,maxn); 49 for(int i = 0; i < 9; ++i) push(q,i); 50 display(q); pop(q); pop(q); push(q,9); 51 display(q); 52 }