C++ 调用在另一个对象中定义的链表对象的函数
好吧,我有两门课,我在这里学习 队列对象和堆栈对象 队列实际上是一个链表,由一个头节点和下一个节点组成C++ 调用在另一个对象中定义的链表对象的函数,c++,object,linked-list,stack,queue,C++,Object,Linked List,Stack,Queue,好吧,我有两门课,我在这里学习 队列对象和堆栈对象 队列实际上是一个链表,由一个头节点和下一个节点组成 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; 像这样 现在,
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();
friend ostream&operator正如OP所建议的,这可以通过从堆栈构造函数调用队列构造函数来实现,如下所示:
Stack<T> :: Stack() { queue = Queue<T>(); }
Stack::Stack(){queue=queue();}
您可以通过使用std::stack
、std::deque
和std::list
简化您的生活。它们是标准的,已经编写并测试过了!解决:我所要做的就是从堆栈构造函数调用队列构造函数,如下所示。。。堆栈::堆栈(){queue=queue();}托马斯·马修斯。。。我不允许:)记得把你的解决方案作为答案发布,然后接受它。
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<T> :: Stack() { queue = Queue<T>(); }