数据结构作业2024.1.23

顺序表操作流程(需要封装的函数):

1.在堆区创建顺序表

2.顺序表尾插

3.顺序表尾删遍历输出

4.顺序表按任意下表修改

5.顺序表按任意下表删除

6.顺序表按任意元素查找

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

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

9.顺序表排序

10.顺序表释放空间

头文件:

#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 empty(sqlist*list);
int output(sqlist*list);
int inster_rear(datatype element,sqlist*list);
void amend_index(sqlist*list,int index,datatype element);
void delete_index(sqlist*list,int index;);
int search_element(sqlist*list,int key);
void update_element(sqlist*list,int key,datatype element);
void delete_element(sqlist*list,int key);
int bubble(sqlist*list);
sqlist *free_list(sqlist*list);

#endif

main函数

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

封装函数

#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;
}
//判断是否为0
int empty(sqlist*list)
{
	return list->len==0?FALSE:SUCCESS;
}
//尾插
int inster_rear(datatype element,sqlist*list)
{
	if(NULL==list || full(list))
		return FALSE;
	list->data[list->len++]=element;
	return SUCCESS;
}
//遍历顺序表输出
int output(sqlist*list)
{
	if(NULL==list || empty(list))
		return FALSE;
	for(int i=0;ilen;i++)
	{
		printf("%-4d",list->data[i]);
	}
	puts("");

}
//按下标修改
void amend_index(sqlist*list,int index,datatype element)
{

	if(NULL==list || empty(list) || index<0 || index>=list->len)
	{	puts("ERROR");
		return;
	}
	list->data[index]=element;
}
//按下标删除
void delete_index(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 search_element(sqlist*list,int key)
{
	int index=0;
	if(NULL==list || empty(list))
	{	puts("ERROR");
		return -1;
	}
	for(int i=0;ilen;i++)
	{
		if(key==list->data[i])
		{   index=i;
			return index;
		}
	}

}
//按元素修改
void update_element(sqlist*list,int key,datatype element)
{
	int index=search_element(list,key);
	amend_index(list,index,element);
}
//按元素删除
void delete_element(sqlist*list,int key)
{
	int index=search_element(list,key);
	delete_index(list,index);
}
//顺序表排序
int bubble(sqlist*list)
{
	if(NULL==list || empty(list))
		return FALSE;
	for(int i=1;ilen;i++)
	{
		for(int j=0;jlen-i;j++)
		{
			if(list->data[j]>list->data[j+1])
			{
				datatype t=list->data[j];
				list->data[j]=list->data[j+1];
				list->data[j+1]=t;
				j--;
			}
		}
	}
}
//释放空间
sqlist *free_list(sqlist*list)
{
	if(NULL==list)
		return NULL;
	free(list);
	list=NULL;
	return list;
}

数据结构作业2024.1.23_第1张图片

思维导图

你可能感兴趣的:(数据结构)