顺序表.h
c
/*
* Powered by Mdr-C-Tutorial. No rights reserved.
*/
#ifndef MDR_SEQLIST_H
#define MDR_SEQLIST_H
#include <stdio.h>
#include <stdlib.h>
constexpr int g_initCap = 4;
typedef int ElementType;
typedef struct
{
ElementType *arr;
size_t size; // length of the sequence list
size_t capacity; // size of the array
} SeqList;
void initSeqList(SeqList *l)
{
l->arr = (ElementType *)malloc(sizeof(ElementType) * g_initCap);
if (!(l->arr))
{
printf("Initialization failed!\n");
exit(-1);
}
l->size = 0;
l->capacity = g_initCap;
}
void initSeqListFromArray(SeqList *l, const ElementType *a, int length)
{
l->arr = (ElementType *)malloc(sizeof(ElementType) * length);
if (!(l->arr))
{
printf("Initialization failed!\n");
exit(-1);
}
for (size_t n = 0; n < length; ++n)
{
l->arr[n] = a[n];
}
l->size = length;
l->capacity = length;
}
void clearSeqList(SeqList *l)
{
free(l->arr);
initSeqList(l);
}
bool isSeqListEmpty(const SeqList *l) { return l->size == 0; }
size_t lengthOfSeqList(const SeqList *l) { return l->size; }
ElementType getFromSeqList(const SeqList *l, int i)
{
if (i >= l->size)
{
printf("Index out of bound!\n");
return 0;
}
return l->arr[i];
}
void expandSeqList(SeqList *l)
{
printf("Expansion\n");
ElementType *newArr = (ElementType *)realloc(l->arr, sizeof(ElementType) * 2 * l->capacity);
if (!(newArr))
{
printf("Expansion failed!\n");
exit(-1);
}
l->capacity *= 2;
l->arr = newArr;
}
void curtailSeqList(SeqList *l)
{
printf("Curtailment\n");
ElementType *newArr = (ElementType *)realloc(l->arr, sizeof(ElementType) * l->capacity / 2);
if (!newArr)
{
printf("Curtailment failed!\n");
exit(-1);
}
l->capacity /= 2;
l->arr = newArr;
}
void insertBack2SeqList(SeqList *l, ElementType x)
{
if (l->size == l->capacity)
{
expandSeqList(l);
}
l->arr[l->size] = x;
l->size += 1;
}
void insert2SeqList(SeqList *l, int i, ElementType x)
{
if (l->size == l->capacity)
{
expandSeqList(l);
}
for (size_t n = l->size; n > i; --n)
{
l->arr[n] = l->arr[n - 1];
}
l->arr[i] = x;
l->size += 1;
}
void removeFromSeqList(SeqList *l, int i)
{
if (i >= l->size)
printf("Index out of bound!\n");
for (size_t n = i; n < l->size - 1; ++n)
{
l->arr[n] = l->arr[n + 1];
}
l->size -= 1;
if ((l->size + 1) * 4 < l->capacity)
{
curtailSeqList(l);
}
}
int indexOfSeqList(const SeqList *l, ElementType x)
{
for (int n = 0; n < l->size; ++n)
{
if (l->arr[n] == x)
{
return n;
}
}
return -1;
}
void printSeqList(const SeqList *l)
{
printf("cap: %zu size: %zu ==>", l->capacity, l->size);
for (size_t i = 0; i < l->size; ++i)
{
printf(" [%zu]=%d", i, l->arr[i]);
}
printf("\n");
}
#endif
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
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