#include "iostream"
using namespace std;
typedef struct da //定义结构
{
int num;
struct da *prior;
struct da *next;
}data;
data * creatlink() //形成双连表
{
data *head ,*tail;
head = new data;
tail = head;
tail ->prior = NULL;
tail ->next = NULL;
int nu = 0,i = 0;
cout<<"输入数字(以-1结束):";
data *ne;
while(nu != -1)
{
ne = new data;
cin>>nu;
ne ->num = nu;
if(i == 0) //判断第一次就一个开始节点
{
tail ->next = ne;
ne ->prior = tail;
ne ->next = NULL;
i = 1;
}
else //其他次插在两节点中间
{
ne ->prior = head;
ne ->next = tail ->next;
tail ->next ->prior = ne;
tail ->next = ne;
}
}
return tail; //返回头结点
}
void put(data *head) //输出双连表内容
{
data *temp = head ->next;
while(temp != NULL)
{
cout<<temp ->num<<' ';
temp = temp ->next;
}
}
void order(data *head) //冒泡排序功能函数
{
data *temp1,*temp2,*a,*b;
int i = 0;
a = head;
while(a ->next != NULL)
{
temp1 = temp2 = a ->next;
while(temp1 ->next != NULL)
{
temp1 = temp1 ->next;
if(temp1 ->num >= temp2 ->num) //找到最大值
temp2 = temp1;
}
for(b = a;b ->next != temp2;b = b ->next); //找到temp2的前一节点
b ->next = temp2 ->next;
if(temp2 ->next != NULL) //判断temp2是否为尾节点
temp2 ->next->prior = b;
head ->next ->prior = temp2; //将temp2插入head 的后面
temp2 ->next = head ->next;
head ->next = temp2;
temp2 ->prior = head;
if(i == 0)
{
a = temp2; //将挡板一次赋值
i = 1;
}
}
}
int main(void)
{
data *head ;
head = creatlink(); //形成双连表调用
cout<<"正常输出:";
put(head); //输出调用
cout<<'/n';
cout<<"排序后输出:";
order(head); //排序双连表
put(head); //输出双连表
cout<<'/n';
getchar();
getchar();
getchar();
}