//节点的结构 template<typename T> struct node { T data; node<T>* next; node():next(nullptr){}; node(T t):data(t),next(nullptr){}; } //模板类构造队列类 template<typename T> Class Myqueue { public: Myqueue():head(nullptr),tail(nullptr),count(0) { head=new node<T>(); tail=head; count=0; } void enqueue(T &x); T front(); void dequeue(); int counts(); bool empty(); ~Myqueue() { while(head->next!=nullptr) { node<T>* p=head; head=head->next; } } private: int count; node<T>* head; node<T>* tail; } template<typename T> void Myqueue<T>::enqueue(T &x) { node<T>* p= new node<T>(x);//申请空间 head->next=p; head=p; count++; } template<typename T> bool Myqueue<T>::empty() { return count==0; } template<typename T> T MyStack<T>::front() { if(!empty()) return head->next->dada; } template<typename T> void MyStack<T>::pop() { if(!empty()) { node<T>* del=head->next; head->next=haed->next->next; delete del; count--; } } template<typename T> int MyStack<T>::counts() { return count; }
时间: 2024-10-19 01:26:04