一、顺序表结构体定义
#define MAXSIZE 8 //定义常量MAXSIZE,表示数据元素的最大个数为8
typedef int datatype; //重定义int类型,分别后期修改顺序表中存储的数据类型
typedef struct
{
int len; //顺序表长度
datatype data[MAXSIZE]; //数据元素
}Seqlist;
二、顺序表创建空间
Seqlist *Request_space(void) //创建空间
{
Seqlist *list=(Seqlist *)malloc(sizeof(Seqlist));
if(NULL==list)
return NULL;
list->len=0; //顺序表长度初始化为0
memset(list->data,0,sizeof(datatype)*MAXSIZE); //数据元素初始化为0
return list;
}
三、实现顺序表增删改查
1. 判断顺序表是否为空
int empty_list(Seqlist *list) //判断顺序表长度是否为空
{
return list->len==0?-1:0;
}
2. 判断顺序表是否为满
int full_list(Seqlist *list) //判断顺序表长度是否为满
{
return list->len==MAXSIZE?-1:0;
}
3. 实现输出
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("");
}
4. 顺序表尾插
int insert_rear(datatype value,Seqlist *list) //实现尾插
{
if(NULL==list||full_list(list))
return -1;
list->data[list->len]=value;
list->len++;
return 0;
}
5. 顺序表尾删
int delete_rear(Seqlist *list) //实现尾删
{
if(NULL==list||empty_list(list))
return -1;
list->len--;
return 0;
}
6. 实现任意下表查找
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];
}
7. 实现任意下标修改
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;
}
8. 实现任意下标插入
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;
}
9. 实现任意下标删除
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;
}
10. 实现任意元素修改
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;
}
11. 实现任意元素查找
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;
}
12. 实现任意元素插入
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;
}
13. 实现任意元素删除
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;
}
四、分布编译实现顺序表增删改查【完整代码】
头文件 head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
}