实现基于静态数组的顺序表的以下基本操作:
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
9. 在任意位置插入元素
seqlist.h
1 #pragma once
2 #include <stdio.h>
3 #define SeqListMax 100
4
5 typedef char SeqListType;
6
7 typedef struct SeqList{
8 SeqListType SeqListArr[SeqListMax];
9 size_t size;
10 }SeqList;
11
12
13 void SeqListInit(SeqList *seq); //初始化
14 void SeqListPrint(SeqList *seq,char *ch);//打印顺序表
15 void SeqListPushEnd(SeqList *seq,SeqListType value);//尾部插入
16 void SeqListPopEnd(SeqList *seq);//尾部删除
17 void SeqListPushSta(SeqList *seq,SeqListType Value);//头部插入
18 void SeqListPopSta(SeqList *seq);//头部删除
19 void SeqListSetValue(SeqList *seq,size_t pos,SeqListType value);//修改任意位置元素
20 void SeqListGetValue(SeqList *seq,size_t pos);//查询下标为pos的元素
21 void SeqListPushValue(SeqList *seq,size_t pos,SeqListType value);//在下标pos处插入元素value
22 void SeqListLookValue(SeqList *seq,SeqListType value);//查询value的下标
seqlist.c
1 #include "seqlist.h"
2
3
4 //初始化
5 void SeqListInit(SeqList *seq)
6 {
7 if(seq==NULL)
8 return;
9 seq->size=0;
10 }
11
12
13 //打印顺序表
14 void SeqListPrint(SeqList *seq,char *ch)
15 {
16 printf("%s\n",ch);
17 int i=0;
18 for(;i<seq->size;i++)
19 {
20 printf("[%c] ",seq->SeqListArr[i]);
21 }
22 printf("\n");
23 }
24
25 //尾部插入
26 void SeqListPushEnd(SeqList *seq,SeqListType value)
27 {
28 if(seq==NULL)
29 return;
30 if(seq->size >= SeqListMax)
31 {
32 printf("当前顺序表已满\n");
33 return;
34 }
35 seq->SeqListArr[seq->size] = value;
36 seq->size++;
37
38 }
39
40 //尾部删除
41 void SeqListPopEnd(SeqList *seq)
42 {
43 if(seq==NULL)
44 return;
45 seq->size--;
46 }
47
48 //头部插入
49 void SeqListPushSta(SeqList *seq,SeqListType value)
50 {
51 if(seq==NULL)
52 return;
53 if(seq->size>=SeqListMax)
54 {
55 printf("顺序表已满\n");
56 return;
57 }
58 int i = 0;
59 i = seq->size-1;
60 for(;i>=0;i--)
61 {
62 seq->SeqListArr[i+1]=seq->SeqListArr[i];
63 }
64 seq->SeqListArr[0]=value;
65 seq->size++;
66 }
67
68 //头部删除
69 void SeqListPopSta(SeqList *seq)
70 {
71 if(seq==NULL)
72 return;
73 if(seq->size==0)
74 {
75 printf("顺序表为空\n");
76 return;
77 }
78 int i = 0;
79 for(i=0;i<seq->size-1;i++)
80 {
81 seq->SeqListArr[i]=seq->SeqListArr[i+1];
82 }
83 seq->size--;
84 }
85 //查询下标为pos的元素
86 void SeqListGetValue(SeqList *seq,size_t pos)
87 {
88 if(seq==NULL)
89 return;
90 if(pos<0||pos>=seq->size)
91 {
92 printf("非法输入\n");
93 return;
94 }
95 printf("[%c]\n",seq->SeqListArr[pos]);
96 }
97 //修改任意位置元素
98 void SeqListSetValue(SeqList *seq,size_t pos,SeqListType value)
99 {
100 if(seq==NULL)
101 return;
102 if(pos<0||pos>=seq->size)
103 {
104 printf("非法输入\n");
105 return;
106 }
107 seq->SeqListArr[pos]=value;
108 }
109 //查询元素下标
110 void SeqListLookValue(SeqList *seq,SeqListType value)
111 {
112 if(seq==NULL)
113 return;
114 int i = 0;
115 for(i=0;i<seq->size;i++)
116 {
117 if(seq->SeqListArr[i]==value)
118 printf("[%d] ",i);
119 }
120
121 }
122 //在下标pos处插入元素value
123 void SeqListPushValue(SeqList *seq,size_t pos,SeqListType value)
124 {
125 if(seq==NULL)
126 return;
127 if(pos<0||pos>=seq->size-1)
128 {
129 printf("非法输入\n");
130 return;
131 }
132 int i =seq->size-1;
133 for(i;i>=pos;i--)
134 {
135 seq->SeqListArr[i+1]=seq->SeqListArr[i];
136 }
137 seq->SeqListArr[pos]=value;
138 seq->size++;
139 }
140
141 //
142 // 测试 //
143 /
144 void TestSeqListInit()
145 {
146 SeqList seq;
147 SeqListInit(&seq);
148 SeqListPrint(&seq,"================初始化================\n");
149 }
150
151 void TestSeqListPushEnd()
152 {
153 SeqList seq;
154 SeqListPushEnd(&seq,'a');
155 SeqListPushEnd(&seq,'b');
156 SeqListPushEnd(&seq,'c');
157 SeqListPrint(&seq,"================尾部增加===============\n");
158 }
159
160 void TestSeqListPopEnd()
161 {
162 SeqList seq;
163 SeqListPopEnd(&seq);
164 SeqListPrint(&seq,"===============尾部删除================\n");
165 }
166
167 void TestSeqListPushSta()
168 {
169 SeqList seq;
170 SeqListPushSta(&seq,'z');
171 SeqListPushSta(&seq,'y');
172 SeqListPushSta(&seq,'x');
173 SeqListPrint(&seq,"===============头部插入================\n");
174 }
175
176 void TestSeqListPopSta()
177 {
178 SeqList seq;
179 SeqListPopSta(&seq);
180 SeqListPrint(&seq,"===============头部删除================\n");
181 }
182
183 void TestSeqListGetValue()
184 {
185 SeqList seq;
186 SeqListPrint(&seq,"===============查询元素================\n");
187 SeqListGetValue(&seq,2);
188 }
189
190 void TestSeqListSetValue()
191 {
192 SeqList seq;
193 SeqListPrint(&seq,"==============修改元素================\n");
194 SeqListSetValue(&seq,2,'H');
195 SeqListPrint(&seq,"修改之后:");
196 }
197
198 void TestSeqListLookValue()
199 {
200 SeqList seq;
201 SeqListPushEnd(&seq,'H');
202 SeqListPrint(&seq,"============查询元素坐标==============\n");
203 printf("所查元素%c坐标为:",'H');
204 SeqListLookValue(&seq,'H');
205 printf("\n");
206 }
207
208 void TestSeqListPushValue()
209 {
210 SeqList seq;
211 SeqListPushValue(&seq,1,'S');
212 SeqListPrint(&seq,"============pos处插入元素=============\n");
213 }
214
215 int main(void)
216 {
217 TestSeqListInit();
218 TestSeqListPushEnd();
219 TestSeqListPopEnd();
220 TestSeqListPushSta();
221 TestSeqListPopSta();
222 TestSeqListGetValue();
223 TestSeqListSetValue();
224 TestSeqListLookValue();
225 TestSeqListPushValue();
226 }
运行结果: