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

📄 di10.c

📁 插入排序
💻 C
字号:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct data)
int n;


struct data
{
	int num;
	struct data *next;
};


struct data *creat(void)
{
 struct data *head,*last;
 struct data *p1,*p2,*p3;
 n=0;
 p1=p2=(struct data *)malloc(LEN);
 scanf("%d",&p1->num);
 head=NULL;
 while(p1->num!=0)
   {
    n=n+1;
    if(n==1) head=p1,last=p1,last->next=NULL;
    else if(p1->num<head->num) p1->next=head,head=p1,last=p1;
         else
          {
			  p2=head,p3=head->next;
			  while(p3!=NULL&&p1->num>p3->num)p2=p2->next,p3=p3->next;
			  p1->next=p3;
			  p2->next=p1;
          }
	p1=(struct data *)malloc(LEN);
	scanf("%d",&p1->num);
   }
 return(head);
}


void print(struct data *head)
{
	struct data *p;
	p=head;
	if(head!=NULL)
		do
		{
			printf("%d ",p->num);
			p=p->next;
		}while(p!=NULL);
}


void maopao()
{
 int a[10];
 int i,j,k;
 printf("intput 10 numbers:\n");
 for(i=0;i<10;i++)
     scanf("%d",&a[i]);
 printf("\n");
 for(i=0;i<9;i++)
     for(j=0;j<10-1;j++)
	 if(a[j]>a[j+1])
	 {
			 k=a[j];
			 a[j]=a[j+1];
			 a[j+1]=k;
	  }
  printf("the sorted numbers:\n");
  for(i=0;i<10;i++)
      printf("%d ",a[i]);
}



void main()
{
	int i,j=1;
	struct data *head,dat;
	while(j==1)
	   {
	     printf("请输入数据(插入法排序请按 1;起泡法排序请按 0)");
	     scanf("%d",&i);
	     if(i==1)
	       {
		  head=creat();
		  print(head);
	       }
	     else
		  maopao();
	     printf("\n是否继续测试(继续请按 1;结束请按 0)");
	     scanf("%d",&j);
	   }
	getch();
}




 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -