您的位置:首页 > 理论基础 > 数据结构算法

SDUT 2121 数据结构实验之链表六:有序链表的建立

2017-09-19 21:26 477 查看

数据结构实验之链表六:有序链表的建立

Time Limit: 1000MSMemory Limit: 65536KB

SubmitStatistic

Discuss

Problem Description

输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表。

Input

第一行输入整数个数N;

第二行输入N个无序的整数。

Output

依次输出有序链表的结点值。

Example Input

6
33 6 22 9 44 5


Example Output

5 6 9 22 33 44


Hint

不得使用数组! 
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct NODE
{
int data;
struct NODE *next;
}node;
node *create(node *&head,int n)
{
head=(node*)malloc(sizeof(node));
node *p,*q;
q=head;
for(int i=0;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
q->next=p;
q=p;
}
q->next=NULL;
return head;
}
void print(node *head)
{
node *p;
p=head->next;
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->data);
else
printf("%d\n",p->data);
p=p->next;
}
}
node *del(node *head)
{
node *p,*q,*t;
p=head->next->next;
head->next->next=NULL;
while(p!=NULL)
{
q=p->next;
t=head;
while(t->next!=NULL&&t->next->data<p->data)
t=t->next;
p->next=t->next;
t->next=p;
p=q;
}
print(head);
}
int main()
{
int n;
node *head;
scanf("%d",&n);
create(head,n);
//print(head);
del(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: