【问题标题】:Calling function of an linked list object defined inside another object调用另一个对象内部定义的链表对象的函数
【发布时间】: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::stackstd::dequestd::list 来简化您的生活。它们是标准的、已经编写好的并且已经过测试!
  • 已解决:我所要做的就是像这样从 Stack 构造函数调用 Queue 构造函数 ... Stack :: Stack() { queue = Queue(); }
  • Thomas Matthews... 我不被允许 :)
  • 请记住将您的解决方案作为答案发布并接受它。

标签: c++ object linked-list stack queue


【解决方案1】:

正如 OP 所建议的,这可以通过从 Stack 构造函数中调用 Queue 构造函数来实现,如下所示:

Stack<T> :: Stack() { queue = Queue<T>(); } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2015-06-16
    • 2018-09-11
    • 2021-03-11
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多