sqlist.h:
#ifndef _SQLIST_H
#define _SQLIST_H
typedef char sqlist_data_t;//方便修改数组类型
#define N 1024 //方便修改数组大小
struct sqlist{
sqlist_data_t buf[N];
int len;
}; //方便书写
typedef struct sqlist sql_node;
typedef struct sqlist* sql_pnode;
//创建顺序表,不传参数,使用malloc
sql_pnode create_sqlist();
//增删改查
//插入
int insert_sqlist(sql_pnode L, sqlist_data_t data, int pos);
//判满
int full_sqlist(sql_pnode L);
//判空
int empty_sqlist(sql_pnode);
//打印
int show_sqlist(sql_pnode L);
//删除 按照位置删除
int delete_sqlist(sql_pnode L, int pos);
//查找 元素查位置
int search_sqlist(sql_pnode L, sqlist_data_t data);
//清空
int clean_sqlist(sql_pnode L);
//销毁,传地址
int destory_sqlist(sql_pnode *L);
#endif
sqlist.c:
#include <stdlib.h>//malloc函数需要的头文件
#include "sqlist.h"
#include <stdio.h>
//创建顺序表,不传参数,使用malloc
sql_pnode create_sqlist()
{
//创建空间
sql_pnode L = (sql_pnode)malloc(sizeof(sql_node));
if(NULL == L)
{
printf("malloc is default\n"); //方便调试
return NULL;
}
//初始化
L->len = 0;
return L;
}
//插入
int insert_sqlist(sql_pnode L, sqlist_data_t data, int pos)
{
//判断顺序表首地址有没有问题
if(NULL == L)
{
printf("L is NULL\n");
return -1;
}
//判断插入位置是否有问题
if(pos < 0 || pos > L->len)
{
printf("pos is default\n");
return -1;
}
//判断顺序表是否为满
if(0 == full_sqlist(L))
{
printf("sqliste is full\n");
return -1;
}
//插入
//移动
int i;
for(i = L->len-1; i >= pos; i--)
{
L->buf[i+1] = L->buf[i];
}
//赋值
L->buf[pos] = data;
L->len++;
return 0;
}
//判满
int full_sqlist(sql_pnode L)
{
if(L->len == N)
return 0;
else
return -1;
}
//判空
int empty_sqlist(sql_pnode L)
{
if( 0 == L->len)
return 0;
else
return -1;
}
//打印
int show_sqlist(sql_pnode L)
{
//判断顺序表首地址有没有问题
if(NULL == L)
{
printf("L is NULL\n");
return -1;
}
//判空
if(0 == empty_sqlist(L))
{
printf("L is empty\n");
return -1;
}
int i;
for(i = 0; i < L->len; i++)
{
printf("buf[%d]=%c ", i,L->buf[i]);
}
puts("");
return 0;
}
//删除 按照位置删除
int delete_sqlist(sql_pnode L, int pos)
{
//判断顺序表首地址有没有问题
if(NULL == L)
{
printf("L is NULL\n");
return -1;
}
//判断插入位置是否有问题
if(pos < 0 || pos >= L->len)
{
printf("pos is default\n");
return -1;
}
//判断顺序表是否为满
if(0 == empty_sqlist(L))
{
printf("sqliste is empty\n");
return -1;
}
int i;
for(i = pos; i < L->len-1; i++)
{
L->buf[i] = L->buf[i+1];
}
L->len--;
return 0;
}
//查找 元素查位置
int search_sqlist(sql_pnode L, sqlist_data_t data)
{
//判断顺序表首地址有没有问题
if(NULL == L)
{
printf("L is NULL\n");
return -1;
}
//判断顺序表是否为空
if(0 == empty_sqlist(L))
{
printf("sqliste is empty\n");
return -1;
}
int i;
for(i = 0; i < L->len ;i++)
{
if(data == L->buf[i])
return i;
}
printf("no search the data\n");
return -1;
}
//清空
int clean_sqlist(sql_pnode L)
{
//判断顺序表首地址有没有问题
if(NULL == L)
{
printf("L is NULL\n");
return -1;
}
//判断顺序表是否为空
if(0 == empty_sqlist(L))
{
printf("sqliste is empty\n");
return -1;
}
L->len = 0;
return 0;
}
int destory_sqlist(sql_pnode *L)
{
//判断顺序表首地址有没有问题
if(NULL == *L)
{
printf("L is NULL\n");
return -1;
}
free(*L); //释放空间
*L = NULL;
return 0;
}
main.c:
#include "sqlist.h"
#include <stdio.h>
int main()
{
sql_pnode L = create_sqlist();
insert_sqlist(L,'A',0);//插入
insert_sqlist(L,'B',0);
insert_sqlist(L,'C',0);
insert_sqlist(L,'D',0);
show_sqlist(L);//打印
delete_sqlist(L,2);//删除
show_sqlist(L);//打印
int pos = search_sqlist(L,'A');//查找
printf("pos = %d\n", pos);
clean_sqlist(L);//清空
show_sqlist(L);
destory_sqlist(&L);//销毁
show_sqlist(L);
}