exp09_03.c
来自「《C语言程序设计教程、实验与练习》 源文件下载」· C语言 代码 · 共 62 行
C
62 行
#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 + =
减小字号Ctrl + -
显示快捷键?