69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int height;
struct node *next;
}linklist;
linklist *CreateList()
{
linklist *head=(linklist *)malloc(sizeof(linklist));
head->next=NULL;
return head;
}
void ini(linklist *head,int num)
{
linklist *s,*r;
int i;
r = head;
for (i=0;i<num;i++)
{
s=(linklist*)malloc(sizeof(linklist));
if (!s)
exit(0);
scanf("%d",&s->height);
r->next = s;
r=s;
}
r->next=NULL;
}
void insert(linklist *head,int h)
{
linklist *p,*q,*z;
q = head;
p = (linklist *)malloc(sizeof(linklist));
if (!p)
exit(0);
p->height = h;
while(q->next && q->next->height < h)
{
q = q->next;
}
p->next = q->next;
q->next = p;
#if 0
if (q->next==NULL)
{
q->next=p;
p->next=NULL;
}
else
{
z=q->next;
q->next=p;
p->next=z;
}
#endif
}
void Output(linklist *head)
{
linklist *p=head->next;
while(p!=NULL)
{
printf("%d ",p->height);
p=p->next;
//printf(" %d",p->height);
}
}
int main()
{
int num,number;
linklist *L;
L=CreateList();
scanf("%d%d",&num,&number);
ini(L, num);
insert(L,number);
Output(L);
return 0;
}
void Output(linklist *head)
{
linklist *p=head->next;
printf("%d",p->height);
while(p!=NULL)
{
printf(" %d",p->height);
p=p->next;
// printf(" %d",p->height); 这句放到上面就没有问题了
}
}