Data structure - linear table orderly inserted into C language source code

Keywords: C

Data structure - linear table orderly inserted into C language source code

#include<stdio.h>
#include<stdlib.h>
struct YXCR{
	int*elem;
	int length;
};
void ko(YXCR& l,int n)
{
	
		int hj;
		l.elem=(int*)malloc(sizeof(int)*n);
		for(int k=0;k<n;k++)
		{
		scanf("%d",&hj);
		l.elem[k]=hj;	
		}
		l.length =n;
}
void xianshi(YXCR l)
{
	for(int i=0;i<l.length ;i++)
	{
		printf(" %d ",l.elem[i]);
	}
}
int hf(YXCR &l,int low,int high)
{
	int p=l.elem[low];
	int lo,h;
	lo=low;
	h=high;
	while(lo!=h)
	{
		while(lo!=h&&l.elem[h]>=p)
		{
			h--;
		}
		int fg=l.elem[lo];
		l.elem[lo]=l.elem[h];
		l.elem[h]=fg;
		while(lo!=h&&l.elem[lo]<=p)
		{
			lo++;
		}
		int xg;
		xg=l.elem[lo];
		l.elem[lo]=l.elem[h];
		l.elem[h]=xg;
	}
	return lo;
}
void fv(YXCR &l,int low,int high)
{
	if(low<high)
	{
		int oi;
		oi=hf(l,low,high);
		fv(l,low,oi-1);
		fv(l,oi+1,high);
	}
}
int insert(YXCR &l,int a,int hu,int &h)
{
	int f=l.length;
	for(int i=0;i<f;i++)
	{
		if(l.elem[i]==a)
		{
			l.elem=(int*)realloc(l.elem,(l.length+1)*sizeof(int));
			for(int j=l.length ;j>i;j--)
			{
				l.elem[j]=l.elem[j-1];
			}
			l.length++;  
			l.elem[i]=hu;
			return  1;
		}
		h++;
	}
	return 0;
}
main()
{
	int n;
	printf("Please enter the table length");
	scanf("%d",&n);
	YXCR l;
	printf("\n Start input table elements");
	ko(l,n);
	int low=0;
	int  high=l.length -1;
	fv(l,low,high);
	xianshi(l);
	printf("\n The operation is to insert a specified element before a specific element. Please enter a specific element and a specified element");
	int a,hu,h=0;
	scanf("%d",&a);
	scanf("%d",&hu);
	int bh=insert(l,a,hu,h);
	if(bh==0)
	{
		printf("No specific elements"); 
	 } 
	 else{
	 	printf("The number of comparisons is%d\n",h);
	 	h=l.length-h-1;
	 	printf("The number of moves is%d",h);
	 }
	 xianshi(l);
	 printf("\n%d",l.length );
	return 0;
} 

Look, that's the point! I want to see that there are only two kinds of students here: those who copy the code and those who copy the code.

However, when copying away, you need to think about one thing. After copying away the code, you have an egg to use. It is the most important thing for you to understand.

OK, it's soy sauce.

Old fellow, if this is not acceptable, I can not tell you!!! Oh, by the way, you look so good, pay attention to it...

Finally, I said to myself:
Every misfortune you encounter now comes from a time when you refused to work hard.

Posted by kushaljutta on Mon, 02 Dec 2019 10:07:59 -0800