c++ queue 顺序队列的实现
2016-03-13 17:47
846 查看
#include<iostream> #include<cstdlib> #include<cstdio> using namespace std; const int initial_lize=10000; const int adding_size=2*initial_lize; template<class T> struct Queue{ T * base; T * Qhead,*Qbegin,*Qend,*Qfront,*Qtail; int front_pos,tail_pos; int now_size,whole_size; Queue(){ base=(T *)malloc(sizeof(T)*initial_lize); Qhead=base; Qbegin=base;Qend=base+(initial_lize-1); Qfront=Qtail=base+1; front_pos=tail_pos=1; now_size=1; whole_size=initial_lize; } bool Empty(){ if(Qfront==Qtail) return true; else return false; } void push(const T x){ now_size++; if(now_size<whole_size){ *Qtail=x; Qtail++; tail_pos++; } else{ base=(T *)realloc(base,(sizeof(T))*whole_size*2);//别忘了类型转换 whole_size*=2; Qhead=base;Qend=base+(whole_size-1); Qfront=base+front_pos;Qtail=base+tail_pos; *Qtail=x;Qtail++;tail_pos++; } } T Top(){ if(!Empty()) return (*Qfront); else{ cerr<<"Queue is empty! can not return any elements"<<endl; } } void pop(){ if(!Empty()){ Qfront++; front_pos++; } else{ cerr<<"Queue is empty! can not pop any elements"<<endl; } } void delete_Queue(){ free(base); } }; int main(){ //队列的测试 // Queue<int> que; // int temp=0; // que.Top(); // que.pop(); // for(int i=0;i<10;i++){ // que.push(i); // } // for(int i=0;i<10;i++){ // temp=que.Top(); // que.pop(); // cout<<temp<<endl; // } // que.delet_Queue(); return 0; }
相关文章推荐
- C++设计模式——Composite 组合模式
- programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive
- C++中const的问题(刷题后感)
- C++_动态存储空间的分配和释放
- leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间
- C++第一次上机实验-1-2
- Effective C++ 条款18
- 俄罗斯方块的c语言基本实现
- 详解C++中如何将构造函数或析构函数的访问权限定为private
- c++第2次实验(循环)
- VC++制作DLL具体解释
- C++中引用的详解
- 2012年第三届蓝桥杯试题(C/C++本科B组)
- stl之空间配置器Allocator
- 类的对象和类的指针的区别
- C++的静态联编和动态联编
- 2048的c语言实现
- 随机函数rand()和srand()(c++)
- 金磊c++
- C++上机报告(2)