单链表上的归并排序 C++
我正在寻找某种简单的方法来学习和理解这些合并排序。我在网上查看过,发现合并排序对于单链表确实很好,但我不明白如何做到这一点。这是我找到的网站: 维基百科合并排序 和 具体链接列表
我不确定要给你什么代码。我基本上只是在我的头文件中添加了这个,并且对此很陌生,所以我非常基础。提前感谢您的帮助:)
class Node
{
public:
int data;
Node* next;
Node()
{
next = NULL;
data = 0;
}
};
class SLLIntStorage
{
public:
Node* head;
Node* current;
Node* tail;
void Read(istream&);
void Write(ostream&);
void setReadSort(bool);
void sortOwn();
void print();
bool _sortRead;
int numberOfInts;
SLLIntStorage(const SLLIntStorage& copying)
{
}
SLLIntStorage(void);
~SLLIntStorage(void);
};
I am looking for some kind of simple way I can learn and understand merge sort on these. I have looked on the web and it has been found that merge sort is really good for singly linked lists but I cant understand how to do it. This is the websites I found:
Wikipedia Merge sort and
Specifically linked lists
I am not sure what code to give you. I basically just have this in my header file and am new to this so I am very basic. Thank you for your help in advance :)
class Node
{
public:
int data;
Node* next;
Node()
{
next = NULL;
data = 0;
}
};
class SLLIntStorage
{
public:
Node* head;
Node* current;
Node* tail;
void Read(istream&);
void Write(ostream&);
void setReadSort(bool);
void sortOwn();
void print();
bool _sortRead;
int numberOfInts;
SLLIntStorage(const SLLIntStorage& copying)
{
}
SLLIntStorage(void);
~SLLIntStorage(void);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你从维基百科上看这一段
这非常简洁地告诉您需要做什么以及需要什么样的操作。第 2 句和第 4 句是您需要能够执行
Split()
和Merge()
的操作。 Split 可以在您的Node
类上实现,类似地 Merge 可以作为一个整体来实现
1,2,3,4 描述执行实际排序所需执行的操作 按顺序执行这些步骤通过使用列表操作 Merge 和 Split 来实现排序功能。
这只是
Merge
和Split
的一种方式,如何实现它们将取决于您的风格、要求、C++ 知识以及各种其他因素。 (我相信您会在答案中看到其他一些解决方案)。您可能还需要一个 Node::Append(Node* val) 或类似的基本运算符。看起来您不需要 Node::Remove(Node* val) ,但实现它也可能不会有什么坏处。If you look at this paragraph from wikipedia
This tells you pretty succinctly what you need to do and what kind of operations you need. sentences 2 and 4 are operations that you need to be able to do
Split()
andMerge()
. Split could be implemented on yourNode
class assimilarly Merge could be implemented as
as a whole 1,2,3,4 describe what you need to do to perform the actual sorting implement these steps in order in the sorting function by using the list operations Merge and Split.
This is only one way
Merge
andSplit
could look like, how you implement them will depend on your style, requirements, knowledge of c++ and various other factors. (I am sure you will see some other solutions in the answers). You will probably also need aNode::Append(Node* val)
or something similar as a basic operator. It does not look like you will needNode::Remove(Node* val)
but it might not hurt to implement that too.