【发布时间】:2014-12-28 20:23:12
【问题描述】:
我在理解这个功能如何工作以及我需要做什么方面遇到了一些麻烦。我有 int number 作为我的数据类型和 node* next 在我的节点类中。我也有节点指针头、电流和温度。我的问题是我将如何让我的整数列表有序?另外,升序和降序如何在单个链表中工作?
我的头文件:
#ifndef SDI_LL
#define SDI_LL
namespace SDI
{
class LinkedList
{
class Node
{
public:
int number; //data element
Node* next; //pointer to next, node inside each node
private:
};
private:
Node *head;
Node *current; //head, current and temp node pointers
Node *temp;
public:
LinkedList(); //constructor to access nodes from above
~LinkedList(); //destructor
void insert(int add);
void remove(int remove); //functions that access private data nodes above
void display();
void reverse();
void search(int searchNum);
void sortAscending();
void sortDecending();
void saveAll();
void restoreAll();
};
}
#endif
到目前为止,我的升序函数从头开始并在列表中搜索:
void LinkedList::sortAscending()
{
current = head;
for (current = head; current;)
{
temp = current;
current = current->next;
}
}
【问题讨论】:
-
对于内部列表,例如std::list,有一个特殊的列表排序,std::list::sort,但是节点是在类中维护的,类函数复制内部节点和某些用户指定类型(如结构)之间的节点数据,不包括链接。但是在这种情况下,您正在创建自己的节点类和函数(例如您自己的排序),因此标准函数将无济于事。
-
@Ryan - 对于这种列表排序,您是要交换节点中的数据还是交换节点?
-
“搁置” - 原始发帖人计划稍后使用排序功能的实际示例代码更新他的问题。答案之一包括交换数据的示例代码。不知道原贴是打算交换数据还是交换节点。
-
@MSalters:是的。我期待一个专业。删除了错误的评论。
标签: c++ sorting linked-list