1.什么是线性表?

线性表是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构。常见的有:顺序表、链表、栈、队列、队列、字符串。

2.什么是顺序表?

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组 上完成数据的增删查改。
顺序表一般可以分为:

  1. 静态顺序表:使用定长数组存储。
  2. 动态顺序表:使用动态开辟的数组存储

静态顺序表一般指的是存储的个数是固定的:

1
2
3
4
5
struct SeqList
{
DataType _array[MAX_CAPACITY];
int _size;
};

动态顺序表可以根据情况实时扩容

1
2
3
4
5
6
struct SeqList
{
DataType* _array;
int _size;
int _capacity;
};

动态顺序表实现
SeqList.h

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
#include <assert.h>
#include <stdio.h>
#include<stdlib.h>
typedef int DataType;
typedef struct SeqList
{
DataType* _array;
int _size;
int _capacity;

}SeqList, *PSeq;
//typedef struct SeqList SeqList;
//typedef struct SeqList* PSeqList;

//// 顺序表的初始化
void SeqListInit(PSeq ps, int capacity)
{
assert(ps);
ps->_size = 0;
ps->_capacity = capacity;
ps->_array = (DataType*)malloc(ps->_capacity * sizeof(DataType));
if (ps->_array == NULL)
{
return;
}
}
//// 顺序表的扩容
void CheckCapacity(PSeq ps)
{
assert(ps);
if (ps->_capacity == ps->_size)
{
ps->_capacity = ps->_capacity * 2;
ps->_array = (DataType *)realloc(ps->_array, ps->_capacity * sizeof(DataType));
if (ps->_array)
printf("扩容成功");
}
return;
}
//// 在顺序表的尾部插入值为data的元素
void SeqListPrint(PSeq ps)
{
assert(ps);
for (int i = 0; i < ps->_size; i++)
{
printf("%d ", ps->_array[i]);
}
}
void SeqListPushBack(PSeq ps,DataType data)
{
assert(ps);
CheckCapacity(ps);
ps->_array[ps->_size] = data;
ps->_size++;
}
//// 删除顺序表最后一个元素
//void SeqListPopBack(PSeq ps);
void SeqListPopBack(PSeq ps)
{
assert(ps);
if (0 == ps->_size)
{
return;
}
ps->_size--;
}
//// 在顺序表的头部插入值为data的元素
void SeqListPushFront(PSeq ps, DataType data)
{
assert(ps);
CheckCapacity(ps);

for (int i = (ps->_size - 1); i >= 0; i--)
{
ps->_array[i + 1] = ps->_array[i];
}
ps->_array[0] = data;
ps->_size++;
}
//// 删除顺序表头部的元素
void SeqListPopFront(PSeq ps)
{
assert(ps);
if (ps->_size == 0)
{
return;
}
for (int i = 1; i <= ps->_size - 1; i++)
{
ps->_array[i - 1] = ps->_array[i];
}
ps->_size--;
}
//// 在顺序表pos位置插入值为data的元素
void SeqListInsert(PSeq ps, int pos, DataType data)
{
assert(ps);

CheckCapacity(ps);

for (int i = ps->_size-1; i >= pos-1; i--)
{
ps->_array[i + 1] = ps->_array[i];
}
ps->_array[pos - 1] = data;
ps->_size++;
}
//// 删除顺序表中pos位置上的元素
void SeqListErase(PSeq ps, int pos)
{
assert(ps);
if (ps->_size == 0) return;
for (int i = pos - 1; i <= ps->_size - 1; i++)
{
ps->_array[i] = ps->_array[i + 1];
}
ps->_size--;
}
//// 在顺序表中查找值为data的元素,找到返回该元素在顺序表中的下标,否则返回-1
int SeqListFind(PSeq ps, DataType data)
{
assert(ps);
int count = ps->_size - 1;
while (count-- >= 0)
{
if (data == ps->_array[count])
{
return count+1;
}
}
return -1;
}
//// 检测顺序表是否为空,如果为空返回非0值,非空返回0
int SeqListEmpty(PSeq ps)
{
assert(ps);
//if (ps->_size)
// return 0;
//return 1;
return ps->_size == 0 ? 1 : 0;
}
//// 返回顺序表中有效元素的个数
int SeqListSize(PSeq ps)
{
assert(ps);
return ps->_size;
}
//// 返回顺序表的容量大小
int SeqListCapacity(PSeq ps)
{
assert(ps);
return ps->_capacity;
}
//// 将顺序表中的元素清空
void SeqListClear(PSeq ps)
{
assert(ps);
ps->_size = 0;
}
//// 删除顺序表中第一个值为data的元素
void SeqListRemove(PSeq ps, DataType data)
{
assert(ps);
int num = SeqListFind(ps,data);
SeqListErase(ps, num);
}

//// 销毁顺序表
void SeqListDestroy(PSeq ps)
{
assert(ps);
ps->_capacity = 0;
ps->_size = 0;
free(ps->_array);
}

最后更新: 2019年05月15日 20:34

原始链接: http://CQolive.github.io/2019/05/15/动态顺序表/

× 请我吃糖~
打赏二维码