在双链接列表上插入排序有什么问题?

发布于 2025-01-22 10:12:32 字数 1840 浏览 0 评论 0原文

我得到一些随机结果。我已经经历了流动,并确实跑了几次,但无法确切地弄清楚代码出了什么问题。

程序流: 链接列表:5 4 3 2 1

  1. 电流位于第二位置,即4,也将此值存储到可变温度。
  2. 上一个位置是第1位,即
  3. 第1个位置,循环运行直到电流变为null。
  4. 第二个嵌套时,循环从电流的前身到头部节点以及条件tempdata。 如果条件为真,则将温度移至正确的位置。
  5. 最后重复第一个循环。
#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

  1. current is at 2nd position i.e. 4 , also storing this value to variable temp.
  2. previous is at 1st position i.e. 5
  3. 1st while loop runs until current becomes null.
  4. 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.
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2025-01-29 10:12:32

在绘制发生的事情时,通过手动跟踪您的代码:

开始时您

temp = 4     
previous = current->prev

5 -> 4 -> 3 -> 2 -> 1
^    ^
p    c

现在有上一个!= head是错误的,因此未输入循环,而您

previous->data = temp
current = current->next

4 -> 4 -> 3 -> 2 -> 1
^         ^
p         c

这看起来开始看起来不好 - 5已经消失了...

下一个迭代:

temp = 3     
previous = current->prev
    
4 -> 4 -> 3 -> 2 -> 1
     ^    ^
     p    c

上一遍!= head&amp;&amp;温度&lt;上一个&gt; data是正确的,因此请输入循环:

previous->next->data = previous->data;

4 -> 4 -> 4 -> 2 -> 1
     ^    ^
     p    c

previous = previous->prev;

4 -> 4 -> 4 -> 2 -> 1
^         ^
p         c

现在上一个!= head是错误的,退出循环。

previous->data = temp
current = current->next

3 -> 4 -> 4 -> 2 -> 1
^              ^
p              c

等等。

要解决此问题,请绘制您的列表以及在编写任何代码之前需要发生的事情
(并且不要忘记处理空列表。)

Trace through your code by hand while drawing what happens:

At the start you have

temp = 4     
previous = current->prev

5 -> 4 -> 3 -> 2 -> 1
^    ^
p    c

Now previous != head is false, so the loop isn't entered, and you

previous->data = temp
current = current->next

4 -> 4 -> 3 -> 2 -> 1
^         ^
p         c

This is beginning to look bad - the 5 has disappeared...

Next iteration:

temp = 3     
previous = current->prev
    
4 -> 4 -> 3 -> 2 -> 1
     ^    ^
     p    c

previous != head && temp < previous->data is true, so enter the loop:

previous->next->data = previous->data;

4 -> 4 -> 4 -> 2 -> 1
     ^    ^
     p    c

previous = previous->prev;

4 -> 4 -> 4 -> 2 -> 1
^         ^
p         c

Now previous != head is false, exit the loop.

previous->data = temp
current = current->next

3 -> 4 -> 4 -> 2 -> 1
^              ^
p              c

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文