day18 小作业

#include"fun.h"
int main()
{
	shu_ptr H = shen();
	if(H == NULL)
	{
		return -1;
	}
	type a;
	char b;
	while(1)
	{
		printf("请输入第%d个数:",H->len+1);
		scanf("%d",&a);
		ru(H,a);
		printf("结束请按q");
		scanf(" %c",&b);
		if(b == 'q' || b == 'Q')
		{
			break;
		}
	}
	bian(H);
	int pos;
	printf("选择要插入的位置:");
	scanf("%d",&pos);
	printf("输入要插入的数:");
	scanf("%d",&a);
	add(H,pos,a);
	bian(H);
	printf("请输入想要查找的元素:");
	scanf("%d",&a);
	zhao(H,a);
	printf("请输入想要最值方式(0:最小 1):");
	scanf("%d",&a);
	zui(H,a);
	printf("请输入想要排序方式(0:降序 1):");
	scanf("%d",&a);
	pai(H,a);
	bian(H);
	//zhuan(H);
//	bian(H);

}
#include"fun.h"
int main()
{
	shu_ptr H = shen();
	if(H == NULL)
	{
		return -1;
	}
	type a;
	char b;
	while(1)
	{
		printf("请输入第%d个数:",H->len+1);
		scanf("%d",&a);
		ru(H,a);
		printf("结束请按q");
		scanf(" %c",&b);
		if(b == 'q' || b == 'Q')
		{
			break;
		}
	}
	bian(H);
	int pos;
	printf("选择要插入的位置:");
	scanf("%d",&pos);
	printf("输入要插入的数:");
	scanf("%d",&a);
	add(H,pos,a);
	bian(H);
	printf("请输入想要查找的元素:");
	scanf("%d",&a);
	zhao(H,a);
	printf("请输入想要最值方式(0:最小 1):");
	scanf("%d",&a);
	zui(H,a);
	printf("请输入想要排序方式(0:降序 1):");
	scanf("%d",&a);
	pai(H,a);
	bian(H);
	//zhuan(H);
//	bian(H);

}
ubuntu@ubuntu:01$ cat fun.c
#include"fun.h"
/***********申请堆区空间************/
shu_ptr shen()
{
	shu_ptr H = (shu_ptr)malloc(sizeof(shu)*MAX);
	if(H == NULL)
	{
		printf("申请不成功!\n");
		return NULL;
	} 
	else
	{
		memset(H->data,0,sizeof(H->data));
		H->len = 0;
		printf("申请成功!\n");
		return H;
	}
}
/************判断表是否填满*****************/
int man(shu_ptr H)
{
	if(H == NULL)
	{
		printf("所给顺序表不合法!\n");
		return 1;
	} 
	return H->len == MAX;
}
/************判断表是否为空*****************/
int kong(shu_ptr H)
{
	if(H == NULL)
	{
		printf("所给顺序表不合法!\n");
		return 1;
	}
	return H->len == 0;
}
/************向列表中添加元素*****************/
int ru(shu_ptr H,type a)
{
	if(H == NULL || man(H))
	{
		printf("添加失败!\n");
		return 0;
	} 
	H->data[H->len]=a;
	H->len++;
	printf("添加成功!\n");
	return 1;
}
/************顺序表的遍历*****************/
void bian(shu_ptr H)
{
	if(H == NULL || kong(H))
	{
		printf("遍历失败!\n");
		return ;
	} 
	printf("当前顺序表的元素分别是:");
	for(int i = 0;i < H->len;i++)
	{
		printf("%d  ",H->data[i]);
	}
	putchar(10);
}
/************向列表中任意位置添加元素*****************/
int add(shu_ptr H,int pos,type a)
{
	if(H == NULL || man(H) || pos < 0 || pos > H->len)
	{
		printf("添加失败!\n");
		return 0;
	} 
	for(int i = H->len-1; i >= pos-1;i--)
	{
		H->data[i+1] = H->data[i];
	}
	H->data[pos-1] = a;
	H->len++;
	return 1;
}
/*************从列表中查找元素****************/
void zhao(shu_ptr H,type a)
{
	if(H == NULL || kong(H))
	{
		printf("查找失败!\n");
		return ;
	} 
	int b = 0;
	for(int i = 0; i < H->len;i++)
	{
		if(a == H->data[i])
		{
			b = 1;
			printf("有该元素!\n");
			printf("%d\n",H->data[i]);
		}
	}
	if(b = 0)
	{
		printf("没有该元素!\n");
	}
}
/*************求最值****************/
void zui(shu_ptr H,int a)
{
	if(H == NULL || kong(H))
	{
		printf("求最值失败!\n");
		return ;
	} 
	int min = H->data[0],max = H->data[0]; 
	if(a == 1)
	{
		for(int i = 0;i < H->len;i++)
		{
			if(H->data[i]>max)
			{
				max = H->data[i];
			}
		}
		printf("max = %d\n",max);
	}
	else if(a == 0)
	{
		for(int i = 0;i < H->len;i++)
		{
			if(H->data[i]data[i];
			}
		}
		printf("min = %d\n",min);
	}
}
/*************排序****************/
void pai(shu_ptr H,int a)
{
	if(H == NULL || kong(H))
	{
		printf("排序失败!\n");
		return ;
	} 
	int k,temp;
	for(int i = 1;i < H->len;i++)
	{
		k=i-1;
		for(int j = i-1; j < H->len;j++)
		{
			if(a == 1)
			{
				if(H->data[k]>H->data[j])
				{
					k=j;
				}
			}
			else if(a == 0)
			{
				if(H->data[k]data[j])
				{
					k=j;
				}
			}
		}
		if(k != i-1)
		{
			temp = H->data[k];
			H->data[k] = H->data[i-1];
			H->data[i-1] = temp;
		}
	}
}
/*************反转****************/
void zhuan(shu_ptr H)
{
	int temp;
	for(int i = 0,j = H->len-1;i < j;i++,j++)
	{
		temp = H->data[i];
		H->data[i] = H->data[j];
		H->data[j] = temp;
	}
}
#ifndef _FUN_H_
#define _FUN_H_
#include
#include
#include
#define MAX 40
typedef int type;
typedef struct 
{
	type data[MAX];
	int len;
}shu,*shu_ptr;
shu_ptr shen();
int man(shu_ptr H);
int kong(shu_ptr H);
int ru(shu_ptr H,type a);
void bian(shu_ptr H);
int add(shu_ptr H,int pos,type a);
void zhao(shu_ptr H,type a);
void zui(shu_ptr H,int a);
void pai(shu_ptr H,int a);
void zhuan(shu_ptr H);
#endif

 

你可能感兴趣的:(学习方法)