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

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

2016-07-23 11:20 337 查看
#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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: