双向链表的操作问题(0960)
Time limit(ms): 1000
Memory limit(kb): 10000
Submission: 1559
Accepted: 778
Accepted
建立一个长度为n的带头结点的双向链表,使得该链表中的数据元素递增有序排列。(必须使用双向链表完成,数据类型为整型。)
Description
第一行:双向表的长度; 第二行:链表中的数据元素。
Input
输出双向链表中的数据元素的值。
Output
1
2
3
|
10
2 4 6 3 5 8 10 21 12 9
|
Sample Input
1
|
2 3 4 5 6 8 9 10 12 21
|
Sample Output
#include<stdio.h>
#include<stdlib.h>struct Date
{
int date;
struct Date *next;
struct Date *prior;
};
#define len struct Date
int main()
{
void res(struct Date *head,int total);
struct Date *head;
int total;
while(scanf("%d",&total)!=EOF)
{
res(head,total);
}
return 0;
}
void res(struct Date *head,int total)
{
void sort(struct Date *head,int total);
struct Date *p,*p1;
head=(len *)malloc(sizeof(len));
head->next=NULL;
head->prior=NULL;
int i;
for(i=0;i<total;i++)
{
p=(len *)malloc(sizeof(len));
scanf("%d",&p->date);
if(head->next==NULL)
{
head->next=p;
p->prior=head;
}
else
{
p1->next=p;
p->prior=p1;
}
p1=p;
}
p1->next=NULL;
sort(head,total);
p=head->next;
while(p!=NULL)
{
if(p==head->next)
printf("%d",p->date);
else
printf(" %d",p->date);
p=p->next;
}
free(head);
free(p);
}
void sort(struct Date *head,int total)
{
struct Date *p,*p1;
int temp;
int i;
for(i=0;i<total;i++)
{
p=head->next;
p1=p->next;
while(p->next!=NULL)
{
if(p->date>p1->date)
{
temp=p->date;
p->date=p1->date;
p1->date=temp;
}
p=p1;
p1=p->next;
}
}比较算法部分,这里比较的时候只是数据域进行比较,节点不动,每一次都从第一个开始比较
}