#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数据结构实验之链表六:有序链表的建立
最新推荐文章于 2026-06-06 11:22:11 发布
本文介绍了一种使用链表进行插入排序的方法,并提供了完整的C语言实现代码。该方法通过创建节点并根据数值大小进行有序链接,实现了对一组整数的有效排序。

512

被折叠的 条评论
为什么被折叠?



