【数据结构】day3

#ifndef __1_H__
#define __1_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 10
typedef int datatype;//给int一个datatype别名



typedef struct
{	int len;
	//数据元素
	datatype data[MAXSIZE];
	//顺序表长度

}DD,*dd;


void output(dd list);
int insert_t(dd list,datatype e);
int empty_l(dd list);
int full_l(dd list);
int delete_t(dd list);
int search_s(dd list,int sub);
dd create();
int chang_l(dd list,int sub,datatype e);
int insert_s(dd list,int sub,datatype e);
int delete_s(dd list,int sub);
int search_data(dd list,datatype e);
int chang_data(dd list,datatype e,datatype key);
int delete_data(dd list,datatype e);
int delete_cf(dd list);
void sort(dd list);
int insert_next(dd list, int sub,datatype e);
void merge_sort(dd list, dd list1,dd list2);
dd free_l(dd list);
#endif

#include "1.h"

int main(int argc, const char *argv[])
{

//创建
	dd list = create();
	//尾插
	int n;
	datatype e;
	printf("input u want insert number\n");
	scanf(" %d",&n);
	for(int i = 0; i < n; i++)
	{
		printf("input data\n");
		scanf("%d",&e);
		insert_t(list,e);
	}

	
	//排序
//	sort(list);	
	//输出
	output(list);
//	output(list1);
	//按下标查找
	
	int k;
	printf("input want search_s num");
	scanf("%d",&k);
	search_s(list,k);

	//按下标修改
	
	printf("u want chang_l number\n");
	scanf("%d",&k);
	printf("u want chang_l data\n");
	scanf("%d",&e);
	chang_l(list,k,e);
	output(list);

	//按下标插入
	
	printf("u want insert_s number\n");
	scanf("%d",&k);
	printf("u want insert_s data\n");
	scanf("%d",&e);
	insert_s(list,k,e);
	output(list);
	//按下标删除
	
	printf("u want delete_s number\n");
	scanf("%d",&k);
	delete_s(list,k);
	output(list);

	//按元素查找
	printf("u want search_data data \n");
	scanf("%d",&e);
	int flag = search_data(list,e);
	printf("the data is the %d number\n",flag);

	//按元素修改
	datatype key;
	printf("u want chang_data data \n");
	scanf("%d",&e);
	printf("u want chang_data new data \n");
	scanf("%d",&key);
	chang_data(list,e,key);
	output(list);

	//尾删
	printf("want delete num\n");
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		delete_t(list);
	}
	//输出
	output(list);

	//去重
	delete_cf(list);
	output(list);

	//按元素删除
	printf("input u want delete_data\n");
	scanf("%d",&e);
	delete_data(list,e);
	output(list);
	
	//按下标后继插入

	int k = 0;
	printf("input u want insert_next number\n");
	scanf("%d",&k);
	printf("input u want insert_next data\n");
	scanf("%d",&e);
	insert_next(list,k,e);
	output(list);


	DD list3 = {5,3,9,5,7,1};
	
	sort(&list3);
	//顺序表合并
	DD list2;
	list2.len = 0;
	DD list1 = {5,3,9,5,7,1};	
	sort(&list1);
	output(&list1);
	output(&list3);
	merge_sort(&list3,&list1,&list2);
	output(&list2);
	
	//释放
	list = free_l(list);
	if(!list) printf("yes");


	return 0;
}

#include "1.h"
//创建
dd create()
{
	dd list = (dd) malloc (sizeof (DD));
	if(!list) return NULL;
	list->len = 0;
	return list;
}
//判满

int full_l(dd list)
{
	return list->len == MAXSIZE?-1:0;
}

//判空

int empty_l(dd list)
{
	return list->len == 0?-1:0;
}


//尾插

int insert_t(dd list,datatype e)
{
	if(list == NULL || full_l(list))
	{
		printf("insert_t failed\n");
		return -1;
	}
	list->data[list->len] = e;
	list->len++;
	return 0;
}


//输出

void output(dd list)
{
	if(list == NULL||empty_l(list))
	{
		printf("output failed\n");
		return;
	}
	for(int i = 0; i < list->len; i++)
		printf("%d\t",list->data[i]);
	putchar(10);
}

//尾删
int delete_t(dd list)
{
	if(list == NULL || empty_l(list))
	{
		printf("delete_t failed\n");
		return -1;
	}
	list->len --;
	return 0;
}


//按下标查找

int search_s(dd list,int sub)
{
	if(list == NULL || empty_l(list) || sub<0 || sub >= list->len)
	{
		printf("search_s failed\n");
		return -1;
	}
	printf("the data is %d\n",list->data[sub]);
	return 0;
}

//按下标修改

int chang_l(dd list,int sub,datatype e)
{
	if(list == NULL || empty_l(list) || sub < 0 || sub >= list->len)
	{
		printf("chang_l failed\n");
		return -1;
	}
	list->data[sub] = e;
	return 0;
}

//按下标插入

int insert_s(dd list,int sub,datatype e)
{
	if(!list || full_l(list) || sub < 0 || sub > list->len)
	{
		printf("insert_s failed\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_s(dd list,int sub)
{	
	if(list == NULL || empty_l(list) || sub < 0 || sub >= list->len)
	{
		printf("delete_s failed\n");
		return -1;
	}
	for(int i = sub;i < list->len; i++)
		list->data[i] = list->data[i+1];
	list->len --;
	return 0;
}

//按元素查找

int search_data(dd list,datatype e)
{
	if(!list || empty_l(list))
	{
		printf("search_data failed\n");
		return -1;
	}
	for(int i = 0; i < list->len; i++)
	{
		if(list->data[i] == e)
			return i;
	}

	return -1;
}

//按元素修改

int chang_data(dd list,datatype e,datatype key)
{
	int flag = search_data(list,e);
	if(flag != -1)
	{
		chang_l(list,flag,key);
	}
	return -1;



}


//按元素删除

int delete_data(dd list,datatype e)
{
	int flag = search_data(list,e);
	if(flag!= -1)
		delete_s(list,flag);
	return 0;
}



//去重

int delete_cf(dd list)
{
	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_s(list,j);
				j--;
			}
		}
	}
}

//归并排序
void merge_sort(dd list, dd list1,dd list2)
{
	int p = 0, q = 0;
	while(p < list->len && q < list1->len && list2->len < MAXSIZE)
	{
		if(list->data[p] <= list1->data[q])
			list2->data[list2->len++] = list->data[p++];
		else
			list2->data[list2->len++] = list1->data[q++];
	}
	while(p < list->len && list2->len < MAXSIZE)
			list2->data[list2->len++] = list->data[p++];
	while(q < list1->len && list2->len < MAXSIZE)
			list2->data[list2->len++] = list1->data[q++];
}


//按下标后继插入

int insert_next(dd list, int sub,datatype e)
{
	if(!list || full_l(list) || sub < 0 || sub > list->len)
	{
		printf("insert_next failed\n");
		return -1;
	}
	insert_s(list,sub+1,e);
	return 0;
}


//排序

void sort(dd list)
{
	for(int i = 1; i < list->len; i++)
	{
		int count = 0;
		for(int j = 0; j < (list->len)-i; j++)
		{
			if((list->data[j]) < (list->data[j+1]))
			{
				datatype t;
				t = (list->data[j]);
				(list->data[j]) = (list->data[j+1]); 
				 (list->data[j+1]) = t;
				 count++;
			}
			if(!count) break;
		}
	}
}


//释放

dd free_l(dd list)
{
	if(!list) return NULL;
	for(int i = 0; i < list->len; i++)
	{
		delete_s(list,i);
	}
	free(list);
	list = NULL;
	return list;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值