简介
顺序表是一种线性表的存储结构,它使用一组地址连续的存储单元依次存储线性表中的元素。在C语言中,顺序表通常通过数组来实现。下面是一个简单的顺序表的C语言实现,包括初始化、插入、删除、查找等基本操作。
C语言实现
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 100 //顺序表最大容量
typedef int ElemType; //顺序表存储数据类型
//顺序表定义
typedef struct{
ElemType *data;
int length;
}SeqList;
/*****************************************************************
* 函数功能 : 初始化顺序表
* 参数 : void
* 返回值 : 成功:SeqList类型指针,失败:NULL
******************************************************************/
SeqList* init_seqlist(void)
{
SeqList *L = (SeqList*)malloc(sizeof(SeqList));
if(NULL == L)
{
perror("malloc Error: ");
return NULL;
}
L->data = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
if(NULL == L->data)
{
free(L);
L = NULL;
perror("malloc Error: ");
return NULL;
}
L->length = 0;
return L;
}
/*****************************************************************
* 函数功能 : 销毁顺序表
* 参数 : L(in) -- 顺序表
* 返回值 : void
******************************************************************/
void destroy_seqlist(SeqList *L)
{
if(NULL != L)
{
if(NULL != L->data)
{
free(L->data);
L->data = NULL;
}
free(L);
L = NULL;
}
}
/*****************************************************************
* 函数功能 : 判断顺序表是否为空
* 参数 : L(in) -- 顺序表
* 返回值 : 空:true,非空:false
******************************************************************/
bool isEmpty(SeqList *L)
{
if(0 == L->length)
{
printf("Seqlist is empty\n");
return true;
}
return false;
}
/*****************************************************************
* 函数功能 : 判断顺序表是否满了
* 参数 : L(in) -- 顺序表
* 返回值 : 满:true,没满:false
******************************************************************/
bool isFull(SeqList *L)
{
if(L->length >= MAXSIZE)
{
printf("Seqlist is full\n");
return true;
}
return false;
}
/*****************************************************************
* 函数功能 : 判断指定位置是否溢出
* 参数 : L(in) -- 顺序表
* 参数 : pos(in) -- 指定位置,正确范围[0,L->length)
* 返回值 : 溢出:true,没溢出:false
******************************************************************/
bool isPosWrong(SeqList *L, int pos)
{
if (pos < 0 || pos >= L->length)
{
printf("The specified pos wrong\n");
return true;
}
return false;
}
/*****************************************************************
* 函数功能 : 尾部添加元素
* 参数 : L(in) -- 顺序表
* 参数 : elem(in) -- 插入的元素
* 返回值 : 成功:0,失败:-1
******************************************************************/
int append_elme(SeqList *L, ElemType elem)
{
if (true == isFull(L))
{
return -1;
}
L->data[L->length] = elem;
L->length++;
return 0;
}
/*****************************************************************
* 函数功能 : 指定位置插入元素
* 参数 : L(in) -- 顺序表
* 参数 : pos(in) -- 指定位置,正确范围[0,L->length)
* 参数 : elem(in) -- 插入的元素
* 返回值 : 成功:0,失败:-1
******************************************************************/
int insert_elme(SeqList *L, int pos, ElemType elem)
{
if (true == isFull(L) || true == isEmpty(L) || true == isPosWrong(L, pos))
{
return -1;
}
//元素后移
for (int i = L->length-1; i >= pos; i--)
{
L->data[i+1] = L->data[i];
}
L->data[pos] = elem; //插入的元素
L->length++;
return 0;
}
/*****************************************************************
* 函数功能 : 删除指定位置元素
* 参数 : L(in) -- 顺序表
* 参数 : pos(in) -- 指定位置,正确范围[0,L->length)
* 参数 : elem(out) -- 删除的元素
* 返回值 : 成功:0,失败:-1
******************************************************************/
int delete_elem(SeqList *L, int pos, ElemType *elem)
{
if(true == isEmpty(L) || true == isPosWrong(L, pos) )
{
return -1;
}
*elem = L->data[pos]; //删除的元素
//元素前移
for (int i = pos+1; i < L->length; i++)
{
L->data[i-1] = L->data[i];
}
L->length--;
return 0;
}
/*****************************************************************
* 函数功能 : 修改指定位置元素
* 参数 : L(in) -- 顺序表
* 参数 : pos(in) -- 指定位置,正确范围[0,L->length)
* 参数 : elem(in) -- 替换的值
* 返回值 : 成功:0,失败:-1
******************************************************************/
int modify_elem(SeqList *L, int pos, ElemType elem)
{
if(true == isEmpty(L) || true == isPosWrong(L, pos) )
{
return -1;
}
L->data[pos] = elem;
return 0;
}
/*****************************************************************
* 函数功能 : 查找数据位置
* 参数 : L(in) -- 顺序表
* 参数 : elem(in) -- 查找的元素
* 返回值 : 成功:元素的位置,正确范围[0,L->length),失败:-1
******************************************************************/
int get_elem_pos(SeqList *L, ElemType elem)
{
if(true == isEmpty(L))
{
return -1;
}
for (int i = 0; i < L->length; i++)
{
if(L->data[i] == elem)
{
return i;
}
}
return -1;
}
/*****************************************************************
* 函数功能 : 查找指定位置数据
* 参数 : L(in) -- 顺序表
* 参数 : pos(in) -- 指定位置,正确范围[0,L->length)
* 参数 : elem(out) -- 找到的元素
* 返回值 : 成功:0,失败:-1
******************************************************************/
int get_elem_date(SeqList *L, int pos, ElemType *elem)
{
if(true == isEmpty(L) || true == isPosWrong(L, pos) )
{
return -1;
}
*elem = L->data[pos];
return 0;
}
/*****************************************************************
* 函数功能 : 获取顺序表长度
* 参数 : L(in) -- 顺序表
* 返回值 : 顺序表长度
******************************************************************/
int seqlist_length(SeqList *L)
{
return L->length;
}
/*****************************************************************
* 函数功能 : 打印顺序表
* 参数 : L(in) -- 顺序表
* 返回值 : void
******************************************************************/
void print_seqlist(SeqList *L)
{
for (int i = 0; i < L->length; i++)
{
printf("%d ", L->data[i]);
}
printf("\n\n");
}
//测试
int main(int argc, char const *argv[])
{
SeqList *seqlist = init_seqlist();
if(NULL == seqlist)
{
return -1;
}
printf("seqlist->length :%d\n",seqlist_length(seqlist));
print_seqlist(seqlist);
//尾部添加
append_elme(seqlist, 00);
append_elme(seqlist, 11);
append_elme(seqlist, 22);
printf("seqlist->length :%d\n",seqlist_length(seqlist));
print_seqlist(seqlist);
//指定插入
insert_elme(seqlist, 3, 33); //pos溢出
insert_elme(seqlist, -1, -11);//pos溢出
insert_elme(seqlist, 1, 55);
printf("seqlist->length :%d\n",seqlist_length(seqlist));
print_seqlist(seqlist);
//指定删除
ElemType elem = 0;
delete_elem(seqlist, 5, &elem); //pos溢出
printf("elem :%d\n", elem);
print_seqlist(seqlist);
delete_elem(seqlist, 1, &elem);
printf("elem :%d\n", elem);
printf("seqlist->length :%d\n",seqlist_length(seqlist));
print_seqlist(seqlist);
//指定修改
modify_elem(seqlist, -1, -11);//pos溢出
modify_elem(seqlist, 6, 66); //pos溢出
print_seqlist(seqlist);
modify_elem(seqlist,1, 77);
print_seqlist(seqlist);
//获取元素位置
printf("pos: %d\n", get_elem_pos(seqlist, 100));
printf("pos: %d\n", get_elem_pos(seqlist, 77));
//获取指定位置数据
get_elem_date(seqlist, 0, &elem);
printf("elem: %d\n", elem);
//销毁
destroy_seqlist(seqlist);
return 0;
}
测试结果
seqlist->length :0
seqlist->length :3
0 11 22
The specified pos wrong
The specified pos wrong
seqlist->length :4
0 55 11 22
The specified pos wrong
elem :0
0 55 11 22
elem :55
seqlist->length :3
0 11 22
The specified pos wrong
The specified pos wrong
0 11 22
0 77 22
pos: -1
pos: 1
elem: 0