#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; }
时间: 2024-10-20 17:33:56