- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用冒泡排序对双向链表进行排序。它存在循环问题,而且交换也没有正确进行。问题是最大的数字应该在 i 的一次迭代之后出现在最后。但它并没有停在两者之间。我尝试了各种选择但无法解决。请帮我解决这个问题
#include <iostream>
using namespace std;
class node{
public:
node *next;
node *prev;
int data;
node(int x, node *p, node *n)
{
next = n;
prev = p;
data = x;
}
};
class list{
private:
node *head;
node *tail;
public:
list()
{
head = NULL;
tail = NULL;
}
void addtoend(int x)
{
if(head == NULL)
{
head = new node (x,NULL,NULL);
tail = head;
}
else if(tail->prev == NULL)
{
tail = new node(x,head,NULL);
head->next = tail;
}
else
{
node *temp = tail;
tail = new node(x,tail,NULL);
temp->next = tail;
}
}
void bubblesort()
{
node *temp = head;
node *temp2 = temp;
for(int i=0;i<10;i++)
{
cout<<"I: "<<i<<endl;
temp = head;
while(1)
{
temp2 = temp;
if(temp->next->data < temp->data)
{
if(temp == head)
{
cout<<"HEAD"<<endl;
head = head->next;
node *after = head->next;
head->prev = NULL;
temp->prev = head;
head->next = temp;
temp->next = after;
after->prev = temp;
}
else if(temp->next == tail)
{
cout<<"TAIL"<<endl;
node *back = temp->prev;
temp->next = NULL;
tail->next = temp;
temp->prev = tail;
back->next = tail;
tail->prev = back;
tail = tail->next;
tail->next=NULL;
}
else
{
cout<<"ANY"<<endl;
node *back = temp->prev;
node *after = temp->next->next;
back->next=temp->next;
temp->prev = temp->next;
after->prev->next = temp;
after->prev->prev = back;
after->prev = temp;
temp->next = after;
}
}
display();
display0();
temp = temp2->next;
cout<<"HEAD: "<<head->data<<endl;
cout<<"Tail: "<<tail->data<<endl;
cout<<endl;
if(temp->next->next== NULL)
{
break;
}
}
}
}
void display()
{
node *temp = head;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void display0()
{
node *temp = tail;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->prev;
}
cout<<endl;
}
};
main()
{
list l1;
int x[10] = {17,15,8,12,10,5,4,1,7,2};
for(int i=0;i<10;i++)
{
l1.addtoend(x[i]);
}
l1.display();
l1.bubblesort();
l1.display();
}
最佳答案
至少有两种方法可以在 c++
中为双链表 实现冒泡排序算法。如 I. 所示交换内容,即两个相邻节点的数据,并如 II. 所示重新连接指针:
II.的优点是不需要临时内存temp2
。因此,正如 OP 所述,这是首选解决方案。
只需用这个替换 OP bubblesort
函数:
void bubblesort()
{
bool is_sorted = false;
node *temp = head;
while (!is_sorted)
{
is_sorted = true;
temp = head;
while (temp->next != NULL)
{
if(temp->next->data < temp->data)
{
is_sorted = false;
int temp2 = temp->data;
temp->data = temp->next->data;
temp->next->data = temp2;
}
temp = temp->next;
}
}
display();
display0();
}
该算法使用 bool 变量is_sorted
来检查双链表 的排序是否完成。否则泡沫仍在上升。它只交换双链表的数据值。
这是 OP 所需的解决方案,也是首选解决方案:
void bubblesort()
{
bool is_sorted = false;
node *temp = head;
while (!is_sorted)
{
is_sorted = true;
temp = head;
while (temp->next != NULL)
{
if(temp->next->data < temp->data)
{
is_sorted = false;
if(temp == head)
{
//temp->prev->next = temp->next; // 1. (impossible)
temp->next->prev = NULL; // 2.
head = temp->next;
}
else
{
temp->prev->next = temp->next; // 1.
temp->next->prev = temp->prev; // 2.
}
temp->prev = temp->next; // 3.
if(temp->next == tail)
{
temp->next = NULL; // 4.
tail = temp;
//temp->next->prev = temp2; // 5. (impossible)
}
else
{
temp->next = temp->next->next; // 4.
temp->next->prev = temp; // 5.
}
temp->prev->next = temp; // 6.
temp = temp->prev;
}
temp = temp->next;
}
}
display();
display0();
}
具有更新的 bubblesort
函数的程序的输出是:
17 15 8 12 10 5 4 1 7 2
1 2 4 5 7 8 10 12 15 17
17 15 12 10 8 7 5 4 2 1
1 2 4 5 7 8 10 12 15 17
第一行显示未排序的输入列表。第二个显示升序排列结果,第三个显示降序排列,这证明列表的正向和反向链接仍然有效。最后一行是 main
函数中输出的排序列表。
当然,列表的原始顺序丢失了。
完整的源代码也可以在 github 上找到。 .
希望对您有所帮助。
关于c++ - 冒泡排序双向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573297/
我在 Angular 项目中使用了来自 Github 的模块项目,它可以让我调整 DOM 元素的大小,这些元素绘制在 div 之上,充当绘图板。 为了塑造我的第一个元素(它们是简单的矩形),顺便说一句
如何在类之间传递事件? 我知道这听起来很荒谬(确实如此),但在过去的一段时间里我一直被这个问题难倒了。搜索没有出现类似的问题,所以我想我会提出这个问题。 这里是涉及的对象: WinForm -> Sp
我在 class 中订阅了一个 Event。比如 MainStation mainStation = StationFactory.GetMainStation(); mainStation.Fre
当用户多次悬停(mouseenter)时如何防止冒泡或“失控”。当用户悬停时,我使用slideDown和slideUp作为mouseleave,并将延迟设置为250。只有当延迟设置为1毫秒时,我才能解
背景:我目前正在编写一个greasemonkey 脚本,用于嵌入/修改特定页面的html。该页面设置有 3 个嵌套 div。在这 3 个 div 中,我只能将事件监听器添加到最外面的 div(这是由于
想想 HTML5 服务器发送的事件(我在服务器端使用 php)。为了获取服务器发送的数据,我有以下代码: if(typeof(EventSource) !== "undefined") { v
我想禁用 SPA 路由器正文部分中所有具有“nolink”类的链接。为了实现这一点,我使用了事件委托(delegate),它不能很好地处理嵌套元素。 (下面是简化的代码)。 HTML:
实验证据使我相信键(向上、向下、按下)事件只会触发(在气泡阶段)处于焦点的元素。 这种行为可能是直观的、明显的和/或可取的,但还没有在任何地方看到这种记录,所以我希望社区能够证实我的“理论”。 否则,
如果我有以下布局: public class A : INotifyPropertyChanged { public event PropertyChangedEventHandler Pro
我有几个带有随机数的 div,我后来使用一些简单的冒泡排序代码按升序排列,我想逐渐排列并为它们添加样式,而不是对它们进行样式设置并立即安排。 这感觉很简单,但我无法找到 sleep for 循环 或正
我已经用 Java 实现了所有四种排序算法。只是为了它,我决定查看每种算法中的交换次数和比较次数。对于大小为 20 的随机数组,这是我的结果 冒泡排序:87 次交换,87 次比较 插入排序:87 次交
所以根据MDN (这是有道理的)AnimationEvent 有 bubble 参数,但是如何将它设置为 true?考虑到该事件是从 CSS 动画触发的。 最佳答案 好吧,原来CSS是做冒泡事件的,例
我在 WPF WindowsFormsHost 中有 Winforms 控件。Winforms 控件是被动的,不能处理任何鼠标事件。鼠标事件应该像往常一样从 WPF 可视化树中最内部的 WPF 控件引
我有一个 iframe,它具有警报功能和 console.log 功能。 我能够看到 console.log 的输出,但是警报功能没有冒泡(永远不会以可见的方式触发)。 我尝试在 chromium 上
我有以下 html 设置: blaat blaat2 它的样式使您不能悬停 div1 而不悬停其他 2 个 div 之一。现在我在 div1 上有一个 mouseout。 问题是当我从 conte
最近学习了python基础,写一下3大排序练练手: 复制代码 代码如下: ''' Created on 2013-8-23 @author: codegeek
我正在处理一个内容可编辑的 div,我需要在将字符添加到 div 之前和之后捕获击键。 我对捕获与冒泡的理解是,事件首先在 dom 树的最高层捕获,然后向下传递,而对于冒泡,它从最低层开始,然后在树上
Demo Here 我想要实现的目标: 鼠标悬停时 - 将显示共享图标。 单击共享图标时,将显示新 Div 问题 当鼠标移出共享图标时“新 Div 不应关闭,必须显示”。 当大图像的 MouseOut
我知道我的问题是在 DOM 内冒泡,因为我的分页中有多个页面,当我多次单击编辑类链接时,它会冒泡并连续加载相同的文件,我想知道更好的解决这个问题的方法。 $('.edit').live('click'
我需要使用事件委托(delegate)捕获内部带有图像的 anchor 节点。 document.addEventListener( 'click', function(event) {
我是一名优秀的程序员,十分优秀!