我是靠谱客的博主 淡然小兔子,这篇文章主要介绍线性表顺序存储的一些操作(初始化,添加,删除)一.线性表顺序存储二.一些操作三.线性表的优缺点,现在分享给大家,希望可以做个参考。

最近有时间学一些自己想学的,就找来看看数据结构。看的是大话数据结构。之前学数据结构是大一时候,现在看这个还可以,没那时候学的累。但是还是需要进行一些总结,以免自己会忘记。
虽然大部分和书上差不多,但是不自己动手敲,还是会有问题。或者有些地方自己这样写会好理解一些。

一.线性表顺序存储

复制代码
1
2
线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素。

接下来我们对线性表结构进行了解

复制代码
1
2
3
4
5
6
#define MAXSIZE 200 typedef struct{ Elemtype data[MAXSIZE]; int length; }Sqlist;

这里的长度指的是线性表长度,是线性表中数据元素的个数。我们对线性表进行操作,这个量是变化的。
其实线性表的顺序存储就是数组,但是这个长度是变化的,可以这么理解。

二.一些操作

1.初始化线性表

复制代码
1
2
3
4
5
6
7
8
9
10
Sqlist *InitList(int n,int t) { Sqlist *L; L = (Sqlist *)malloc(sizeof(Sqlist)); L->length = n; for(int i = 0; i < n; i++) L->data[i] = t*i; return L; }

这里的t是随便给的值,n是长度。

2.获得元素

复制代码
1
2
3
4
5
6
7
8
9
10
Status GetElem(Sqlist *L,int i,Elemtype *e) { if(i < 1 || i > L->length) return ERROR; if(L->length == 0) return ERROR; *e = L->data[i-1]; return OK; }

这里主要是记得要判断分界条件,不然有时候没输出会觉得很奇怪。

3.增加元素

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Status InsertElem(Sqlist *L,int i,Elemtype e) { int k; if(i < 1 || i > L->length) return ERROR; if(L->length ==0) return ERROR; for(k = L->length - 1;k >= i -1;k--) L->data[k+1] = L->data[k]; L->length++; L->data[i-1] = e; return OK; }

这里需要注意的是,从最后一个元素进行遍历,不然从前开始,中间的数都会被覆盖掉。还有别忘了改线性表长度。

4.删除元素

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Status DeleteElem(Sqlist *L,int i,Elemtype *e) { int k; if(i < 1 || i > L->length) return ERROR; if(L->length ==0) return ERROR; *e = L->data[i-1]; for(k = i;k<L->length;k++) L->data[k-1] = L->data[k]; L->length--; return OK; }

这里和上面类似,不是很难理解。
下面是总体代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h> #include<stdlib.h> typedef int Status; typedef int Elemtype; #define MAXSIZE 20 #define OK 1 #define ERROR 0 typedef struct{ Elemtype data[MAXSIZE]; int length; }Sqlist; Sqlist *InitList(int n,int t) { Sqlist *L; L = (Sqlist *)malloc(sizeof(Sqlist)); L->length = n; for(int i = 0; i < n; i++) L->data[i] = t*i; return L; } void PrintList(Sqlist *L) { int i; for(i = 0;i < L->length ; i++) printf("%d ",L->data[i]); printf("n"); } Status GetElem(Sqlist *L,int i,Elemtype *e) { if(i < 1 || i > L->length) return ERROR; if(L->length == 0) return ERROR; *e = L->data[i-1]; return OK; } Status InsertElem(Sqlist *L,int i,Elemtype e) { int k; if(i < 1 || i > L->length) return ERROR; if(L->length ==0) return ERROR; for(k = L->length - 1;k >= i -1;k--) L->data[k+1] = L->data[k]; L->length++; L->data[i-1] = e; return OK; } Status DeleteElem(Sqlist *L,int i,Elemtype *e) { int k; if(i < 1 || i > L->length) return ERROR; if(L->length ==0) return ERROR; *e = L->data[i-1]; for(k = i;k<L->length;k++) L->data[k-1] = L->data[k]; L->length--; return OK; } int main(void) { Sqlist *L; Elemtype e; L = InitList(11,3); PrintList(L); //GetElem(L,7,&e); //printf("%d ",e); InsertElem(L,7,100); PrintList(L); DeleteElem(L,7,&e); PrintList(L); printf("%d",e); return 0; }

三.线性表的优缺点

在这里插入图片描述
第一次用markdown,不是很适应,就用截图了。

最后

以上就是淡然小兔子最近收集整理的关于线性表顺序存储的一些操作(初始化,添加,删除)一.线性表顺序存储二.一些操作三.线性表的优缺点的全部内容,更多相关线性表顺序存储内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部