此代码用单链表代替复杂难懂得双链表
#include<iostream>
using namespace std;
struct list //创建链表
{
int price;
int struct_node;
list* next;
};
//初始化链表变量
int n;
list* head = new list;
list* cur = head;
list* tail = 0;
list* Positioning_nodes(int x) {//定位节点(代替双链表,但时间复杂度更高)
cur = head;
for (; cur->struct_node != x;) {
cur = cur->next;
}
list* return_1 = cur;
cur = head;
return return_1;
}
int main() {
cin >> n;
//初始化链表/输入数据
for (int i = 0; i < n; i++) {
cin >> cur->price;
cur->struct_node = i;
cur->next = new list;
tail = cur;
cur = cur->next;
}
cur = head;
for (int i = 0; i < n; i++) {
//从后往前扫描
for (int m = i; m > 0; m--) {
if ((Positioning_nodes(m)->price) < (Positioning_nodes(m - 1)->price)) {//交换
int temp=Positioning_nodes(m)->price;
Positioning_nodes(m)->price = Positioning_nodes(m - 1)->price;
Positioning_nodes(m - 1)->price = temp;
}
}
}
cur = head;
for (int i = 0; i < n; i++) {//输出结果
cout << cur->price<<" ";
cur = cur->next;
}
}