day3 data homwork

顺序表按元素后续插入 排序 合并

.h

#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAXSIZE 12
typedef int datatype;

typedef struct
{
	datatype data[MAXSIZE]; 
	int len;
}Seqlist,*seqlist;

seqlist creat();
int insert_rear(seqlist list,datatype e);
int full_seqlist(seqlist list);
int empty_seqlist(seqlist list);
void output_seqlist(seqlist list);
int delete_rear(seqlist list);
int search_sub(seqlist list,int sub);
int updata_sub(seqlist list,int sub,datatype e);
int insert_sub(seqlist list,int sub,datatype e);
int delete_sub(seqlist list,int sub);

int search_data(seqlist list,datatype e);
int updata_data(seqlist list,datatype e,datatype key);
int delete_data(seqlist list,datatype e);
void delete_re(seqlist list);
int insert_data(seqlist list,datatype e,datatype key);
int bubble_seqlist(seqlist list);
void merge_seqlist(seqlist a,seqlist b,seqlist c);
seqlist free_space(seqlist list);



#endif

.c

#include"1.h"

seqlist creat()
{
	seqlist list=(seqlist)malloc(sizeof(Seqlist));

	if(list==NULL)
		return NULL;
	list->len=0;//顺序表初始化
	return list;
}
int full_seqlist(seqlist list)
{
	return list->len == MAXSIZE ? -1:0;
}
int empty_seqlist(seqlist list)
{
	return list->len == 0? -1:0;
}

int insert_rear(seqlist list,datatype e)
{   //判断
	if(list==NULL || full_seqlist(list))
	{
		printf("insert fail\n");
		return -1;  //fail
	}
//尾部插入
	list->data[list->len]=e;
	list->len++;
	return 0;    //sucess
}
int delete_rear(seqlist list)
{
	if(list==NULL || empty_seqlist(list))
	{
		printf("delete_rear fail\n");
		return -1;  //fail
	}
	//尾部删除
	list->len --;
	
}
int search_sub(seqlist list,int sub)
{
	if(list==NULL||empty_seqlist(list)||sub<0||sub>=list->len)
	{
		printf("search fail\n");
		return -1;
	}
	//查找就是输出
	printf("要查找的元素是:%d\n",list->data[sub]);
	
}
int updata_sub(seqlist list,int sub,datatype e)
{
	if(list==NULL||empty_seqlist(list)||sub<0||sub>=list->len)
	{
		printf("updata fail\n");
		return -1;
	}
	//修改重新赋值
	list->data[sub]=e;
}
int insert_sub(seqlist list,int sub,datatype e)
{
	if(list==NULL||full_seqlist(list)||sub<0||sub > list->len)//这个条件改了!list->len
	{
		printf("insert_sub fail\n");
		return -1;
	}
	//按下标插入
	for(int i=list->len-1;i>=sub;i--)
	{
		list->data[i+1]=list->data[i];
	}
		list->data[sub]=e;
		list->len++;
		return 0;
}
int delete_sub(seqlist list,int sub)
{
	if(list==NULL||empty_seqlist(list)||sub<0||sub>=list->len)
	{
		printf("delete_sub fail\n");
		return -1;
	}
	//按下标删除
	for(int i = sub+1;i < list->len;i++)
	{
		list->data[i-1]=list->data[i];
	}
	list->len--;
	return 0;
}
//按元素查找
int search_data(seqlist list,datatype e)
{
	if(list==NULL||empty_seqlist(list))
	{
		printf("search_data fail\n");
	}
	//循环顺序表比较
	for(int i=0;i<list->len;i++)
	{
		if(e==list->data[i])
		{
			return i;
		}
	}
	return -1;
}
//按元素修改 e被修改的值  key要修的
int updata_data(seqlist list,datatype e,datatype key)
{
	int sub=search_data(list,e);
	if(sub==-1)
	{
		printf("updata_data fail\n");
		return -1;
	}
	updata_sub(list,sub,key);
	return 0;
}
//按元素删除
int delete_data(seqlist list,datatype e)
{
	int sub=search_data(list,e);
	if(sub==-1)
	{
		printf("delete_data fail\n");
		return -1;
	}
	delete_sub(list,sub);
	return 0;
}
//顺序表去重
void delete_re(seqlist list)
{
	if(list==NULL||empty_seqlist(list))
	{
		return;
	}
	for(int i=0;i<list->len-1;i++)
	{
		for(int j=i+1;j<list->len;j++)
		{
			if(list->data[i]==list->data[j])
			{
				delete_sub(list,j);
				j--;
			}
		}
	}
}
//按元素后继插入 查找的元素是e 插入的元素是key
int insert_data(seqlist list,datatype e,datatype key)
{
	int sub=search_data(list,e);
	if(sub==-1)
	{
		printf("insert_data fail\n");
		return -1;
	}
    insert_sub(list,sub+1,key)
	
	return 0;
}


//顺序表排序
int bubble_seqlist(seqlist list)
{
	for(int i=1;i<list->len;i++)
	{
		for(int j=0;j< (list->len)-i;j++)
		{
			if(list->data[j]>list->data[j+1])
			{
				int t;
				t=list->data[j];
				list->data[j]=list->data[j+1];
				list->data[j+1]=t;
			}
		}
	}
	return 0;
}
//顺序表合并
void merge_seqlist(seqlist a,seqlist b,seqlist c)
{
	int i,j,k;
	i=0;j=0;k=0;
	bubble_seqlist(a);
	bubble_seqlist(b);
	while(i<a->len && j<b->len && k< MAXSIZE)
	{
		if(a->data[i]<=b->data[j])
		{
			c->data[k]=a->data[i];
			k++;
			i++;
			c->len++;
		}
		else
		{
			c->data[k]=b->data[j];
			k++;
			j++;
			c->len++;
		}
	}
	while(i < a->len&&k<MAXSIZE)
	{
		c->data[k] = a->data[i];
		k++;
		i++;
		c->len++;
	}
	while(j < b->len&&k<MAXSIZE)
	{
		c->data[k]=b->data[j];
		k++;
		j++;
		c->len++;
	}
}

void output_seqlist(seqlist list)
{
	if(list == NULL||empty_seqlist(list))
	{
		printf("bian li fail\n");
		return;
	}
	for(int i=0;i<list->len;i++)
	{
		printf("%d\t",list->data[i]);
	}
	puts("");
}
//顺序表空间释放
seqlist free_space(seqlist list)
{
	if(list==NULL)
		return NULL;
	free(list);
	list=NULL;
	return list;
}

.main

#include"1.h"
int main(int argc, const char *argv[])
{
	int n;
	datatype e;
/*	seqlist list =creat();
	printf("请输入元素的个数::");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("请输入想插入的元素:");
		scanf("%d",&e);
		insert_rear(list,e);	
	}
	output_seqlist(list);

	delete_rear(list);
	printf("删除尾元素之后:");
	output_seqlist(list);

	int sub;
	printf("请输入要查找元素的下标:");
	scanf("%d",&sub);
	search_sub(list,sub);

	printf("请输入要修改元素的下标:");
	scanf("%d",&sub);
	printf("请输入要修改的元素是:");
	scanf("%d",&e);
	updata_sub(list,sub,e);
	output_seqlist(list);

	printf("please puts insert_sub xiabiao:");
	scanf("%d",&sub);
	printf("please puts insert_sub yuansu:");
	scanf("%d",&e);
	insert_sub(list,sub,e);
	output_seqlist(list);


	
	printf("please puts delete_sub xioabiao:");
	scanf("%d",&sub);
	delete_sub(list,sub);
	printf("delete_sub after:");
	output_seqlist(list);

	printf("请输入查找的元素:");
	scanf("%d",&e);
	int f=search_data(list,e);
	if(f==-1)
	{
	printf("search fail\n");
	}
	else
	{
		printf("在下标%d输出\n",f);
	}
	datatype key;
	
	printf("please puts 被updata_data:");
	scanf("%d",&e);
	printf("please puts 要 updata_data:");
	scanf("%d",&key);
	updata_data(list,e,key);
	output_seqlist(list);
	
	printf("please puts delete_data:");
	scanf("%d",&e);
	delete_data(list,e);

	delete_re(list);
	printf("去重后的元素:");
	output_seqlist(list);

	printf("在哪个元素后插入后继元素:");
	scanf("%d",&e);
	printf("要插入的后继元素是:");
	scanf("%d",&key);
	insert_data(list,e,key);
	
	bubble_seqlist(list);
	output_seqlist(list);
*/
	int sub;
	seqlist a=creat();
	seqlist b=creat();
	seqlist c=creat();
	c->data[c->len]=0;
	printf("请输入a里面元素的个数:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("请输入a插入的元素:");
		scanf("%d",&e);
		insert_sub(a,i,e);	
	}

	printf("请输入b里面元素的个数:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("请输入b插入的元素:");
		scanf("%d",&e);
		insert_sub(b,i,e);	
	}
	merge_seqlist(a,b,c);
	output_seqlist(a);
	output_seqlist(b);
	output_seqlist(c);

	/*list=free_space(list);
*/


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值