作业2024/1/23

10.顺序表按任意元素修改

参数:list, 查找的值key,修改的值element

1.根据key查找对应的下表index

search_key()

2.根据index修改为element

update_index();

test.c里的代码
int find(sqlist*list,int key)
{
	if(NULL == list || empty(list))
	{
		return FALSE;
	}
	for(int i=0;ilen;i++)
	{
		if(list->data[i]==key)
		{
			return i;
		}
	}
}

//顺序表按任意元素修改
void modify(sqlist*list,int key,int element)
{	
	int index=find(list,key);

	amend_index(list,index,element);
}



1.c里的代码
	int key;
	printf("please enter key:");
	scanf("%d",&key);
	printf("please enter element:");
	scanf("%d",&element);
	modify(list,key,element);
	output(list);
	return 0;

作业2024/1/23_第1张图片

11.顺序表按任意元素删除

参数:list ,删除的元素key

1.根据key查找对应的下表index

search_key()

2.根据index删除

delete_index();

//按任意下标删除
void delete(sqlist*list,int index)
{
	if(NULL==list || empty(list) || index<0 || index>=list->len)
	{	puts("ERROR");
		return;
	}

	for(int i=index+1;ilen;i++)
	{
		list->data[i-1]=list->data[i];
	}
	list->len--;
}

//顺序表按照任意元素删除
void Edelete(sqlist*list,int key)
{
	int index=find(list,key);
	delete(list,index);
	
}




	int key;
	printf("please enter key:");
	scanf("%d",&key);
	Edelete(list,key);
	output(list);
	return 0;

作业2024/1/23_第2张图片

12.顺序表排序

//顺序表排序
void bubble(sqlist*list)
{
	for(int i=0;ilen;i++)
	{
	for(int j=0;j<(list->len)-i-1;j++)
		{
			if(list->data[j]>list->data[j+1])
			{	
				int t=list->data[j];
				list->data[j]=list->data[j+1];
				list->data[j+1]=t;
			}
		}
	}

}

作业2024/1/23_第3张图片

13.free释放

作业2024/1/23_第4张图片
 

1.函数封装

#include "head.h"
//创建顺序表在堆区
sqlist *creat()
{
	sqlist *list=(sqlist *)malloc(sizeof(sqlist));
	if(NULL==list)
		return NULL;
	//对数据元素清零
	memset(list->data,0,sizeof(list->data));
	//对顺序表长度清0
	list->len=0;
	return list;
}
//判断是否为满
int full(sqlist*list)
{
	return list->len==MAXSIZE?FALSE:SUCCESS;
}
//尾插
int inster_rear(datatype element,sqlist*list)
{
	if(NULL==list || full(list))
			return FALSE;
			list->data[list->len++]=element;
			return SUCCESS;
}
//判断是否为0
int empty(sqlist*list)
{
	return list->len==0?FALSE:SUCCESS;
}
//输出
int output(sqlist*list)
{
	if(NULL==list || empty(list))
			return FALSE;
			for(int i=0;ilen;i++)
			{
			printf("%-4d",list->data[i]);
			}
			puts("");

}
//尾删
int delete_rear(sqlist*list,int index)
{
	if(NULL==list || empty(list))
	return FALSE;
	list->len--;
	return SUCCESS;
}
//按下标查找
void search_index(sqlist*list,int index)
{
	if(NULL==list || empty(list) || index<0 || index>=list->len)
	{	puts("ERROR");
		return;
	}
	printf("search by index element is %d\n",list->data[index]);

}
//按下标修改
void amend_index(sqlist*list,int index,int element)
{
	
	if(NULL==list || empty(list) || index<0 || index>=list->len)
	{	puts("ERROR");
		return;
	}
    list->data[index]=element;
}
//按下标插入
void inster(sqlist*list,int index,int element)
{
	if(NULL==list || full(list) || index<0 || index>=list->len)
	{	puts("ERROR");
		return;
	}
    for(int i=list->len-1;i>=index;i--)
	{
		list->data[i+1]=list->data[i];
	}
	list->data[index]=element;
	list->len++;
}
//按任意下标删除
void delete(sqlist*list,int index)
{
	if(NULL==list || empty(list) || index<0 || index>=list->len)
	{	puts("ERROR");
		return;
	}

	for(int i=index+1;ilen;i++)
	{
		list->data[i-1]=list->data[i];
	}
	list->len--;
}
//按任意元素查找

int find(sqlist*list,int key)
{
	if(NULL == list || empty(list))
	{
		return FALSE;
	}
	for(int i=0;ilen;i++)
	{
		if(list->data[i]==key)
		{
			return i;
		}
	}
}

/*按照顺序表去重
void sdelete(sqlist*list,int element)
{
	
	if(NULL==list || empty(list) )
	{	puts("ERROR");
		return;
	}
	for(int i=0;ilen-1;i++)
	{
		for(int j=i+1;jlen;j++)
		{
		if(list->data[i]==list->data[j])
			delete(list,j);
		j--;
		}
	
	}
}
*/
//顺序表按任意元素修改
void modify(sqlist*list,int key,int element)
{	
	int index=find(list,key);

	amend_index(list,index,element);}

//顺序表按照任意元素删除
void Edelete(sqlist*list,int key)
{
	int index=find(list,key);
	delete(list,index);
	
}
//顺序表排序
void bubble(sqlist*list)
{
	for(int i=0;ilen;i++)
	{
	for(int j=0;j<(list->len)-i-1;j++)
		{
			if(list->data[j]>list->data[j+1])
			{	
				int t=list->data[j];
				list->data[j]=list->data[j+1];
				list->data[j+1]=t;
			}
		}
	}

}
//顺序表释放空间
void freedom(sqlist*list)
{
	if(list=NULL)
		return NULL;
	free(list);
	list=NULL;
}

2.main.c
 

#include "head.h"
int main(int argc, const char *argv[])
{
	sqlist *list=creat();
	//循环调用尾插函数
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	datatype element;
	for(int i=0;i

3.head.h

#ifndef _HEAD_H_
#define _HEAD_H_
#include 
#include 
#include 
#include 
#include 
enum num
{
	FALSE=-1,
	SUCCESS
};
#define MAXSIZE 7
typedef int datatype;
//定义顺序表
typedef struct  SQLIST
{
	//数据元素
	datatype data[MAXSIZE];
	//顺序表长度
	int len;
}sqlist;
sqlist *creat();
int full(sqlist*list);
int inster_rear(datatype element,sqlist*list);
int empty(sqlist*list);
int output(sqlist*list);
int delete_rear(sqlist*list,int index);
void search_index(sqlist*list,int index);
void amend_index(sqlist*list,int element,int index);
void inster(sqlist*list,int index,int element);
void delete(sqlist*list,int index);
int  find(sqlist*list ,int element);
void sdelete(sqlist*list,int element);
void  modify(sqlist*list,int key,int element);
void Edelete(sqlist*list,int key);
void bubble(sqlist*list);
void freedom(sqlist*list);
#endif

作业2024/1/23_第5张图片

你可能感兴趣的:(windows)