单链打印 C++

2022-01-01 00:00:00 linked-list c++

我正在尝试以 {1,2,3,4,etc} 格式选择我的链.您可以在下面找到包含节点布局的头文件.我只是对如何循环遍历我的列表以打印出 Item 感到困惑.

I am trying to pick my chain in the format {1,2,3,4,etc}. You can find the header file below which will have the layout of the nodes. I am just confused on how I should go about cycling through my list to print out Item.

任何指导将不胜感激!

set.h

using namespace std;

#include <iostream>

class Set
{
  private:

    struct Node
    {
      int Item;      // User data item
      Node * Succ;   // Link to the node's successor
    };

    unsigned Num;    // Current count of items in the set
    Node * Head;     // Link to the head of the chain

  public:

    // Return information about the set
    //
    bool is_empty() const { return Num == 0; }
    unsigned size() const { return Num; }

    // Initialize the set to empty
    //
    Set();

    // Insert a specified item into the set, if possible
    //
    bool insert( int );

    // Display the set
    //
    void display( ostream& ) const;

};

推荐答案

假设你的列表是循环的,你可以这样使用:

Suppose your list is cyclical, you can use this:

struct Node *n = begin;

if (n != NULL) {
    //do something on it
    ...
    for (n = begin->Succ; n != begin; n = n->Succ) {
    }
}

struct Node *n = begin;
if (n != NULL) {        
    do {        
        //do something
        ...
        n = n->Succ;
    } while (n != begin)
}

相关文章