📄 exp09_03.c
字号:
#include <stdio.h>
#include <alloc.h>
struct Node_tag
{
int x;
struct Node_tag *next;
};
typedef struct Node_tag NODE;
void main()
{
int k;
int a;
NODE *head;
NODE *p,*q;
head=(NODE *)malloc(sizeof(NODE));
if(head==NULL)
{
printf("no enough memory!\n");
return;
}
head->next=NULL;//creat a linklist with zero nodes
for(k=0;k<5;k++)
{
p=(NODE *)malloc(sizeof(NODE));
if(p==NULL)
{
printf("no enough memory!\n");
return;
}
printf("input the %dth number:",k+1);
scanf("%d",&a);
p->x=a;
//insert the p to linklist
q=head;
while(q->next!=NULL)
{ //search the position of insert
if(q->next->x<p->x)
break;
q=q->next;
}//while
// p should be insert after q
p->next=q->next;
q->next=p;
}//for
p=head->next;
while(p!=NULL)
{//output the data of linklist
printf("%d ",p->x);
p=p->next;
}
p=head;
while(p->next!=NULL)
{ //destroy the linklist
q=p->next;
p->next=q->next;
free(q);
}
free(head); //free the head
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -