⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exp09_03.c

📁 《C语言程序设计教程、实验与练习》 源文件下载
💻 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 + -