顺序表的实现

本文介绍了一种基于动态一维数组实现的顺序表,并提供了初始化、插入、删除等基本操作的C语言代码示例。此外,还展示了如何合并两个已排序的顺序表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

顺序表是线性表的一种,本质而言是一个动态的一维数组,我用的不多,或许没接触到用的地方吧。

一个简单的实现代码如下:

Code:
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #define LIST_SIZE 10/* the init allocate of list */  
  5. #define LISTINCREMENT 10/* the increment allocate of list */  
  6. #define Type int/* the element type*/  
  7. #define bool int  
  8. #define true 1  
  9. #define false 0  
  10.   
  11. struct seq_list {  
  12.     Type *base_addr;/* the base address of seq list */  
  13.     int length;/* the current length of seq list */  
  14.     int list_size;/* the capacity of seq list*/  
  15. };  
  16.   
  17. bool init_list(struct seq_list *sl)  
  18. {  
  19.     sl->base_addr = (Type *)malloc(LIST_SIZE * sizeof(Type));  
  20.     if (sl->base_addr == NULL){  
  21.         printf("Error: malloc failed!");  
  22.         exit(-1);  
  23.     }  
  24.     sl->length = 0;  
  25.     sl->list_size = LIST_SIZE;  
  26.     return true;  
  27. }  
  28.   
  29. /* insert e into sl before ith pos */  
  30. bool insert_list(struct seq_list *sl, int i, Type e)  
  31. {  
  32.     if (i < 0 || i > sl->length) {  
  33.         printf("Error: i is larger than length");  
  34.         return false;  
  35.     }  
  36.     if (sl->length >=  sl->list_size) {  
  37.         Type *newbase_addr = (Type *)realloc(sl->base_addr, (sl->list_size + LISTINCREMENT) * sizeof(Type));  
  38.         if (newbase_addr == NULL)  
  39.             exit(-1);  
  40.         sl->base_addr = newbase_addr;  
  41.         sl->list_size += LISTINCREMENT;  
  42.     }  
  43.       
  44.     Type *p;  
  45.     for (p = sl->base_addr + sl->length - 1; p >= sl->base_addr + i; p--)  
  46.         *(p + 1) = *p;  
  47.     *(p + 1) = e;  
  48.     ++sl->length;  
  49.     return true;  
  50. }  
  51.   
  52. /* delete the ith pos element in list */  
  53. bool delete_list(struct seq_list *sl, int i)  
  54. {  
  55.     if (i < 0 || i > sl->length) {  
  56.         printf("Error: i is larger than length");  
  57.         return false;  
  58.     }  
  59.       
  60.     Type *p;  
  61.     for ( p = sl->base_addr + i; p < sl->base_addr + sl->length - 1; p++)  
  62.         *p = *(p + 1);  
  63.     sl->length--;  
  64.     return true;  
  65. }  
  66.   
  67. void destory_list(struct seq_list *sl)  
  68. {  
  69.     free(sl->base_addr);  
  70.     sl->base_addr = NULL;  
  71.     sl->length = 0;  
  72.     sl->list_size = 0;  
  73. }  
  74.   
  75. void display(struct seq_list *sl)  
  76. {  
  77.     Type *p;  
  78.   
  79.     for (p = sl->base_addr; p < sl->base_addr + sl->length; p++)  
  80.         printf("%d ", *p);  
  81. }  
  82. int main()  
  83. {  
  84.     struct seq_list list;  
  85.     struct seq_list *plist = &list;  
  86.     int i;  
  87.       
  88.     init_list(plist);  
  89.     for (i = 0; i < 10; i++)  
  90.         insert_list(plist, i, i);  
  91.     display(plist);  
  92.       
  93.     delete_list(plist, 0);  
  94.     display(plist);   
  95.       
  96.     destory_list(plist);  
  97.     return 0;  
  98. }  

这里 i 属于 [0, length)。merge_list用于合并两个已按非递减顺序排列的顺序表。

  1. void merge_list(struct seq_list *sl1, struct seq_list *sl2, struct seq_list *sl)  
  2. {  
  3.     Type *p1, *p2, *p;  
  4.     int length;  
  5.       
  6.     p1 = sl1->base_addr;  
  7.     p2 = sl2->base_addr;  
  8.     length = sl1->length + sl2->length;  
  9.     sl->base_addr = (Type *)malloc(length * sizeof(Type));  
  10.     p = sl->base_addr;  
  11.       
  12.     if (sl->base_addr == NULL)  
  13.         exit(-1);  
  14.     while ((p1 < p1 + sl1->length) && (p2 < p2 + sl2->length)) {  
  15.         if (*p1 <= *p2)  
  16.             *p++ = *p1++;  
  17.         else  
  18.             *p++ = *p2++;  
  19.     }  
  20.     while (p1 < p1 + sl1->length)  
  21.         *p++ = *p1++;  
  22.     while (p2 < p2 + sl2->length)  
  23.         *p++ = *p2++;  
  24. }  

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值