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

查看:140
本文介绍了单链链印花c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以格式{1,2,3,4,etc}选择我的链。你可以找到下面的头文件,它将有节点的布局。我只是困惑,如何我应该骑自行车通过我的列表打印出项目。

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天全站免登陆