数据结构第二章--线性表

一、概念 

线性结构的特点是:在数据元素的非空有限集中,(1)存在唯一的一个被称做“第一个”的数据元素;(2)存在唯一的一个被称作“最后一个”的数据元素;(3)除第一个之外,集合中的每个数据元素均只有一个前驱;(4)除最后一个数据之外,集合中每个数据元素均只有一个后继。


二、线性表的应用实例

例1:假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员,聚合中没有相同的元素),求一个新的集合A=AUB。

void Union(List &LA,List &LB)

{

int iLenA = ListLen(LA);int iLenB = ListLen(LB);//求线性表的长度

for(int i = 0; i < iLenB; ++i)

{

GetElem(LB, i, e);//依次获取LB中的元素

if(!LocateElem(La, e, equal))ListInsert(LA, ++iLenA, e);//将LB中的元素与LA中元素进行比较,如果在LA中不存在,则插入到LA中

}

}


时间复杂度为:O(T) = O(iLenA*iLenB)


例二:归并LA和LB到LC中。(LA、LB中的数据按值非递减有序排列,里面可能有重复的值,要求LC也一样)

void MergeList(LA, LB, LC)

{

InitList(LC);

i = j = 0; k = 0;

iLenA = ListLength(LA);iLenB = ListLength(LB);

while(i < iLenA-1 && j < iLenB-1)

{

GetElem(LA, i , a);GetElem(LB, j, b);

if(a < b){ ListInsert(LC, ++k, a);++i; }

else{ListInsert(LC, ++k; b);++j; }

}

while(i < LA-1)

{GetElem(LA, i++ , a); ListInsert(LC, ++k, a);}

while(j < LB-1)

{GetElem(LB, j++ , b); ListInsert(LC, ++k,b); }

}

时间复杂度为O(T) = O(iLenA+iLenB);


三、线性表的顺序表示和实现

线性表的顺序表示指的是用一组地址连续的存储单元依次存储线性表的数据元素。线性表的这种机内表示称作线性表的顺序存储结构或顺序映象,反之,称这种存储结构的线性表为顺序表。


3.1 顺序存储线性表:

线性表的动态分配顺序存储结构的表示:

#define  LIST_INIT_SIZE 100//线性表存储空间的初始分配量

#defime  LISTINCREMENT 10 //线性表存储空间的分配增量

typedef struct {

ElemType  *elem; //存储空间基地址

int  iLength; //当前长度

int  iListSize; //当前分配的存储容量(以sizeof(ElemType)为单位)

}

常见操作的复杂度:

插入、删除的时间复杂度都为O(length)

求表长、取第i个数据元素的时间复杂度为O(1)

LocateElem的时间复杂度为O(length),查找某个元素是否在线性表中



转自http://blog.csdn.net/agul_/article/details/8634619

1.线性表的定义:零个或多个相同类型的数据元素的有限序列。若元素为多个,则第一个元素无前驱,最后一个元素无后继,其他的每个元素都有且只有一个前驱和后继。线性表中的每个数据元素可以由若干个数据项组成,这时,数据元素称为记录record,线性表称为文件file。

2.线性表的抽象数据类型:

    线性表是一种数据结构,其元素之间存在一一对应的逻辑关系{D,S}。D表示数据元素,S表示数据元素之间的关系。

   线性表的抽象数据类型{D,S,P},其中{D,S}表示数据结构,P表示数据结构上的一组操作。

3.线性表的物理结构


3.1顺序存储结构:用一段地址连续的存储单元一次存储线性表的数据元素。C语言中用数组为例。

(1)描述线性表需要三个属性:存储空间的起始位置,线性表的最大存储容量(数组长度,即总的存储空间的长度),线性表的当前长度(线性表中数据元素的个数)。

(2)随机存取结构:LOC(ai)=LOC(a1)+(i-1)c;存取操作的时间复杂度为O(1)。

(3)插入:检验当前的list的长度是否超过定义的数组最大长度,检验插入的位置是否合理(插入第i个之前,因此i可以取length+1),移位(若插入的不在表尾),插入,表长加1。

(4)删除:检验list是否为空表,检验删除的位置是否合理,赋值,(若删除的不在表尾)移位,表长-1。

(5)时间复杂度:存取操作O(1),插入删除O(n)。插入平均移动次数为n/2,删除平均移动次数为(n-1)/2。

(6)顺序存储优点:无需表示元素之间的逻辑关系而额外增加存储空间。可以快速存取

     顺序存储缺点:删除插入操作需要大量移动元素。当线性表长度变化较大时,难以确定存储空间的容量。造成存储空间的碎片。

     适用:元素个数不太变化,更多是存取数据的应用。

线性表的顺序存储结构用一维数据实现。

优点:

1.无须为表示表中元素之间的逻辑关系而增加额外的存储空间

2.可以快速地存取表中任意位置的元素


缺点:

1.插入和删除操作需要移动大量的元素。

2.当线性表长度变化较大时,难以确定存储空间的容量。

3.造成存储空间的“碎片”


"Common.h"

[cpp]  view plain copy
  1. #ifndef COMMON_H_INCLUDED  
  2. #define COMMON_H_INCLUDED  
  3.   
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7. #include <stdarg.h>  
  8.   
  9. #define TRUE 1  
  10. #define FALSE 0  
  11. #define OK 1  
  12. #define ERROR 0  
  13. #define INFEASIBLE -1  
  14. #define OVERFLOW -2  
  15.   
  16. typedef int Status;  
  17. typedef int ElemType;  
  18.   
  19. #endif // COMMON_H_INCLUDED  

"List_Seq.h"

[cpp]  view plain copy
  1. //链表的顺序存储  
  2.   
  3. #ifndef LIST_SEQ_H_INCLUDED  
  4. #define LIST_SEQ_H_INCLUDED  
  5.   
  6. #include "Common.h"  
  7.   
  8. #define LIST_INIT_SIZE 100  
  9. #define LISTINCREMENT 10  
  10.   
  11. typedef struct {  
  12.     ElemType * elem;//存储空间基址  
  13.     int length;//当前长度  
  14.     int listsize;//当前分配的存储容量,以sizeof(ElemType)为单位  
  15. }SqList;  
  16.   
  17. Status InitList_Seq(SqList * list);  
  18. Status DestroyList_Seq(SqList * list);  
  19. Status ClearList_Seq(SqList * list);  
  20. Status ListEmpty_Seq(SqList * list);  
  21. Status ListLength_Seq(SqList * list);  
  22. void GetElem_Seq(SqList * list, int i, ElemType * e);  
  23. int LocateElem_Seq(SqList * list, ElemType e);  
  24. void PriorElem_Seq(SqList * list, ElemType cur_e, ElemType * pre_e);  
  25. void NextElem_Seq(SqList * list, ElemType cur_e, ElemType * next_e);  
  26. Status ListInsert_Seq(SqList * list, int i, ElemType e);  
  27. Status ListDelete_Seq(SqList * list, int i, ElemType * e);  
  28. void ListTraverse(SqList * list);  
  29.   
  30. #endif // LIST_SEQ_H_INCLUDED  

"List_Seq.c"

[cpp]  view plain copy
  1. #include "List_Seq.h"  
  2. Status InitList_Seq(SqList * list)  
  3. {  
  4.     list->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));  
  5.     if(!list->elem)  
  6.         exit(OVERFLOW);  
  7.     list->length = 0;  
  8.     list->listsize = LIST_INIT_SIZE;  
  9.   
  10.     return OK;  
  11. }  
  12.   
  13. Status DestroyList_Seq(SqList * list)  
  14. {  
  15.     if(!list->elem)  
  16.         exit(ERROR);  
  17.     free(list->elem);  
  18.     list->elem = NULL;  
  19.     list->length = 0;  
  20.     list->listsize = 0;  
  21.   
  22.     return OK;  
  23. }  
  24.   
  25. Status ClearList_Seq(SqList * list)  
  26. {  
  27.     if(!list->elem)  
  28.         exit(ERROR);  
  29.     list->length = 0;  
  30.     //list->listsize = LIST_INIT_SIZE;  
  31.   
  32.     return OK;  
  33. }  
  34.   
  35. Status ListEmpty_Seq(SqList * list)  
  36. {  
  37.     if(!list->elem)  
  38.         exit(ERROR);  
  39.     if(list->length == 0)  
  40.         return TRUE;  
  41.     else  
  42.         return FALSE;  
  43. }  
  44.   
  45. Status ListLength_Seq(SqList * list)  
  46. {  
  47.     if(!list->elem)  
  48.         exit(ERROR);  
  49.     return list->length;  
  50. }  
  51.   
  52. void GetElem_Seq(SqList * list, int i, ElemType * e)  
  53. {  
  54.     if(!list->elem || i>list->length || i<1)  
  55.         exit(ERROR);  
  56.     *e = list->elem[i-1];  
  57. }  
  58.   
  59. //返回与e相等的元素的位序  
  60. int LocateElem_Seq(SqList * list, ElemType e)  
  61. {  
  62.     if(!list->elem)  
  63.         exit(ERROR);  
  64.     int i;  
  65.     for(i=0; i<ListLength_Seq(list); i++)  
  66.     {  
  67.         if(list->elem[i] == e)  
  68.             return i+1;  
  69.     }  
  70.     return 0;  
  71. }  
  72.   
  73. //返回cur_e的前驱,保存在pre_e中  
  74. void PriorElem_Seq(SqList * list, ElemType cur_e, ElemType * pre_e)  
  75. {  
  76.     if(!list->elem)  
  77.         exit(ERROR);  
  78.     int i;  
  79.     for(i=0; i<ListLength_Seq(list); i++)  
  80.     {  
  81.         if(cur_e == list->elem[i] && i!=0)  
  82.             *pre_e = list->elem[i-1];  
  83.     }  
  84. }  
  85.   
  86. //返回cur_e的后继,保存在next_e中  
  87. void NextElem_Seq(SqList * list, ElemType cur_e, ElemType * next_e)  
  88. {  
  89.     if(!list->elem)  
  90.         exit(ERROR);  
  91.     int i;  
  92.     for(i=0; i<ListLength_Seq(list); i++)  
  93.     {  
  94.         if(cur_e == list->elem[i] && i!=ListLength_Seq(list)-1)  
  95.             *next_e = list->elem[i+1];  
  96.     }  
  97. }  
  98.   
  99. Status ListInsert_Seq(SqList * list, int i, ElemType e)  
  100. {  
  101.     if(!list->elem)  
  102.         exit(ERROR);  
  103.     if(i<1 || i>ListLength_Seq(list)+1)  
  104.         return ERROR;  
  105.     if(list->length == list->listsize)  
  106.     {  
  107.         ElemType * newbase = (ElemType *)realloc(list->elem, (list->listsize + LISTINCREMENT)*sizeof(ElemType));  
  108.         if(!newbase)  
  109.             exit(OVERFLOW);  
  110.         list->elem = newbase;  
  111.         list->listsize += LISTINCREMENT;  
  112.     }  
  113.     int j;  
  114.     for(j=ListLength_Seq(list); j>i-1; j--)  
  115.     {  
  116.         list->elem[j] = list->elem[j-1];  
  117.     }  
  118.     list->elem[i-1] = e;  
  119.     list->length++;  
  120.     return OK;  
  121. }  
  122.   
  123. Status ListDelete_Seq(SqList * list, int i, ElemType * e)  
  124. {  
  125.     if(!list->elem)  
  126.         exit(ERROR);  
  127.     if(i<1 || i>ListLength_Seq(list))  
  128.         return ERROR;  
  129.     int j;  
  130.     *e = list->elem[i-1];  
  131.     for(j=i-1; j<ListLength_Seq(list)-1; j++)  
  132.     {  
  133.         list->elem[j] = list->elem[j+1];  
  134.     }  
  135.     list->length--;  
  136.     return OK;  
  137. }  
  138.   
  139. void ListTraverse(SqList * list)  
  140. {  
  141.     if(!list->elem)  
  142.         exit(ERROR);  
  143.     int i;  
  144.     for(i=0; i<ListLength_Seq(list); i++)  
  145.     {  
  146.         printf("%d  ", list->elem[i]);  
  147.     }  
  148.     printf("\n");  
  149. }  

http://blog.csdn.net/ggxxkkll/article/details/8660949

线性表的顺序存储结构称为顺序表。

 

图解:

 

 

下面来看下顺序表的C++实现:


SeqList.h文件

[cpp]  view plain copy
  1. //SeqList.h 声明类SeqList,文件名为SeqList.h  
  2. #ifndef SeqList_H  
  3. #define SeqList_H  
  4. const int MaxSize=100;  //100只是示例性的数据,可以根据实际问题具体定义  
  5. template <class T>      //定义模板类SeqList  
  6. class SeqList  
  7. {  
  8. public:  
  9.    SeqList( );       //无参构造函数  
  10.    SeqList(T a[], int n);       //有参构造函数  
  11.    ~SeqList();             //析构函数为空  
  12.    int Length();           //求线性表的长度  
  13.    T Get(int i);         //按位查找,取线性表的第i个元素  
  14.    int Locate(T x);       //按值查找,求线性表中值为x的元素序号  
  15.    void Insert(int i, T x);  //在线性表中第i个位置插入值为x的元素  
  16.    T Delete(int i);        //删除线性表的第i个元素  
  17.    void PrintList();       //遍历线性表,按序号依次输出各元素  
  18. private:  
  19.    T data[MaxSize];      //存放数据元素的数组  
  20.    int length;            //线性表的长度  
  21. };  
  22. #endif  

SeqList.cpp文件

[cpp]  view plain copy
  1. //SeqList.cpp    
  2. #include "SeqList.h"  
  3. /* 
  4. *前置条件:顺序表不存在 
  5. *输    入:无 
  6. *功    能:构建一个顺序表 
  7. *输    出:无 
  8. *后置条件:构建一个顺序表 
  9. */  
  10. template <class T>   
  11. SeqList<T>:: SeqList( )  
  12. {  
  13.   length=0;  
  14. }  
  15. /* 
  16. *前置条件:顺序表不存在 
  17. *输    入:顺序表信息的数组形式a[],顺序表长度n 
  18. *功    能:将数组a[]中元素建为长度为n的顺序表 
  19. *输    出:无 
  20. *后置条件:构建一个顺序表 
  21. */  
  22. template <class T>   
  23. SeqList<T>:: SeqList(T a[], int n)  
  24. {  
  25.   if (n>MaxSize) throw "参数非法";  
  26.   for (int i=0; i<n; i++)    
  27.     data[i]=a[i];  
  28.   length=n;  
  29. }  
  30. /* 
  31. *前置条件:无 
  32. *输    入:无 
  33. *功    能:无 
  34. *输    出:无 
  35. *后置条件:无 
  36. */  
  37. template <class T>   
  38. SeqList<T>:: ~SeqList( )  
  39. {  
  40. }  
  41. /* 
  42. *前置条件:顺序表存在 
  43. *输    入:插入元素x,插入位置i 
  44. *功    能:将元素x插入到顺序表中位置i处 
  45. *输    出:无 
  46. *后置条件:顺序表插入新元素 
  47. */  
  48. template <class T>   
  49. void SeqList<T>::Insert(int i, T x)  
  50. {   
  51.     int j;  
  52.   if (length>=MaxSize) throw "上溢";  
  53.     if (i<1 || i>length+1) throw "位置";  
  54.   for (j=length; j>=i; j--)  
  55.   data[j]=data[j-1];   //注意第j个元素存在数组下标为j-1处  
  56.   data[i-1]=x;  
  57.   length++;  
  58. }  
  59.   
  60. /* 
  61. *前置条件:顺序表存在 
  62. *输    入:要删除元素位置i 
  63. *功    能:删除顺序表中位置为i的元素 
  64. *输    出:无 
  65. *后置条件:顺序表删除元素 
  66. */  
  67. template <class T>  
  68. T SeqList<T>::Delete(int i)  
  69. {   
  70.     int x,j;  
  71.   if (length==0) throw "下溢";  
  72.   if (i<1 || i>length) throw "位置";  
  73.   x=data[i-1];  
  74.   for (j=i; j<length; j++)  
  75.     data[j-1]=data[j];   //注意此处j已经是元素所在的数组下标  
  76.   length--;  
  77.   return x;  
  78. }  
  79. /* 
  80. *前置条件:顺序表存在 
  81. *输    入:无 
  82. *功    能:输出顺序表长度 
  83. *输    出:顺序表长度 
  84. *后置条件:顺序表不变 
  85. */  
  86. template <class T>  
  87. int SeqList<T>::Length()  
  88. {  
  89.      return length;  
  90. }  
  91. /* 
  92. *前置条件:顺序表存在 
  93. *输    入:查询元素位置i 
  94. *功    能:按位查找位置为i的元素并输出值 
  95. *输    出:查询元素的值 
  96. *后置条件:顺序表不变 
  97. */  
  98. template <class T>   
  99. T SeqList<T>::Get(int i)  
  100. {  
  101.   if (i<1 && i>length) throw "查找位置非法";  
  102.     else return data[i-1];  
  103. }  
  104. /* 
  105. *前置条件:顺序表存在 
  106. *输    入:查询元素值x 
  107. *功    能:按值查找值的元素并输出位置 
  108. *输    出:查询元素的位置 
  109. *后置条件:顺序表不变 
  110. */  
  111. template <class T>  
  112. int SeqList<T>::Locate(T x)  
  113. {       
  114.       for (int i=0; i<length; i++)  
  115.        if (data[i]==x)   
  116.          return i+1 ;  //下标为i的元素等于x,返回其序号i+1  
  117.       return 0;  //退出循环,说明查找失败  
  118. }  
  119. /* 
  120. *前置条件:顺序表存在 
  121. *输    入:无 
  122. *功    能:顺序表遍历 
  123. *输    出:输出所有元素 
  124. *后置条件:顺序表不变 
  125. */  
  126. template <class T>  
  127. void SeqList<T>::PrintList()  
  128. {  
  129.     for(int i=0;i<length;i++)  
  130.     cout<<data[i]<<endl;  
  131. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值