我正在尝试以 {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/