建立一个长度为n的带头结点的双向链表,使得该链表中的数据元素递增有序排列。(必须使用双向链表完成,数据类型为整型。)
输入
第一行:双向表的长度; 第二行:链表中的数据元素。
输出
输出双向链表中的数据元素的值。
样例输入
10 2 4 6 3 5 8 10 21 12 9
样例输出
2 3 4 5 6 8 9 10 12 21
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std;
struct node
{
int date;
node *last;
node *next;
};
int main()
{
int n;
while (cin >> n)
{
node *p = new node;
node *s, *q, *t;
cin >> p->date;
p->next = NULL;
s = p;
for(int i=2;i<=n;i++)
{
t = new node;
cin >> t->date;
t->next = NULL;
s->next = t;
t->last = s;
s = t;
}
for (int i = 1; i <= n; i++)
{
t = p;
while (t != NULL)
{
if (t->next != NULL)
{
if (t->date > t->next->date)
{
int temp = t->date;
t->date = t->next->date;
t->next->date = temp;
}
}
t = t->next;
}
}
t = p;
while (t != NULL)
{
cout << t->date << ' ';
t = t->next;
}
}
return 0;
}