#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
}*head,*p,*q;
void insertsort(int n)
{
struct node *x,*y;
while(n--)
{
q=(struct node *)malloc(sizeof(struct node));
scanf("%d",&q->data);
x=head->next;
y=head;
while(x!=NULL)
{
if(q->data<x->data)
{
y->next=q;
q->next=x;
break;
}
y=x;
x=x->next;
}
if(x==NULL)
{
y->next=q;
q->next=NULL;
}
}
}
void print(struct node *head)
{
p=head->next;
while(p)
{
printf("%d",p->data);
if(p->next)
printf(" ");
p=p->next;
}
}
int main()
{
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
int n;
scanf("%d",&n);
insertsort(n);
print(head);
}
SDUT2121数据结构实验之链表六:有序链表的建立
最新推荐文章于 2020-05-18 13:00:32 发布