c++ - 单链链打印C++

标签 c++ linked-list

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

任何指导将不胜感激!

设置.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;

};

最佳答案

这里有两个建议:1)先对列表进行排序,然后打印所有节点; 2)为数据创建另一个列表(索引)并对这些链接进行排序(不需要那些节点中的数据)。

先排序列表

一种常用的技术是按照您希望打印的顺序对节点进行排序。这应该涉及更改链接字段。
接下来,从头节点开始打印列表中的每个节点(或列表中每个节点的数据)。

使用索引列表

创建另一个没有数据字段的链表。此列表中的链接指向原始列表中的数据字段。按照您希望打印节点的顺序对新列表进行排序。
该技术保留了第一个列表的创建顺序并允许不同的排序方案。

更改链接

由于您正在编写自己的链接列表,因此链接的更改留作练习,因为编写您的代码不会给我报酬。 SO以及网络上有很多排序和遍历链表的例子。

关于c++ - 单链链打印C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13390449/

相关文章:

c# - 从 Windows Phone 8.1 运行时组件访问 CoreWindow

C++ 将 NULL 和 NON-NULL 值转换为指针

c - 如何将数字插入到 C 中的二叉搜索树中?

c++ - VC++中Float数据类型的值,如何判断是否混合了字符串或特殊字符

c++ - 如何在 Windows XP 中获取快速启动栏的大小?

c++ - 在Windows上使用MinGW构建mysql-connector-c++-8.0.17-src

java - 混淆Java链表问题的值

c - 使用 C 对链表进行排序

c - 递归打印链表时出现段错误

java - 使用基于数组的链表实现ADT列表