C++编程题,求大佬解答。。。谢谢

2024-12-16 15:34:33
推荐回答(3个)
回答1:

#include
#include
template
class Sequence
{
public:
virtual void push(T x) = 0;
virtual void pop() = 0;
};
//栈
template
class Stack: public Sequence
{
public:
void push(T x){deque.push_back(x);}
void pop(){deque.pop_back();}
private:
std::deque deque;
};
//队列
template
class Queue:public Sequence
{
public:
void push(T x) { deque.push_back(x);}
void pop() {deque.pop_front();}
private:
std::deque deque;
};
int main()
{
Sequence* s1 = new Stack;
s1->push(1);
s1->pop();

Sequence* s2 = new Queue;
s2->push(1);
s2->pop();

delete s1;
delete s2;
}

回答2:

了解一下C++中的template,可能对你有帮助

回答3:

乘承揽百费毁坏好普智取角