代码实现
#include <iostream>#include <queue>using namespace std;template <typename T>class My_queues{private://队列数据存储空间T *data;//头下标int head;//尾下标int tail;int max_len;public://无参构造函数My_queues():data(NULL),head(0),tail(0),max_len(1){}//拷贝构造My_queues(My_queues<T> &other):data(new T[other.max_len+1]),head(other.head),tail(other.tail),max_len(other.max_len){memcpy(data, other.data, sizeof(T)*other.max_len);}//拷贝赋值函数My_queues &operator=(const My_queues<T> &other){if(this != &other){delete []data;data = new T[other.max_len+1];memcpy(data, other.data, sizeof(T)*other.max_len);head = other.head;tail = other.tail;max_len = other.max_len;}return *this;}//析构函数~My_queues(){if(NULL != data){delete []data;data = NULL;}}//empty如果为空返回真bool empty(){if(NULL == data || head == tail){return true;}else{return false;}}//back返回最后一个元素T &back(){return data[(tail-1+max_len)%max_len];}//判满bool full(){if(head == (tail+1)%(max_len+1)){return true;}else{return false;}}//将两个指针所指向空间的内容赋值//front返回第一个元素T &front(){return data[head];}//pop删除队列中的一个元素void pop(){if(empty()){cout << "队列为空,删除失败。" <<endl;}else{data[head] = (T)0;head = (head+1)%(max_len+1);}}//push增加一个元素void push(const T &val){if(full()){//建立临时指针接一下空间,将原先的内容拷贝到新空间中T *temp = new T[2*max_len+1];memcpy(temp, data, sizeof(T)*max_len);//释放原先的空间delete []data;//将数据指针指向扩容后的空间data = temp;//增加元素max_len *= 2;*(data+tail) = val;tail = (tail+1)%(max_len+1);}else if(NULL == data){data = new T[1];max_len = 1;*(data+tail) = val;tail = (tail+1)%(max_len+1);}else{*(data+tail) = val;tail = (tail+1)%(max_len+1);}}//判断队列中的元素个数int size(){if(tail > head){return tail -head;}else{return tail-head+max_len;}}};template <typename T>class My_Stacks{private://队列数据存储空间T *data;//头下标int top;//最大元素int max_len;public://无参构造函数My_Stacks():data(NULL),top(-1),max_len(1){}//拷贝构造My_Stacks(My_Stacks<T> &other):data(new T[other.max_len+1]),top(other.top),max_len(other.max_len){memcpy(data, other.data, sizeof(T)*other.max_len);}//拷贝赋值函数My_Stacks &operator=(const My_Stacks<T> &other){if(this != &other){delete []data;data = new T[other.max_len];memcpy(data, other.data, sizeof(T)*(top+1));top = other.top;max_len = other.max_len;}return *this;}//析构函数~My_Stacks(){if(NULL != data){delete []data;data = NULL;}}//empty如果为空返回真bool empty(){if(NULL == data || top == -1){return true;}else{return false;}}//判满bool full(){if(top == (max_len-1)){return true;}else{return false;}}//front返回栈顶元素T &front(){if(top < 0){cout<< "栈为空" <<endl;return data[0];}return data[top];}//pop从栈顶删除元素void pop(){if(empty()){cout << "栈为空,删除失败。" <<endl;}else{data[top] = (T)0;top = top-1;}}//push压栈增加元素void push(const T &val){if(full()){//建立临时指针接一下空间,将原先的内容拷贝到新空间中T *temp = new T[2*max_len+1];memcpy(temp, data, sizeof(T)*max_len);//释放原先的空间delete []data;//将数据指针指向扩容后的空间data = temp;//增加元素max_len *= 2;data[++top] = val;}else if(NULL == data){data = new T[1];max_len = 1;data[++top] = val;}else{data[++top] = val;}}//判断队列中的元素个数int size(){if(top<0){return 0;}else{return top+1;}}};int main(){cout<< "******************FIFO******************" <<endl;My_queues<int> q1;cout << "my_empty() \t= " << boolalpha << q1.empty() << endl;q1.push(10);cout << "my_front() \t= " << q1.front() << endl;cout << "my_size() \t= " << boolalpha << q1.size() << endl;q1.push(100);cout << "my_size() \t= " << boolalpha << q1.size() << endl;cout << "my_front() \t= " << q1.front() << endl;cout << "my_back() \t= " << q1.back() << endl;My_queues<int> q3(q1);cout << "拷贝构造后的元素个数 \t= " << q3.size() << endl;cout << "my_front() \t= " << q1.front() << endl;cout << "my_back() \t= " << q1.back() << endl;My_queues<int> q4 = q1;cout << "拷贝赋值后的元素个数 \t= " << q4.size() << endl;cout << "my_front() \t= " << q1.front() << endl;cout << "my_back() \t= " << q1.back() << endl;q1.pop();cout << "pop,my_size() \t= " << boolalpha << q1.size() << endl;cout << "my_front() \t= " << q1.front() << endl;cout << "my_back() \t= " << q1.back() << endl;/**********************************************************************************************/cout<< "******************LIFO******************" <<endl;My_Stacks<int> s1;cout << "s1.empty() \t= " << s1.empty() << endl;s1.push(100);cout << "s1.front() \t= " << s1.front() << endl;My_Stacks<int> s2(s1);cout << "s2.empty() \t= " << s2.empty() << endl;cout << "s2.front() \t= " << s2.front() << endl;My_Stacks<int> s3 = s1;cout << "s3.empty() \t= " << s2.empty() << endl;cout << "s3.front() \t= " << s2.front() << endl;s1.push(1000);cout << "s1.size() \t= " << s1.size() << endl;cout << "s1.front() \t= " << s1.front() << endl;s1.pop();cout << "s1.size() \t= " << s1.size() << endl;cout << "s1.front() \t= " << s1.front() << endl;s1.pop();cout << "s1.size() \t= " << s1.size() << endl;cout << "s1.front() \t= " << s1.front() << endl;return 0;}