经过三天的时间终于把顺序表的操作实现搞定了。(主要是在测试部分停留了太长时间)

1;线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素。

2;采用的实现方式:一段地址连续的存储单元可以用固定数组或者动态存储结构来实现,这里采用动态分配存储结构。

3;顺序表的定义及操作集合:头文件为defs.h

 1 #ifndef _DEFS_H
2 #define _DEFS_H
3
4 #include<stdio.h>
5 #include<stdlib.h>
6 #include<malloc.h>
7
8 #define LIST_INIT_MAX 10 //长表为10
9 #define LIST_INCREMENT 2 //短表为2
10 typedef struct
11 {
12 int * elem; //采用动态存储分配结构
13 int length;
14 int listsize;
15 }sqlist;
16 //线性表操作集合
17 void InitList(sqlist *L); //初始化,动态分配一块存储空间
18 void DestroyList(sqlist *L); //释放这一段存储空间(撤销对应于动态)
19 void ClearList(sqlist *L);
20 void ListEmpty(sqlist L);
21 int ListLength(sqlist L);
22 int GetElem(sqlist L, int i, int *e);
23 void LocateList(sqlist L, int e); //在表中查找值为e的元素
24 int PriorElem(sqlist L, int cur_e, int *pri_e); //求当前元素的前驱
25 int NextElem(sqlist L, int cur_e, int *Nex_e); //求当前元素的后继
26 int ListInsert(sqlist &L, int i, int e); //插入操作
27 int ListDelete(sqlist &L, int i, int *e); //删除操作
28 void TravelList(sqlist L); //便历操作
29 #endif

4;顺序表结构体示意图 
 

5;InitList函数实现

 1 #include"defs.h"
2
3 void InitList(sqlist *L)
4 {
5 L->elem = (int *)malloc(LIST_INIT_MAX*sizeof(int)); //初始化指针
6 if (!L->elem)
7 exit(-1);
8 L->length = 0; //初始化当前元素个数
9 L->listsize = LIST_INIT_MAX; //初始化表长
10 }

InitList

6;DestroyList函数实现

1 #include"defs.h"
2
3 void DestroyList(sqlist *L)
4 {
5 free(L->elem); //释放空间
6 L->elem = NULL; //将指针置空
7 L->length = 0; //当前元素个数为0
8 L->listsize = 0; //表长为0
9 }

DestroyList

7;ClearList函数实现

1 #include"defs.h"
2
3 void ClearList(sqlist *L)
4 {
5 L->length = 0; //令当前元素个数为0
6 }

ClearList

8;ListEmpty函数实现

1 #include"defs.h"
2
3 void ListEmpty(sqlist L)
4 {
5 if (L.length == 0)
6 printf("表为空.\n");
7 else
8 printf("表不为空.\n");
9 }

ListEmpty

9;ListLength函数实现

1 #include"defs.h"
2
3 int ListLength(sqlist L)
4 {
5 return L.length; //返回表的当前元素个数
6 }

ListLength

10;GetElem函数实现

 1 #include"defs.h"
2
3 int GetElem(sqlist L, int i, int *e) //1<= i <=L.length
4 {
5 if (i<1 || i>L.length)
6 {
7 printf("取值位置不正确。\n");
8 return 0;
9 }
10 *e = *(L.elem + i - 1);
11 return 0;
12 }

GetElem

11;LocateElem函数实现

 1 #include"defs.h"
2
3 void LocateElem(sqlist L, int e)
4 {
5 int i;
6 int * p = L.elem;
7
8 for (i=0; i<L.length; i++, p++)
9 if (*p == e)
10 printf("找到值为%d的元素,其位置为%d\n", e, i+1);
11
12 printf("在顺序表中没有找到值为%d的元素.\n", e);
13 }

LocateElem

12;PriorElem函数实现

 1 PriorElem.c
2 #include"defs.h"
3
4 int PriorElem(sqlist L, int cur_e, int *pri_e) //第一个元素无前驱
5 {
6 int i;
7 int *p = L.elem;
8
9 for (i=0; i<L.length; i++,p++) //顺序表长度已知,故用for循环
10 {
11 if (i==0 && *p==cur_e)
12 {
13 printf("当前元素为第一个元素,无前驱.\n");
14 return 0;
15 }
16 if (*p == cur_e) //找到了当前元素且不是第一个元素,
17 {
18 *pri_e = *--p; //将其前驱赋给引用参数
19 return 0;
20 }
21 }
22 printf("顺序表中无当前值%d。\n", cur_e);
23 return 0;
24 }

PriorElem

13;NextElem函数实现

 1 NextElem.c
2 #include"defs.h"
3
4 int NextElem(sqlist L, int cur_e, int *nex_e) //最后一个元素无后继
5 {
6 int i;
7 int *p = L.elem;
8
9 for (i=0; i<L.length; i++) //顺序表长度已知,故用for
10 {
11 if (i==L.length-1 || *p==cur_e)
12 {
13 printf("当前元素为最后一个元素,无后继.\n");
14 return 0;
15 }
16 if (*p == cur_e)
17 {
18 *nex_e = *++p; //将后继赋给引用参数带回
19 return 0;
20 }
21 }
22 printf("顺序表中无当前值%d。\n", cur_e);
23 return 0;
24 }

NextElem

14;ListInsert函数实现

 1 ListInsert.c
2 #include"defs.h"
3
4 int ListInsert(sqlist *L, int i, int e) //1<= i <=L->length+1
5 {
6 int *newbase, *p, *q;
7
8 if (i<1 || i>L->length+1) //插入位置不合理
9 {
10 printf("插入位置不合理.\n");
11 return 0;
12 }
13 if (L->length == L->listsize) //表已满
14 {
15 newbase = (int *)realloc(L->elem, (L->listsize + LIST_INCREMENT) * sizeof (int)); //用newbase指针是为了保护L->elem
16 if (!newbase)
17 {
18 printf("继续分配内存空间失败.\n");
19 exit(-1);
20 }
21 L->listsize += LIST_INCREMENT;
22 }
23 p = L->elem + i - 1; //p指向插入的位置
24 q = L->elem + L->length - 1; //q指向表中元素最后一个位置
25
26 for (; q>=p; q--) //从最后一个元素开始依次向后移动表中元素
27 *(q+1) = *q;
28 *q = e; //插入元素
29 L->length++; // 表长增一
30 return 0;
31 }

ListInsert

15;ListDelete函数实现

 1 ListDelete.c
2
3 #include"defs.h"
4
5 int ListDelete(sqlist *L, int i, int *e) //1<= i <=L->length
6 {
7 int *p, *q;
8
9 if (i<1 || i>L->length)
10 {
11 printf("删除位置不合理.\n");
12 return 0;
13 }
14
15 p = L->elem + i - 1; //p指向要删除的元素的位置
16 q = L->elem + L->length - 1; //q指向表中最后一个元素位置
17
18 *e = *p; //将要删除的元素保存起来
19 for (; p<=q; p++) //从要删除元素的后面一个元素开始移动元素
20 *p = *(p+1);
21 L->length--; //表长减一
22 return 0;
23 }

ListDelete

16;TravelList函数实现

 1 TravelList.c
2
3 #include "defs.h"
4
5 void TravelList(sqlist L)
6 {
7 int i
8 int *p = L.elem;
9
10 for (i=0; i<L.length; i++,p++)
11 {
12 printf("第%d个元素为:%d\n", i+1, *p);
13 }
14
15 }

TravelList

17;makefile的实现

 1 object = main.o InitList.o DestroyList.o ClearList.o ListEmpty.o \
2 ListLength.o GetElem.o LocateElem.o PriorElem.o NextElem.o \
3 ListInsert.o ListDelete.o TravelList.o
4
5 test : $(object)
6 gcc -g -Wall -o test $(object)
7 main.o : defs.h
8 InitList.o : defs.h
9 DestroyList.o : defs.h
10 ClearList.o : defs.h
11 ListEmpty.o : defs.h
12 ListLength.o : defs.h
13 GetElem.o : defs.h
14 LocateElem.o : defs.h
15 PriorElem.o : defs.h
16 NextElem.o : defs.h
17 ListInsert.o : defs.h
18 ListDelete.o : defs.h
19 TravelList.o : defs.h
20
21 .PHONY : clean
22 clean :
23 rm *.o -f

18;顺序表的优缺点:顺序表由于其存储结构的特点,特别适合查找(其时间复杂度为O(1)),不适合频繁插入和删除(每一次插入和删除的时间复杂度都是O(n))

c语言实现--顺序表操作的更多相关文章

  1. C++语言实现顺序表

    C++语言实现顺序表 顺序表的定义及其特点 顺序表的定义是:把线性表中的所有表项按照其逻辑顺序依次存储到从计算机存储中指定存储位置开始的一块连续的存储空间中. 这样,线性表中第一个表项的存储位置就是被 ...

  2. C语言实现顺序表(顺序存储结构)

    顺序表(顺序存储结构)及初始化过程详解 顺序表,全名顺序存储结构,是线性表的一种.通过<线性表>一节的学习我们知道,线性表用于存储逻辑关系为"一对一"的数据,顺序表自然 ...

  3. C语言实现顺序表

    C语言实现顺序表代码 文件SeqList.cpp #pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqLi ...

  4. C语言实现顺序表的基本操作(从键盘输入 生成线性表,读txt文件生成线性表和数组生成线性表----三种写法)

    经过三天的时间终于把顺序表的操作实现搞定了.(主要是在测试部分停留了太长时间) 1. 线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素. 2. 采用的实现方式:一段地 ...

  5. java语言建立顺序表

    package datastructure; //线性表 public interface IList { public void clear(); public boolean isEmpty(); ...

  6. [PTA] 数据结构与算法题目集 6-2 顺序表操作集

    //创建并返回一个空的线性表: List MakeEmpty() { List L; L = (List)malloc(sizeof(struct LNode)); L->Last = -1; ...

  7. C# 顺序表操作

    虽然.NET已经是现实了Reverse(),但是学习算法有必要知道其是怎么实现的: private static void ReverseArray(int[] array) { int temp; ...

  8. 顺序表的C语言实现

    在现实应用中,有两种实现线性表数据元素存储功能的方法,分别是顺序存储结构和链式存储结构.顺序表操作是最简单的操作线性表的方法.下面的代码实现了顺序表的几种简单的操作.代码如下 //start from ...

  9. 顺序表及其多种实现方式 --- C/C++

    所谓顺序表,即线性表的顺序存储结构.下面给出的是数据结构---线性表的定义. ADT List{ 数据对象: 线性表的数据对象的集合为{a1,a2,a3,...,an},每个元素的类型为ElemTyp ...

随机推荐

  1. d3 zoom 抖动问题 事件

    最近在使用d3 zoom得时候   遇到一个小坑 直接对元素添加 zoom事件 会有很大得抖动,查文档 看代码之后发现是 由于元素在不断变化, 所以计算基础值也不不断变化,所以会导致计算出来得值 忽大 ...

  2. idea中文注释出现乱码,我靠自己解决了

    如果你像我一样️,查遍google百度,半天下来还是找不到解决方案,说不定这篇博客能帮助你顺利解决呢 好了,那么开始说说我是怎么解决麻烦的. 首先,我想打开一份java文稿.光预览,它是没有任何问题的 ...

  3. 基于Python实现的系统SLA可用性统计

    基于Python实现的系统SLA可用性统计 1. 介绍 SLA是Service Level Agreement的英文缩写,也叫服务质量协议.根据SRE Google运维解密一书中的定义: SLA是服务 ...

  4. dig的安装和使用

    -bash: dig: command not found 解决办法: yum -y install bind-utils dig www.baid bu.com   查看a记录 dig www.ba ...

  5. 【IMPDP】ORA-31655

    出现ora-31655错误的情况是因为不是同一个schema,导致的问题产生 解决的方法; 在导入语句最后添加上remap_schema=old:new 着old是原schema,也就是导出的用户名, ...

  6. Java中,那些关于String和字符串常量池你不得不知道的东西

    老套的笔试题 在一些老套的笔试题中,会要你判断s1==s2为false还是true,s1.equals(s2)为false还是true. String s1 = new String("xy ...

  7. .net core 和 WPF 开发升讯威在线客服与营销系统:使用 TCP协议 实现稳定的客服端

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

  8. Redis布隆过滤器与布谷鸟过滤器

    大家都知道,在计算机中,IO一直是一个瓶颈,很多框架以及技术甚至硬件都是为了降低IO操作而生,今天聊一聊过滤器,先说一个场景: 我们业务后端涉及数据库,当请求消息查询某些信息时,可能先检查缓存中是否有 ...

  9. 借助 AppleScript 一键打开工作空间

    我有个小毛病:同时只能在一个工程里工作. 假如让我开四五个 Webstorm,在工程里 A 改个Bug,然后又到工程 B 里加个需求,再去工程 C 发个版,切来切去一会儿就懵了. 于是有了这个项目:m ...

  10. 抛弃 .NET 经典错误:object null reference , 使用安全扩展方法? 希望对大家有帮助---Bitter.Frame 引用类型的安全转换

    还是一样,我不喜欢长篇大论,除非关乎我设计思想领域的文章.大家过来看,都是想节省时间,能用白话表达的内容,绝不长篇大论.能直接上核心代码的,绝不上混淆代码. 长期从事 .NET 工作的人都知道..NE ...