2012-11-16 132 views

回答

0

据推测,您的链表具有典型的链表操作。这些包括获取引用第一个元素的迭代器,递增迭代器以引用下一个元素,检查迭代器是否已经跑出列表的末尾等等。算法如下:

  1. 设置一个迭代器来引用链表中的第一个元素。

  2. 如果迭代器已经跑出链表的末尾,请停止。

  3. 打印迭代器引用的元素。

  4. 递增迭代器。

  5. 转到步骤2.

如果你不知道该怎么做这些具体步骤,那么你不知道如何使用您有特别的链接类。为了帮助你,我们需要看到它的代码(如果它是一个现成的类,那么它的链接就是它的文档)。

一个典型的C++实现会是这个样子:

void LinkedList::print(ostream& stream) const 
{ 
     LinkedListElement* ptr = head; // this is my step 1 
     while (ptr != NULL)   // this is my step 2 
     { 
      stream << *ptr;   // this is my step 3 
      ptr = ptr->getNext();  // this is my step 4 
     } // step 5 happens here because this is a loop 
} 
0

您可以使用此:

void print(node* n) { 
    cout << n -> value << endl; 
    if(n -> next) print(n -> next); 
} 

,并调用它像这样:

int main() { 
    linked_list l; 
    ... 
    print(l -> head); 
    return 0; 
} 
+0

好的。一个问题是函数在.h文件中声明了某种特定的方式,我无法编辑,所以我到目前为止是:void Set :: display(ostream&Out)const {/*...*/} – IrfanM

+0

这是一个什么问题? –

+0

@DavidSchwartz我不知道如何将其转换为具有类型为ostream的参数/参数的函数。 – IrfanM

0

希望这有助于!

struct Node 
    { 
     int data; 
     struct Node *next; 
    } 

void Print(Node *head) 
{ 
    Node *a =head; 
    while(a!=NULL){ 
     cout<<a->data<<endl; 
     a = a->next; 
    } 
}