【发布时间】:2013-02-09 22:47:29
【问题描述】:
好吧,我在这里有两个课程。
队列对象和堆栈对象。
队列实际上是一个由头节点和下一个节点组成的链表。
class Node
{
public:
Node(const T& data, Node* n = 0)
{
element = data;
next = n;
}
T element;
Node* next;
};
/*The head of the queue*/
Node* head;
像这样......
现在 Queue 有了我已经实现的功能
friend ostream& operator<< <T>(ostream&,Queue<T>&);
/*The default constructor*/
Queue();
/*The copy constructor*/
Queue(const Queue<T>& other);
Queue<T>& operator=(const Queue<T>& other);
/*The destructor*/
~Queue();
void enqueue(const T& el);
T dequeue();
void increasePriority(const T& el);
bool isEmpty();
他们都工作......
所以我进入了 Stack 类
Queue<T>* queue;
这就是堆栈的定义...
问题在于使用 Stack 对象调用这些函数
friend ostream& operator<< <T>(ostream&,Stack<T>&);
/*The constructor for the Stack class*/
Stack();
/*The copy constructor*/
Stack(const Stack<T>& other);
Stack<T>& operator=(const Stack<T>& other);
~Stack();
void push(const T& el);
T pop();
T peek();
bool isEmpty();
我将如何实现这些函数以便它们使用队列对象函数?
换句话说。 Stack类的构造函数必须调用Queue类的构造函数等...
【问题讨论】:
-
您可以使用
std::stack、std::deque和std::list来简化您的生活。它们是标准的、已经编写好的并且已经过测试! -
已解决:我所要做的就是像这样从 Stack 构造函数调用 Queue 构造函数 ... Stack
:: Stack() { queue = Queue (); } -
Thomas Matthews... 我不被允许 :)
-
请记住将您的解决方案作为答案发布并接受它。
标签: c++ object linked-list stack queue