#pragma once
#include<stack>
template<class T>
class CMyQueue
{
std::stack<T> m_stack1;
std::stack<T> m_stack2;
public:
void push(const T& elem);
T pop();
CMyQueue(void);
~CMyQueue(void);
};
#include"MyQueue.cpp"
MyQueue.cpp
#include "MyQueue.h"
template<class T>
CMyQueue<T>::CMyQueue(void)
{
}
template<class T>
CMyQueue<T>::~CMyQueue(void)
{
}
template<class T>
void CMyQueue<T>::push(const T& elem)
{
m_stack1.push(elem);
}
template<class T>
T CMyQueue<T>::pop()
{
if(m_stack2.empty())
{
while(!m_stack1.empty())
{
T val=m_stack1.top();
m_stack1.pop();
m_stack2.push(val);
}
}
try
{
if(m_stack2.empty())
throw exception("out of range");
T val=m_stack2.top();
m_stack2.pop();
return val;
}
catch(exception e)
{
std::cout<<e.what();
exit(-1);
}
}
No comments:
Post a Comment