#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
int main()
{
struct node *head,*p,*q,*t;
int i,n,a;
scanf("%d",&n);
head=NULL;
for(i=1;i<=n;i++)
{
scanf("%d",&a);
p=(struct node *)malloc(sizeof(struct node));//动态申请一个空间
p->data=a;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;//如果不是第一个创建的结点,则将上一个的结点的后继指针指向当前指针
q=p;
}
scanf("%d",&a);
t=head;
while(t!=NULL)
{
if(t->next->data>a){
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=t->next;
t->next=p;//p是t 后
break;
}
t=t->next;
}
t=head;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->next;
}
return 0;
}
链表
最新推荐文章于 2024-09-04 18:37:12 发布