Chinaunix首页 | 论坛 | 博客
  • 博客访问: 293816
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 20:38
文章分类
文章存档

2016年(20)

2015年(56)

分类: 嵌入式

2016-02-28 20:07:28



点击(此处)折叠或打开

  1. 数据结构---线性表的顺序表示和实现
  2. 16年2月26日19:24:49
  3. 线性表的顺序表示,它的优点是容易实现随机存取线性表的第i个元素,但是在插入,删除的操作时,需要移动大量的元素,所以它适用于相对稳定的线性表,如职工工资表,学生学籍表等。以下是我写的源程序:

  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <stdlib.h>

  7. #define LIST_INIT_SIZE 100
  8. #define LISTINCREMENT 10

  9. typedef struct SqList {
  10.     int *elem; //动态分配内存的首地址
  11.     int length; //当前线性表的长度
  12.     int listsize; //当前分配的存储空间的容量
  13. }SqList, *pSqList;

  14. /* 初始化函数,主要是对SqList中的三个元素进行赋值初始化。 */
  15. /**
  16.  * Initialise the list.
  17.  * @param L [the list]
  18.  */
  19. void InitList(SqList *L)
  20. {
  21.     L->elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
  22.     if (!L->elem)
  23.     {
  24.         printf("Cannot malloc memory for L->elem.\n");
  25.         exit(-1);
  26.     }

  27.     L->length = 0;
  28.     L->listsize = LIST_INIT_SIZE;
  29. }

  30. /**
  31.  * Destroy the list.
  32.  * @param L [this list]
  33.  */
  34. void DestroyList(SqList *L)
  35. {
  36.     free(L->elem);
  37.     L->elem = NULL;
  38.     L->length = 0;
  39.     L->listsize = 0;
  40. }

  41. /**
  42.  * return the length of the list.
  43.  * @param L [the list]
  44.  * @return [description]
  45.  */
  46. int ListLength(SqList L)
  47. {
  48.     return L.length;
  49. }

  50. /**
  51.  * This function is test the list empty or not.
  52.  * @param L: the list.
  53.  * @return : is empty, return 1; else return 0.
  54.  */
  55. int IsListEmpty(SqList L)
  56. {
  57.     return L.length ? 0 : 1;
  58. }

  59. /* 在这个线性表中的某个位置插入某个元素,首先注意的是pos的值是否有效,另外,如果随着线性表中元素个数的增加,它的个数可能超过刚开始预定义的 LIST_INIT_SIZE,那样的话,就需要从新分配 LISTINCREMENT增量的内存,从新把分配内存的首地址赋给 L->elem。如果在pos的位置插入元素,pos位置之后的所有元素均需要移动一个位置。 */
  60. /**
  61.  * Insert one value the the list
  62.  * @param L [the list]
  63.  * @param pos [the position of this list]
  64.  * @param val [the value]
  65.  * @return [if success return 1]
  66.  */
  67. int InsertList(SqList *L, int pos, int val)
  68. {
  69.     int *newbase, *p, *q;

  70.     if (pos < 1 || pos > L->length + 1)
  71.     {
  72.         printf("Error position.\n");
  73.         exit(-1);
  74.     }

  75.     if (L->length >= L->listsize)
  76.     {
  77.         newbase = (int *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(int));
  78.         L->elem = newbase;
  79.         L->listsize += LISTINCREMENT;
  80.     }

  81.     p = L->elem + pos - 1;
  82.     q = L->elem + L->length - 1;
  83.     for (q; q >= p; q--)
  84.     {
  85.         *(q + 1) = *q;
  86.     }

  87.     *p = val;
  88.     L->length += 1;

  89.     return 1;
  90. }

  91. /* 遍历这个线性表。 */
  92. /**
  93.  * Traverse the list.
  94.  * @param L [the list]
  95.  */
  96. void TraverseList(SqList L)
  97. {
  98.     int i;

  99.     printf("The values of this list are:\n");
  100.     for (i = 0; i < L.length; i++)
  101.     {
  102.         printf("%d ", *(L.elem + i));
  103.     }

  104.     printf("\n");
  105. }

  106. /* 删除某个元素的操作,比如删除pos位置的元素,所有pos位置之后的元素都要向前移动一个位置。*/
  107. /**
  108.  * Delete one entry of this list.
  109.  * @param L [this list]
  110.  * @param pos [position in this list]
  111.  * @param val [the value of the deleted one]
  112.  * @return [if success, return 1]
  113.  */
  114. int DeleteList(SqList *L, int pos, int *val)
  115. {
  116.     int *p, *q;

  117.     if (pos < 1 || pos > L->length + 1)
  118.     {
  119.         printf("Error position.\n");
  120.         exit(-1);
  121.     }

  122.     p = L->elem + pos - 1;
  123.     q = L->elem + L->length - 1;

  124.     *val = *p;
  125.     for (p; p <= q; p++)
  126.     {
  127.         *p = *(p + 1);
  128.     }

  129.     L->length -= 1;

  130.     return 1;
  131. }

  132. int main(int argc, char const *argv[])
  133. {
  134.     SqList L;
  135.     int val;

  136.     InitList(&L);
  137.     printf("After Initialise : L.elem = %p, L.length = %d, L.listsize = %d.\n", L.elem, L.length, L.listsize);
  138.     if (IsListEmpty(L))
  139.     {
  140.         printf("The list is empty.\n");    
  141.     }
  142.     InsertList(&L, 1, 1);
  143.     InsertList(&L, 1, 2);
  144.     InsertList(&L, 1, 3);
  145.     InsertList(&L, 1, 4);
  146.     InsertList(&L, 1, 5);
  147.     InsertList(&L, 1, 6);
  148.     InsertList(&L, 1, 7);
  149.     InsertList(&L, 1, 8);
  150.     TraverseList(L);
  151.     if (IsListEmpty(L))
  152.     {
  153.         printf("The list is empty.\n");    
  154.     }
  155.     DeleteList(&L, 1, &val);
  156.     printf("The delete value is %d.\n", val);
  157.     DeleteList(&L, 5, &val);
  158.     printf("The delete value is %d.\n", val);
  159.     TraverseList(L);
  160.     DeleteList(&L, 12, &val);
  161.     return 0;
  162. }

  163. 程序运行结果是:
  164. After Initialise : L.elem = 0xa00b008, L.length = 0, L.listsize = 100.
  165. The list is empty.
  166. The values of this list are:
  167. 8 7 6 5 4 3 2 1
  168. The delete value is 8.
  169. The delete value is 3.
  170. The values of this list are:
  171. 7 6 5 4 2 1
  172. Error position.

阅读(1384) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~