在双链接列表上插入排序有什么问题?
我得到一些随机结果。我已经经历了流动,并确实跑了几次,但无法确切地弄清楚代码出了什么问题。
程序流: 链接列表:5 4 3 2 1
- 电流位于第二位置,即4,也将此值存储到可变温度。
- 上一个位置是第1位,即
- 第1个位置,循环运行直到电流变为null。
- 第二个嵌套时,循环从电流的前身到头部节点以及条件tempdata。 如果条件为真,则将温度移至正确的位置。
- 最后重复第一个循环。
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int data) {
this->data = data;
next = NULL;
prev = NULL;
}
};
void insert(Node *&head, Node *&tail, int data) {
if (head == NULL) {
Node *temp = new Node(data);
temp->next = temp;
head = temp;
tail = temp;
} else {
Node *temp = new Node(data);
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void display(Node *head) {
Node *temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void insertionSort(Node *&head, int size) {
Node *current = head->next;
Node *previous;
while (current!= NULL) {
int temp = current->data;
previous = current->prev;
while (previous != head && temp < previous->data) {
previous->next->data = previous->data;
previous = previous->prev;
}
previous->data = temp;
current = current->next;
}
}
int main() {
Node *head = NULL;
Node *tail = NULL;
int size;
int data;
cout << "Linked-List Size: ";
cin >> size;
cout << "Enter Elements: ";
for (int i = 0; i < size; i++) {
cin >> data;
insert(head, tail, data);
}
display(head);
insertionSort(head, size);
display(head);
}
I am getting some random result. I have gone through the flow and did dry run several time but can't figure out exactly what's wrong with the code.
flow of program:
linked list : 5 4 3 2 1
- current is at 2nd position i.e. 4 , also storing this value to variable temp.
- previous is at 1st position i.e. 5
- 1st while loop runs until current becomes null.
- 2nd nested while loop runs from predecessor of current to head node along with the condition tempdata.
if condition is true then move temp to its correct position. - at last repeat the first loop.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int data) {
this->data = data;
next = NULL;
prev = NULL;
}
};
void insert(Node *&head, Node *&tail, int data) {
if (head == NULL) {
Node *temp = new Node(data);
temp->next = temp;
head = temp;
tail = temp;
} else {
Node *temp = new Node(data);
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void display(Node *head) {
Node *temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void insertionSort(Node *&head, int size) {
Node *current = head->next;
Node *previous;
while (current!= NULL) {
int temp = current->data;
previous = current->prev;
while (previous != head && temp < previous->data) {
previous->next->data = previous->data;
previous = previous->prev;
}
previous->data = temp;
current = current->next;
}
}
int main() {
Node *head = NULL;
Node *tail = NULL;
int size;
int data;
cout << "Linked-List Size: ";
cin >> size;
cout << "Enter Elements: ";
for (int i = 0; i < size; i++) {
cin >> data;
insert(head, tail, data);
}
display(head);
insertionSort(head, size);
display(head);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在绘制发生的事情时,通过手动跟踪您的代码:
开始时您
现在有
上一个!= head
是错误的,因此未输入循环,而您这看起来开始看起来不好 - 5已经消失了...
下一个迭代:
上一遍!= head&amp;&amp;温度&lt;上一个&gt; data
是正确的,因此请输入循环:现在
上一个!= head
是错误的,退出循环。等等。
要解决此问题,请绘制您的列表以及在编写任何代码之前需要发生的事情。
(并且不要忘记处理空列表。)
Trace through your code by hand while drawing what happens:
At the start you have
Now
previous != head
is false, so the loop isn't entered, and youThis is beginning to look bad - the 5 has disappeared...
Next iteration:
previous != head && temp < previous->data
is true, so enter the loop:Now
previous != head
is false, exit the loop.And so on.
To solve this, draw your lists and what needs to happen before writing any code.
(And don't forget to handle the empty list.)