c语言 键盘上输入10个任意的整数,要求按照升序的规则将其存入单链表中,并输出

如题所述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{ int num;
  node* next;
}*h,*p;
node *creat()
{ node*h,*p,*q,*q1;
  int x,i;
  h=(node*)malloc(sizeof(node));
  h->next=NULL;
  for(i=0;i<10;i++)
  {scanf("%d",&x);
   q=(node*)malloc(sizeof(node));
    q->num=x;
    q1=h;
    for(p=h->next;p&&p->num<x;p=p->next)q1=p;
    q->next=q1->next;
q1->next=q;
  }
  return h;
}
int main()
{ h=creat();
  for(p=h->next; p; p=p->next)
    printf("%d ",p->num);
  return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答