数据结构-----顺序表
C语言实现《顺序表》增,删,改,查
编写顺序表思想:
1. 规范的编程
头文件 list.h
接口封装 list.c
接口的测试 test.c
2. 谁申请谁释放
申请完毕空间,一定要主动释放。
3. 解题思路 :
画图;
分析步骤;
伪代码;
需要将伪代码修改为真实可用的代码
顺序表特点:
1、大小固定,访问方便;
2、地址连续、存储密度大;
3、删除或者插入时,需要移动元素;
4、除第0个元素外,其它元素都有前驱;
除最后一个元素外,其它元素都有后继;
5、表满不能插入,表空不能删除
优点:
缺点:
代码部分:
顺序表操作
创建、销毁、尾插、尾删、查找、更新、判空、判满、遍历
头文件 list.h
#ifndef _LIST_H_
#define _LIST_H_
//常量定义
enum e_result
{
NO = -1,
OK ,
};
#define SIZE 10
//类型定义
/*定义一个数据类型来描述顺序表*/
typedef int dataType;
struct list
{
dataType data[SIZE]; //数据
int count; //存入数据个数,(元素下标)
};
typedef struct list LIST;
//全局变量申明
//函数声明
//创建线性表
LIST * createList();
//销毁线性表
void destroyList( LIST * pList );
//插入数据到线性表中
int insertDataToList( LIST * pList, int offset, dataType newData );
//输出线性表中所有元素
void showList( LIST * pList );
//删除线性表中某个元素
int deleteDataFromList( LIST * pList, int offset, dataType pData );
//查找指定的元素在线性表中的位置
int searchDataFromList( LIST * pList, dataType searchData );
//修改线性表中的元素
int updateDataToList( LIST * pList, dataType oldData, dataType newData );
#endif //_LIST_H_
接口封装 list.c
#include <stdlib.h>
#include <stdio.h>