#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
template<typename T>
class CQueue
{
public:
CQueue(){};
~CQueue(){};
void push_back(T data);
T pop_front();
bool empty();
public:
CQueue(T *pdata, int len);
private:
stack<T>stack1;
stack<T>stack2;
};
template<typename T>
CQueue<T>::CQueue(T*pdata, int len)
{
if(!(stack1.empty()))
{
cout<<"heihei"<<endl;
return;
}
else
{
for (int i = 0 ; i< len; i++)
{
stack1.push(pdata[i]);
}
}
}
template<typename T>
void CQueue<T>::push_back(T data)
{
stack1.push(data);
}
template<typename T>
T CQueue<T>::pop_front()
{
if(stack2.empty())
{
while(!(stack1.empty()))
{
stack2.push(stack1.top());
stack1.pop();
}
T data = stack2.top();
stack2.pop();
return data;
}
else
{
T data = stack2.top();
stack2.pop();
return data;
}
return -1;
}
template<typename T>
bool CQueue<T>::empty()
{
if(stack1.empty() && stack2.empty())
return true;
else
return false;
}
void main()
{
fstream file1;
file1.open("./data/treedata.txt", ios::binary|ios::in);
int num;
file1>>num;
int *pdata = new int[num];
for(int i = 0;i < num; i++)
{
file1>>pdata[i];
cout<<pdata[i]<<endl;
}
CQueue<int> myQueue(pdata, num);
while(!myQueue.empty())
{
int data = myQueue.pop_front();
cout<<data<<endl;
}
cout<<"hahaaaaa"<<endl;
for (int i = 100; i < 110; i++)
{
myQueue.push_back(i);
int data = myQueue.pop_front();
cout<<data<<endl;
}
}
双栈实现队列
最新推荐文章于 2024-07-10 22:06:07 发布