逻辑结构体:线型
物理结构:顺序存储
功能:在堆区开辟一片空间
返回值:成功返回首地址,失败返回NULL.
参数:开辟空间的大小
栈区:自动开辟自动释放局部变量
全局: 未初始化0
以初始化 声明周期,整个程序
堆区:手动开辟,手动释放
typedef int *p;
P== int *q;
p q==int *q
——————————————————————————————————————————
typedef struct sqlist *p;
p==strucst sqliste *
p q==struct sqlist *q;
- #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
-
- #include
//malloc函数需要的头文件 - #include "sqlist.h"
- #include
-
-
- //创建顺序表,不传参数,使用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;
- }
- #include "sqlist.h"
- #include
-
-
- 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);
- }
- OBJS=sqlist.o main.o
- CFLAGS=-O -g -o
-
- sqlist:$(OBJS)
- $(CC) $^ $(CFLAGS) $@
-
- sqlist.o:sqlist.c sqlist.h
- $(CC) -c $< $(CFLAGS) $@
-
- main.o:main.c
- $(CC) -c $^ $(CFLAGS) $@
-
- .PHONY:clean
-
- clean:
- $(RM) $(OBJS) sqlist
-