基于静态数组的顺序表的一些基本操作(2)

本文详细介绍了序列列表的各种操作,包括查找指定位置的值、删除指定元素及所有特定值、实现冒泡排序(升序、降序及通用版)以及选择排序等。通过对这些基本操作的解析,帮助读者深入理解序列列表的使用。

1.查找下标为Pos位置的值
2.删除指定元素
3.删除所有to_move的值
4.冒泡排序
5.选择排序


查找下标为Pos位置的值
216 //11.查找指定位置pos的值
217 int SeqListGet(SeqList *seqlist,size_t pos,SeqListType *value)
218 {
219     if(seqlist ==NULL)                                                                                    
220     {
221     //非法输入
222     return 0;
223     }
224     if(pos>=seqlist->size)
225     {
226     //越界访问
227     return 0;
228     }
229     *value=seqlist->data[pos];
230     return 1;
231 }

删除指定元素
233 //12.删除具体值(只删除一个)
234 void SeqListRemove(SeqList *seqlist,SeqListType to_move)
235 {
236     if(seqlist ==NULL)
237     {
238     //非法输入
239     return ;
240     }
241     size_t pos =SeqListFind(seqlist,to_move);
242     if(pos==(size_t)-1)
243     {
244     return ;                                                                                              
245     }
246     SeqListErase(seqlist,pos);
247     return ;
248 }
删除所有to_move的值
250 //13.删除所有的to_remove值
251 void SeqListRemoveall(SeqList *seqlist,SeqListType to_remove)
252 {
253     if(seqlist == NULL)
254     {
255     //非法输入
256     return ;
257     }
258     while(1)
259     {
260     size_t pos=SeqListFind(seqlist,to_remove);
261     if(pos==(size_t)-1)
262     {                                                                                                     
263         break;
264     }
265     SeqListErase(seqlist,pos);
266     }
267 }

冒泡排序(升序)
270 void Swap(SeqListType *a,SeqListType *b)
271 {
272     SeqListType tmp=*a;
273     *a=*b;                                                                                                
274     *b=tmp;
275 }
276 
277 //14.冒泡排序(升序)
278 void SeqListBubbleSort(SeqList *seqlist)
279 {
280     if(seqlist == NULL)
281     {
282     //非法输入
283     return ;
284     }
285     //count为冒泡的次数
286     size_t count =0;
287     for(;count<seqlist->size;++count)
288     {
289     size_t cur =0;
290     for(;cur<seqlist->size-count-1;++cur)
291     {
292         if(seqlist->data[cur]>seqlist->data[cur+1])
293         {
294         Swap(&seqlist->data[cur],&seqlist->data[cur+1]);
295         }
296     }
297     }                                                                                                     
298     return ;
299 }

冒泡排序(降序)
302 //15.冒泡排序(降序)
303 void SeqListBubbleDesc(SeqList *seqlist)
304 {
305     if(seqlist == NULL)
306     {
307     //非法输入
308     return ;
309     }
310     //count为冒泡的次数
311     size_t count =0;
312     for(;count<seqlist->size;++count)
313     {
314     size_t cur =0;                                                                                        
315     for(;cur<seqlist->size-count-1;++cur)
316     {
317         if(seqlist->data[cur]<seqlist->data[cur+1])
318         {
319         Swap(&seqlist->data[cur],&seqlist->data[cur+1]);
320         }
321     }
322     }
323     return ;
324 }

冒泡排序(通用版)
327 //15.冒泡排序(通用版)
328 typedef int(*cmp)(SeqListType a,SeqListType b);
329 void SeqListBubbleSortEx(SeqList *seqlist,cmp cmp)
330 {
331     if(seqlist == NULL)
332     {
333     //非法输入
334     return ;
335     }
336     //count为冒泡的次数
337     size_t count =0;
338     for(;count<seqlist->size;++count)
339     {                                                                                                     
340     size_t cur =0;
341     for(;cur<seqlist->size-count-1;++cur)
342     {
343         if(cmp(seqlist->data[cur],seqlist->data[cur+1]))
344         {
345         Swap(&seqlist->data[cur],&seqlist->data[cur+1]);
346         }
347     }
348     }
349     return ;
350 }
351 

选择排序
352 //选择排序
353 void SeqListSelectSort(SeqList *seqlist)
354 {
355     if(seqlist == NULL)
356     {
357     //非法输入
358     return ;
359     }
360     if(seqlist->size<=1)
361     {
362     //不需要排序
363     return ;
364     }                                                                                                     
365     //[0,bound)表示有序区间[bound,size)表示待排序区间
366     size_t bound=0;
367     for(;bound<seqlist->size-1;++bound)
368     {
369     size_t cur=bound+1;
370     for(;cur<seqlist->size;++cur)
371     {
372         if(seqlist->data[cur]<seqlist->data[bound])
373         {
374         Swap(&seqlist->data[cur],&seqlist->data[bound]);
375         }
376     }
377     }
378     return ;
379 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值