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

📄 zhongheti.c

📁 二叉树dsff soih o ijd sh
💻 C
字号:
 
#define LEN 10
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"
typedef struct node
	{
 		int data;
  		struct node *link;
	}NODE;
typedef struct sqlist
	{
 		int a[LEN];
 		int length;
	};
void init(struct  sqlist *L)  /*初始化*/
	{
 		int i;
 		for(i=0;i<LEN;i++)
	        L->a[i]=0;
 		L->length=0;
	}
void creat1(struct sqlist *L)  /*建顺序表*/
	{
 		int i;
 		printf("please input length:\n");
 		scanf("%d",&L->length);
 		printf("please input %d nums\n",L->length);
 		for(i=0;i<L->length;i++)
 			scanf("%d",&L->a[i]);
	}
void print1(struct sqlist *L)    /*输出顺序表*/
	{
 		int i;
		for(i=0;i<L->length;i++)
		printf("%d",L->a[i]);
		printf("\n");
	}

 NODE *creat2()     /*按照由大到小建立链表*/
 {
	int data;
  	NODE *h,*p,*q,*r;
	h=(NODE*) malloc (sizeof (NODE)); h->link=NULL;
	printf("please input datas(0 end)\n");
	scanf("%d",&data);
	while (data)
 		{p=(NODE*) malloc (sizeof (NODE));
 		 p->data=data; p->link=NULL;
  		if (h->link==NULL) h->link=p;
  		else
  			{r=h; q=r->link;
   			while  (p->data<q->data && q)
     				{r=q; q=q->link;}
   			if (q)
     			 p->link=q;
    			r->link=p;
   			}
  		scanf("%d",&data);
 		 }
 	return(h);
 }

void print2(struct node *h)          /*输出链表*/
	{	NODE *p;
  		p=h->link;
		while(p)
			{printf("%5d",p->data);
 			 p=p->link;
			}
 		printf("\n\n");
	}


void print3(struct node *h)
	{NODE *p;
 	 p=h;
 	 while(p)
 		{printf("%5d",p->data);
  		 p=p->link;
		}
  		printf("\n\n");
	}
NODE *invert(NODE *h)
	{
	 NODE *p,*q;
	 p=h->link;
 	 h->link=NULL;
	 while(p)
 		{q=p;p=p->link;
 		q->link=h->link;
 		h->link=q;
		}
	 }                                 /*链表逆置*/


void bubblesort(struct sqlist *L)
	{
	int i,j,t;
	for(j=0;j<L->length;j++)
  		for(i=0;i<L->length-1-j;i++)
   			 if(L->a[i]>L->a[i+1])
     				 {
      				  t=L->a[i];
       				  L->a[i]=L->a[i+1];
      				  L->a[i+1]=t;
     				 }
    	printf("the sorted nums:\n");
    	for(i=0;i<L->length;i++)
    	printf("%d",L->a[i]);
  	printf("\n");
	}


void selectsort(struct sqlist *L)
	{
 		int i, j, small,temp;
 		for(i=0;i<=L->length-1;i++)
 			{small=i;
  			for(j=i+1;j<L->length;j++)
  				{
				 if(L->a[j]<L->a[small]) small=j;
  				 if(small!=i)
     					{temp=L->a[i];
     					 L->a[i]=L->a[small];
     					 L->a[small]=temp;
      					}
   				}
  			}
		printf("the selectsort nums:\n");
		for(i=0;i<L->length;i++)//把原来的i<=的=号去了就对了.
		printf("%d",L->a[i]);printf("\n");
	}	



void quicksort(struct sqlist *L,int s,int t)
{int i=s,j=t;
 if(s<t)
	{L->a[0]=L->a[s];
		do
 		{while(i<j&&L->a[j]>=L->a[0])
  			j--;
  		if(i<j)
			{
		        L->a[i]=L->a[j];
			i++;
 		  	}
  		while(i<j&&L->a[i]<=L->a[0]) i++;
 		if(i<j)
 			{ L->a[j]=L->a[i];j--;}
  		}
		while(i<j);
   			L->a[i]=L->a[0];
   		quicksort(L,s,j-1);
   		quicksort(L,j+1,t);
	}
 printf("the quicksort nums:\n");
 for(i=0;i<L->length;i++)
 printf("%d",L->a[i]);printf("\n");
}

NODE *combine(struct sqlist *sq, NODE *ah)
	{
	NODE *k,*i,*ch,*p;
	int x,n=1;
	i=ah; ch=NULL; k=NULL;
	while ((i!=NULL)&&((n<=sq->length)))
 		{
		if (sq->a[n-1]>=i->data)  {x=i->data; i=i->link;}
  		else {x=sq->a[n-1]; n++;}
 		 if (ch==NULL)
    			{p=(struct node *) malloc (sizeof(struct node));
    		 	p->data=x;ch=p; k=p;
    			}
  		else  if (x!=k->data)
	  		{p=(struct node *) malloc (sizeof(struct node));
	   		p->data=x; k->link=p; k=p;
	  		}
 		}
	if (n>sq->length)
		while (i!=NULL)
			{
			x=i->data; i=i->link;
			if (ch==NULL)
				{p=(struct node *) malloc (sizeof(struct node));
				p->data=x; ch=p; k=p;
				}
			else if (x!=k->data)
				{
				p=(struct node *) malloc (sizeof(struct node));
				p->data=x; k->link=p; k=p;
				}
			}
	else
		while (n<=sq->length)
			{x=sq->a[n-1]; n++;
			if (ch==NULL)
				{p=(struct node *) malloc (sizeof(struct node));
				p->data=x; ch=p; k=p;
				}
			else if (x!=k->data)
				{p=(struct node *) malloc (sizeof(struct node));
				p->data=x; k->link=p; k=p;
				}
			}
	if (k!=NULL) k->link=NULL;
	return(ch);
	}



void main()
{
   int n,x;NODE *p,*y,*c;
   struct sqlist *L;
   struct sqlist slist;
   L=&slist;

    x=0;

while(1)
 {printf("=====================\n");
  printf("1.init\n");
  printf("2.built\n");
  printf("3.bubblesort\n");
  printf("4.selectsort\n");
  printf("5.quicksort\n");
  printf("6.invert\n");
  printf("0.tuichu\n");
  printf("7.combine\n");
  printf("=====================\n");
  printf("please input your chosice:\n");
  scanf("%d",&x);
   switch(x)
    {case 1: creat1(L);print1(L);break;
     case 2: p=creat2();print2(p);break;
     case 3: bubblesort(L);break;
     case 4: selectsort(L);break;
     case 5: quicksort(L,0,L->length);break;
     case 6: invert(p);print2(p);break;
     case 0: exit(0);break;
     case 7: {bubblesort(L);c=invert(p);y=combine(L,c);print3(y);break;}
     default : printf("\nInput error!"); break;
     }
  }
}



//你帮我看看selectsort和quicksort好像一个有乱码  一个出错  编译都没有问题拉

⌨️ 快捷键说明

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