Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232430
  • 博文数量: 127
  • 博客积分: 34
  • 博客等级: 民兵
  • 技术积分: 655
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-03 10:53
文章分类

全部博文(127)

文章存档

2013年(19)

2012年(108)

分类: C/C++

2013-02-04 21:10:15

原文地址:数据结构之线性表(顺序表) 作者:


1.1 线性表的定义

   线性表(List):零个或多个数据元素的有限序列。

  在数据元素的非空有限集中,线性结构的特点:

 (1)存在唯一的一个被称为“第一个”的数据元素;

 (2)存在唯一的一个称为“最后一个”的数据元素;

 (3)除第一个之外,集合中的每个数据元素均只有一个前驱;

 (4)除最后一个之外,集合中的每个数据元素只有一个后继;

  优点:可以随机访问元素

  缺点:对于插入和删除元素,需要大量的位置移动


1.2 线性表的顺序表示

   线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素。

   来看线性表的动态分配的存储结构。    


  1. typedef ElemType int; //假设为int

  2. #define LIST_INIT_SIZE 100 //线性表存储空间初始分配值
  3. #define LISTINCREMENT 10 //线性表存储空间分配增量

  4. typedef struct
  5. {
  6.     ElemType *elem; //存储基地址
  7.     int listsize; //当前线性表存储空间分配大小
  8.     int length; //当前线性表大小
  9. }SqList;



1.3 线性表顺序结构代码实现


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. //宏定义
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define OK 1
  8. #define ERROR 0
  9. #define INFEASIBLE -1
  10. #define OVERFLOW -2

  11. #define LIST_INIT_SIZE 100         //线性表存储空间的初始分配量
  12. #define LISTINCREMENT 10           //线性表存储空间的分配增量

  13. typedef int ElemType;

  14. typedef struct LNode
  15. {
  16.     ElemType *elem;                //存储空间基地址
  17.     int length;                    //线性表当前长度
  18.     int listsize;                  //线性表存储空间实际分配大小
  19. }SqList;


  20. /* 函数名称:InitList
  21.  * 函数功能:构造空的线性表
  22.  * 返回值:
  23.  *        ERROR:malloc() error
  24.  *        OK   : 构造成功
  25. */
  26. int InitList(SqList *L, int length)
  27. {
  28.     if( 0 == length )
  29.     {
  30.         length = LIST_INIT_SIZE;                    //默认分配LIST_INIT_SIZE
  31.     }
  32.     L->elem = (ElemType *)malloc(sizeof(length));
  33.     if(NULL == L->elem)                             //空间分配失败
  34.     {
  35.         fprintf(stderr, "malloc() error.\n");
  36.         return ERROR;
  37.     }
  38.     L->listsize = length;                         
  39.     L->length = 0;                                

  40.     return OK;
  41. }

  42. /* 函数名称:InsertList
  43.  * 函数功能:在线性表的第i个位置插入元素e.(第i个位置,即为线性表中第i-1个元素)
  44.  * 返回值:
  45.  *        ERROR:i不合法或realloc失败
  46.  *        OK   :插入成功
  47. */
  48. int InsertList(SqList *L, ElemType e, int i)
  49. {
  50.     if(< 0 || i > L->length + 1)         //i不合法
  51.     {
  52.         return ERROR;
  53.     }

  54.     if(L->length >= L->listsize)            //空间不足
  55.     {
  56.         ElemType *newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
  57.         if(!newbase)
  58.         {
  59.             return ERROR;                   //realloc error
  60.         }
  61.         L->elem = newbase;
  62.         L->listsize = L->listsize + LISTINCREMENT;
  63.     }
  64.     
  65.     ElemType *= &(L->elem[i]);        
  66.     ElemType *= NULL;
  67.     for(= &(L->elem[L->length - 1]); p >= q; --p)
  68.     {
  69.         *(+ 1) = *p;
  70.     }
  71.     *= e;
  72.     
  73.     L->length++;
  74.     
  75.     return OK;
  76. }

  77. /* 函数名称:DeleteList
  78.  * 函数功能:删除线性表第i个元素,并将其保存到e中返回
  79.  * 返回值:
  80.  *        ERROR:i不合法
  81.  *        OK   :删除成功
  82. */
  83. int DeleteList(SqList *L, int i, ElemType *e)
  84. {
  85.     if(< 0 || i > L->length)            //i不合法
  86.     {
  87.         fprintf(stderr, "i不合法\n");
  88.         return ERROR;
  89.     }

  90.     ElemType *= &(L->elem[i]);    
  91.     *= *q;                              //获得第i个元素的值
  92.     
  93.     ElemType *= L->elem + L->length - 1;
  94.     
  95.     for( ; q < p; q++)                    //循环左移
  96.     {
  97.         *= *(+ 1);
  98.     }
  99.     
  100.     L->length--;                          //当前长度减1

  101.     return OK;
  102. }


  103. //快速排序
  104. int partition(SqList *L, ElemType low, ElemType high)
  105. {
  106.     ElemType key = L->elem[low];
  107.     
  108.     while(low < high)                                //
  109.     {
  110.         while(low < high && L->elem[high] >= key)    //
  111.         {
  112.             high--;
  113.         }
  114.         L->elem[low] = L->elem[high];                //
  115.         L->elem[high] = key;

  116.         while(low < high && L->elem[low] <= key)    //
  117.         {
  118.             low++;
  119.         }
  120.         L->elem[high] = L->elem[low];                //
  121.         L->elem[low] = key;
  122.     }
  123.     
  124.     return low;
  125. }

  126. void quickSort(SqList *L, ElemType low, ElemType high)
  127. {
  128.     ElemType key;
  129.     if(low < high)
  130.     {
  131.         key = partition(L, low, high);
  132.         quickSort(L, low, key - 1);
  133.         quickSort(L, key + 1, high);
  134.     }
  135. }

  136. /* 函数名称:MergeList
  137.  * 函数功能:将线性表La和线性表Lb合并到Lc中,并不删除元素
  138. */
  139. int MergeList(SqList La, SqList Lb, SqList *Lc)
  140. {
  141.     //线性表La和Lb中的元素均不递减,合并后的Lc也不递减
  142.     
  143.     ElemType *pa, *pb, *pc;
  144.     Lc->listsize = La.length + Lb.length;

  145.     InitList(Lc, Lc->listsize);

  146.     Lc->length = Lc->listsize;

  147.     pa = La.elem;
  148.     pb = Lb.elem;
  149.     pc = Lc->elem;

  150.     while(pa <= &(La.elem[La.length - 1]) && pb <= &(Lb.elem[Lb.length - 1]))
  151.     {
  152.         if(*pa <= *pb)
  153.         {
  154.             *pc++ = *pa++;
  155.         }
  156.         else
  157.         {
  158.             *pc++ = *pb++;
  159.         }
  160.     }
  161.     while(pa <= &(La.elem[La.length - 1]))
  162.     {
  163.         *pc++ = *pa++;
  164.     }
  165.     while(pb <= &(Lb.elem[Lb.length - 1]))
  166.     {
  167.         *pc++ = *pb++;
  168.     }
  169.     
  170.     return OK;
  171. }

  172. //打印线性表中元素
  173. void printList(SqList L)
  174. {
  175.     int i = 0;
  176.     for(= 0; i < L.length; i++)
  177.     {
  178.         printf("%d ",L.elem[i]);
  179.     }
  180.     printf("\n");
  181. }

  182. int main(int argc, char **argv)
  183. {
  184.     SqList La, Lb, Lc;
  185.     ElemType e;
  186.     int ret;
  187.     
  188.     ret = InitList(&La, LIST_INIT_SIZE);
  189.     if(ret == ERROR)
  190.     {
  191.         fprintf(stderr, "InitList() error.\n");
  192.         exit(EXIT_FAILURE);
  193.     }

  194.     int data[6] = {6, 5, 8, 3, 9, 4};
  195.     int i = 0;
  196.     for(= 0; i < 6; i++)
  197.     {
  198.          ret = InsertList(&La, data[i], i);    
  199.          if(ret != OK)
  200.          {
  201.                  fprintf(stderr, "插入元素失败.\n");
  202.                  exit(EXIT_FAILURE);
  203.          }
  204.     }

  205.     printf("初始化后的La:\n");
  206.     printList(La);

  207.     ret = DeleteList(&La, 3, &e);                     
  208.     if(ret != OK)
  209.     {
  210.         fprintf(stderr, "删除元素失败.\n");
  211.         exit(EXIT_FAILURE);
  212.     }
  213.     printf("删除第3个元素后的La:\n");
  214.     printList(La);

  215.     ret = InsertList(&La, e, 3);                
  216.     if(ret != OK)
  217.     {
  218.         fprintf(stderr, "插入元素失败.\n");
  219.         exit(EXIT_FAILURE);
  220.     }
  221.     printf("插入e后的La:\n");
  222.     printList(La);

  223.     //快速排序
  224.     quickSort(&La, 0, La.length - 1);
  225.     printf("排序后的La:\n");
  226.     printList(La);

  227.     printf("\n");
  228.     InitList(&Lb, LIST_INIT_SIZE);

  229.     int bdata[5] = {1, 3, 2, 4, 6};
  230.     for(= 0; i < 5; i++)
  231.     {
  232.         InsertList(&Lb, bdata[i], i);
  233.     }
  234.     printf("初始化后的Lb:\n");
  235.     printList(Lb);

  236.     //快速排序
  237.     quickSort(&Lb, 0, Lb.length - 1);
  238.     printf("排序后的Lb:\n");
  239.     printList(Lb);

  240.     MergeList(La, Lb, &Lc);
  241.     printf("La与Lb合并后的Lc:\n");
  242.     printList(Lc);

  243.     return 0;
  244. }
 


       

    参考:严蔚敏老师之《数据结构》

                       

                                                        梦醒潇湘love

                                                       2013-01-06 22:06


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