数据结构基本概念、线性表、顺序表

一、头文件 head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#include 
#include 
#include 
#define MAXSIZE 8

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

Seqlist * Request_space(void);
int full_list(Seqlist *list);  //判断顺序表长度是否为满
int empty_list(Seqlist *list);  //判断顺序表长度是否为空
int insert_rear(datatype value,Seqlist *list);  //实现尾插
void Output(Seqlist *list);  //实现输出
int delete_rear(Seqlist *list);  //实现尾删
datatype search_by_sub(int sub,Seqlist *list);  //实现任意下标查找
int modify_list(int sub,datatype modify,Seqlist *list);  //实现任意下标修改
int insert_list(int sub,datatype value,Seqlist *list);  //实现任意下标插入
int delete_list(int sub,Seqlist *list);  //实现任意下标删除
int search_by_element(datatype value,Seqlist *list);  //实现任意元素查找
int modify_by_element(datatype value,datatype modify,Seqlist *list);  //实现任意元素修改
int insert_by_element(datatype value,datatype modify,Seqlist *list);  //实现任意元素插入
int delete_by_element(datatype value,Seqlist *list);  //实现任意元素删除

#endif


二、自定义函数 fun.c

#include "head.h"

Seqlist *Request_space(void) //创建空间
{
	Seqlist *list=(Seqlist *)malloc(sizeof(Seqlist));
	if(NULL==list)
		return NULL;
	list->len=0;
	memset(list->data,0,sizeof(datatype)*MAXSIZE);
	return list;
}
int empty_list(Seqlist *list)  //判断顺序表长度是否为空
{	
	return list->len==0?-1:0;
}
int full_list(Seqlist *list)  //判断顺序表长度是否为满
{
	return list->len==MAXSIZE?-1:0;
}
int insert_rear(datatype value,Seqlist *list)  //实现尾插
{
	if(NULL==list||full_list(list))
		return -1;
	list->data[list->len]=value;
	list->len++;
	return 0;
}
void Output(Seqlist *list)  //实现输出
{
	if(NULL==list||empty_list(list))
		return ;
	for(int i=0;i<list->len;i++)
	printf("%d ",list->data[i]);
	puts("");
}
int delete_rear(Seqlist *list)  //实现尾删
{
	if(NULL==list||empty_list(list))
		return -1;
	list->len--;
	return 0;
}
datatype search_by_sub(int sub,Seqlist *list)  //实现任意下标查找
{
	if(NULL==list||empty_list(list)||sub<0||sub>=list->len)
		return -1;
	return list->data[sub];
}
int modify_list(int sub,datatype modify,Seqlist *list)  //实现任意下标修改
{
	if(NULL==list||empty_list(list)||sub<0||sub>=list->len)
		return -1;
	list->data[sub]=modify;
	return 0;
}
int insert_list(int sub,datatype value,Seqlist *list)  //实现任意下标插入
{
	if(NULL==list||full_list(list)||sub<0||sub>list->len)
		return -1;
	for(int i=list->len-1;i>=sub;i--)
	{
		list->data[i+1]=list->data[i];
	}
	list->data[sub]=value;
	list->len++;
	return 0;
}
int delete_list(int sub,Seqlist *list)  //实现任意下标删除
{
	if(NULL==list||empty_list(list)||sub<0||sub>=list->len)
		return -1;
	for(int i=sub;i<list->len-1;i++)
	{
		list->data[i]=list->data[i+1];
	}
	list->len--;
	return 0;
}
int modify_by_element(datatype value,datatype modify,Seqlist *list)  //实现任意元素修改
{
	if(NULL==list||empty_list(list))
		return -1;
	for(int i=0;i<list->len;i++)
	{
		if(list->data[i]==value)
		{
			list->data[i]=modify;
			return 0;
		}
	}
	printf("the value was not found\n");
	return -1;
}
int search_by_element(datatype value,Seqlist *list)  //实现任意元素查找
{
	if(NULL==list||empty_list(list))
		return -1;
	for(int i=0;i<list->len;i++)
	{
		if(value==list->data[i])
			return i;
	}
	printf("the value was not found\n");
	return -1;
}
int insert_by_element(datatype value,datatype modify,Seqlist *list)  //实现任意元素插入
{
	int sub=search_by_element(value,list);
	if(sub==-1)
		return -1;
	insert_list(sub,modify,list);
	return 0;
}
int delete_by_element(datatype value,Seqlist *list)  //实现任意元素删除
{
	int sub=search_by_element(value,list);
	if(sub==-1)
		return -1;
	delete_list(sub,list);
	return 0;
}

三、主函数 main.c

#include "head.h"
int main(int argc, const char *argv[])
{
	Seqlist *list=Request_space();
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	datatype value;//定义变量--存储的值
	for(int i=0;i<n;i++)
	{
		printf("please enter a value:");
		scanf("%d",&value);
		insert_rear(value,list);
	}
	Output(list);
	int sub;//定义变量--下标
 	printf("please enter a sub:");
	scanf("%d",&sub);
	printf("%d\n",search_by_sub(sub,list));
	
	datatype modify;//定义变量--需要修改的值
	printf("please enter a sub you want to modify:");
	scanf("%d",&sub);
	printf("please enter a value you want to modify:");
	scanf("%d",&modify);
	modify_list(sub,modify,list);
	Output(list);

	printf("please enter a sub you want to insert:");
	scanf("%d",&sub);
	printf("please enter a value you want to insert:");
	scanf("%d",&value);
	insert_list(sub,value,list);
	Output(list);

	printf("please enter a sub you want to delete:");
	scanf("%d",&sub);
	delete_list(sub,list);
	Output(list);
	printf("please enter the value you want to find:");
	scanf("%d",&value);
	printf("please enter a value you want to insert:");
	scanf("%d",&modify);
	insert_by_element(value,modify,list);
	Output(list);

	printf("please enter the value you want to find:");
	scanf("%d",&value);
	delete_by_element(value,list);
	Output(list);
	return 0;
}

数据结构基本概念、线性表、顺序表_第1张图片

你可能感兴趣的:(数据结构练习,#,数据结构练习(7月17),数据结构)