单链打印 C++ [英] singly linked chain printing c++

查看:20
本文介绍了单链打印 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)
}

这篇关于单链打印 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆