我正在尝试以 {1,2,3,4,etc} 格式选择我的链。您可以在下面找到头文件,其中包含节点的布局。我只是对如何循环浏览列表以打印项目感到困惑。
任何指导将不胜感激!
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;
};
这里有两个建议:1)先对列表进行排序,然后打印所有节点; 2)为数据创建另一个列表(索引)并对这些链接进行排序(不需要这些节点中的数据)。
首先对列表进行排序
一种常用的技术是按照您希望打印的顺序对节点进行排序。这应该涉及更改链接字段。
接下来,从头节点开始,打印列表中的每个节点(或列表中每个节点的数据)。
使用索引列表
创建另一个不带数据字段的链表。该列表中的链接指向原始列表中的数据字段。按照您希望打印节点的顺序对新列表进行排序。
该技术保留了第一个列表的创建顺序并允许不同的排序方案。
更改链接
由于您正在编写自己的链接列表,因此链接的更改将作为练习,因为我不会获得编写您的代码的报酬。 SO 以及网络上有很多用于排序和遍历链表的示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)