一、顺序表结构体定义
#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;
}
14. 实现顺序表去重
int delete_repeat(Seqlist *list) //实现顺序表去重
{
if(NULL==list||empty_list(list))
return -1;
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_list(j,list);
j--; //防止漏删
}
}
}
return 0;
}
15. 实现顺序表排序
int sort_list(Seqlist *list) //实现顺序表排序
{
int count;
if(NULL==list||empty_list(list))
return -1;
for(int i=1;i<list->len;i++)
{
count=0;
for(int j=0;j<list->len-i;j++)
{
if(list->data[j]>list->data[j+1])
{
datatype temp=list->data[j];list->data[j]=list->data[j+1];list->data[j+1]=temp;
count++;
}
}
if(count==0)break;
}
return 0;
}
16. 实现顺序表合并
int combine_list(Seqlist *list,Seqlist *list_b,Seqlist *list_c) //顺序表合并
{
int p=0,q=0;
while(p<list->len&&q<list_b->len)
{
if(list->data[p]<=list_b->data[q])
list_c->data[list_c->len++]=list->data[p++];
else
list_c->data[list_c->len++]=list_b->data[q++];
}
while(p<list->len)
{
list_c->data[list_c->len++]=list->data[p++];
}
while(q<list_b->len)
{
list_c->data[list_c->len++]=list_b->data[q++];
}
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); //实现任意元素删除
int delete_repeat(Seqlist *list); //实现顺序表去重
int sort_list(Seqlist *list); //实现顺序表排序
int combine_list(Seqlist *list,Seqlist *list_b,Seqlist *list_c); //顺序表合并
#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;
}
int delete_repeat(Seqlist *list) //实现顺序表去重
{
if(NULL==list||empty_list(list))
return -1;
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_list(j,list);
j--; //防止漏删
}
}
}
return 0;
}
int sort_list(Seqlist *list) //实现顺序表排序
{
int count;
if(NULL==list||empty_list(list))
return -1;
for(int i=1;i<list->len;i++)
{
count=0;
for(int j=0;j<list->len-i;j++)
{
if(list->data[j]>list->data[j+1])
{
datatype temp=list->data[j];list->data[j]=list->data[j+1];list->data[j+1]=temp;
count++;
}
}
if(count==0)break;
}
return 0;
}
int combine_list(Seqlist *list,Seqlist *list_b,Seqlist *list_c) //顺序表合并
{
int p=0,q=0;
while(p<list->len&&q<list_b->len)
{
if(list->data[p]<=list_b->data[q])
list_c->data[list_c->len++]=list->data[p++];
else
list_c->data[list_c->len++]=list_b->data[q++];
}
while(p<list->len)
{
list_c->data[list_c->len++]=list->data[p++];
}
while(q<list_b->len)
{
list_c->data[list_c->len++]=list_b->data[q++];
}
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);
delete_repeat(list);
sort_list(list);
Output(list);
Seqlist *list_b=Request_space();
printf("please enter n:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("please enter a value:");
scanf("%d",&value);
insert_rear(value,list_b);
}
Output(list);
Seqlist *list_c=Request_space();
combine_list(list,list_b,list_c);
Output(list_c);
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-579501.html
文章来源:https://www.toymoban.com/news/detail-579501.html
到了这里,关于数据结构顺序表,实现增删改查的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!