线性表:0个或n个元素组成的有限序列
线性表基本运算:
创建表,置空表,判断表是否为空,求表长
获取表中元素,删除元素,插入元素,定位元素在表中的位置
这里举例为线性表的顺序储存结构:即线性表中的各元素储存在储存器中的一片连续存储空间
touch sqlist.c sqlist.h test.c //创建三个文件
sqlist.c的实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlist.h"
//创建表
sqlist_t *sqlist_create(void)
{
sqlist_t *L =NULL;
L = (sqlist_t *)malloc(sizeof(sqlist_t));
if(L){
memset(L,0,sizeof(sqlist_t));
L->last = -1;
}
return L;
}
//清空表
void sqlist_clean(sqlist_t *L)
{
if(L)
{
free(L);
}
}
//判断表是否为空,1为空,0不为空
int sqlist_is_empty(sqlist_t *L)
{
int is_empty =0;
if(L)
{
is_empty = (L->last == -1)? 1:0;
}
return is_empty;
}
//判断表是否满,1为满,0不为满
int sqlist_is_full(sqlist_t *L)
{
int is_full =0;
if(L)
{
is_full = (L->last == MAXSIZE-1)? 1:0;
}
return is_full;
}
//求表长
int sqlist_get_length(sqlist_t *L)
{
int length=0;
if(L)
{
length = L->last+1;
}
return length;
}
//插入元素
int sqlist_insert(sqlist_t *L,data_t x,int post)
{
if(L)
{
if(sqlist_is_full(L)==1)
{
printf("sqlist_is_full\n");
return -1;
}
if(post>L->last+1 || post<0){
printf("post error\n");
return -1;
}
for(int i=L->last;i>=post;i--)
L->data[i+1]=L->data[i];
L->data[post] = x;
L->last++;
}
return 0;
}
//show元素
void sqlist_show(sqlist_t *L)
{
if(L)
{
printf("\n===show start===\n");
for(int i=0;i<=L->last;i++)
printf("%d,",L->data[i]);
printf("\n===show end===\n");
}
}
//删除元素
int sqlist_delete(sqlist_t *L,int pos)
{
int ret = -1;
if(L)
{
if(pos>L->last || pos<0)
{
printf("pos is error\n");
return -1;
}
for(int i=pos;i<=L->last;i++)
{
L->data[i]=L->data[i+1];
}
L->last--;
ret=0;
}
return ret;
}
//更新元素
int sqlist_update(sqlist_t *L,data_t x,int pos)
{
int ret = -1;
if(L)
{
if(pos>L->last || pos<0)
{
printf("pos is error\n");
return -1;
}
for(int i=0;i<=L->last;i++)
{
if(i==pos)
{
L->data[i]=x;
return 0;
}
}
}
return ret;
}
//查找元素,返回元素在表中的位置
int sqlist_search(sqlist_t *L,data_t x)
{
int ret = -1;
if(L)
{
for(int i=0;i<=L->last;i++)
{
if(L->data[i]==x)
{
ret=i;
break;
}
}
}
return ret;
}
sqlist.h头文件内容
#ifndef __SQLIST_H__
#define __SQLIST_H__
#define MAXSIZE 10
typedef int data_t;
typedef struct
{
data_t data[MAXSIZE];
int last;
}sqlist_t;
//创建表
sqlist_t *sqlist_create(void);
//清空表
void sqlist_clean(sqlist_t *L);
//判断表是否为空
int sqlist_is_empty(sqlist_t *L);
//判断表是否满
int sqlist_is_full(sqlist_t *L);
//求表长
int sqlist_get_length(sqlist_t *L);
//插入元素
int sqlist_insert(sqlist_t *L,data_t x,int post);
//删除元素
int sqlist_delete(sqlist_t *L,int pos);
//更新元素
int sqlist_update(sqlist_t *L,data_t x,int pos);
//查找元素
int sqlist_search(sqlist_t *L,data_t x);
//show元素
void sqlist_show(sqlist_t *L);
#endif
main.c对各接口的实现
int main()
{
sqlist_t *L = sqlist_create();
if(L)
{
for(int i=0;i<2;i++){
data_t x = i;
sqlist_insert(L,x,0);
}
sqlist_show(L);
int len = sqlist_get_length(L);
printf("len:%d\n",len);
sqlist_delete(L,1);
sqlist_delete(L,0);
len = sqlist_get_length(L);
printf("len1:%d\n",len);
sqlist_show(L);
sqlist_update(L,66,8);
printf("len2:%d\n",len);
sqlist_show(L);
printf("sqlist_is_empty:%d\n",sqlist_is_empty(L));
sqlist_clean(L);
}
return 0;
}