C++编程题,求大佬解答。。。谢谢
定义一个线性表类,从他派生出队列类和栈类,其中队列类能实现数据的FIFO操作,栈类能实现数据的FILO操作。要求他们都能处理整数、实数、字符和复数。...
定义一个线性表类,从他派生出队列类和栈类,其中队列类能实现数据的FIFO操作,栈类能实现数据的FILO操作。要求他们都能处理整数、实数、字符和复数。
展开
3个回答
展开全部
#include <iostream>
#include <deque>
template<typename T>
class Sequence
{
public:
virtual void push(T x) = 0;
virtual void pop() = 0;
};
//栈
template<typename T>
class Stack: public Sequence<T>
{
public:
void push(T x){deque.push_back(x);}
void pop(){deque.pop_back();}
private:
std::deque<T> deque;
};
//队列
template<typename T>
class Queue:public Sequence<T>
{
public:
void push(T x) { deque.push_back(x);}
void pop() {deque.pop_front();}
private:
std::deque<T> deque;
};
int main()
{
Sequence<int>* s1 = new Stack<int>;
s1->push(1);
s1->pop();
Sequence<int>* s2 = new Queue<int>;
s2->push(1);
s2->pop();
delete s1;
delete s2;
}
#include <deque>
template<typename T>
class Sequence
{
public:
virtual void push(T x) = 0;
virtual void pop() = 0;
};
//栈
template<typename T>
class Stack: public Sequence<T>
{
public:
void push(T x){deque.push_back(x);}
void pop(){deque.pop_back();}
private:
std::deque<T> deque;
};
//队列
template<typename T>
class Queue:public Sequence<T>
{
public:
void push(T x) { deque.push_back(x);}
void pop() {deque.pop_front();}
private:
std::deque<T> deque;
};
int main()
{
Sequence<int>* s1 = new Stack<int>;
s1->push(1);
s1->pop();
Sequence<int>* s2 = new Queue<int>;
s2->push(1);
s2->pop();
delete s1;
delete s2;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询