线性表——顺序表的基本操作

线性表的顺序存储结构及其基本操作的实现

  1 /*
  2   Name: 顺序表的实现
  3   Author: SUNPEISHUO
  4   Date: 2017/11/03 22:00 
  5 */
  6 #ifndef SQLIST_H_INCLUDED
  7 #define SQLIST_H_INCLUDED
  8 #include "ds.h" //for Status,OK ...
  9 #ifndef ElemType
 10 #define ElemType int /* 数据元素类型默认为 int */
 11 #define ELEMTYPE_TAG
 12 #endif
 13 /**********************************************************
 14 *  顺序表的存储结构定义 
 15 ***********************************************************/
 16 #define LIST_INIT_SIZE 100 /* 存储空间初始分配容量 */
 17 #define LISTINCREMENT 10 /* 存储空间分配的增量 */
 18    typedef struct {
 19        ElemType *elem;    //存储空间基址
 20        int length;        //当前长度 
 21        int listsize;      //当前已分配的存储空间(元素个数) 
 22    } SqList;
 23 
 24 /**********************************************************
 25 *  顺序表的基本操作声明
 26 ***********************************************************/
 27 
 28 //创建并初始化为空表 
 29    Status InitList(SqList &L);
 30 //销毁整个表(从此之后不再可用) 
 31    Status DestroyList(SqList &L);
 32 //将表L置空
 33    Status ClearList(SqList &L); 
 34 //判断表L是否为空表 
 35    bool ListEmpty(SqList L);
 36 //求表L的长度 
 37    int ListLength(SqList L);
 38 //取表L中的第i个元素,并用e返回. 操作成功返回OK,失败时返回ERROR 
 39    Status GetElem(SqList L, int i, ElemType &e);
 40     template <typename T> bool equal(T a, T b)
 41     {
 42     return a==b;
 43     }
 44 //在表L中定位元素e首次出现的位置. 操作成功返回位序,失败时返回0 
 45    int LocateElem(SqList L, ElemType e, bool (*compare)(ElemType,ElemType)=equal<ElemType>);
 46 //在表L中插入第i个元素e. 操作成功返回OK,失败时返回ERROR
 47    Status ListInsert(SqList &L, int i, ElemType e);
 48 //删除表L中第i个元素,结果用e返回. 操作成功返回OK,失败时返回ERROR 
 49    Status ListDelete(SqList &L, int i, ElemType &e);
 50 //遍历表L,对每个元素调用visit(x). 
 51    Status ListTraverse(SqList L, Status (*visit)(ElemType));
 52  
 53 /**********************************************************
 54 *  顺序表的基本操作的实现
 55 ***********************************************************/
 56 
 57 //创建并初始化为空表 
 58  Status InitList(SqList &L)
 59  {   L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
 60     if(!L.elem)  return ERROR;
 61     L.length=0;
 62     L.listsize=LIST_INIT_SIZE;
 63     return OK;
 64  }
 65 
 66 //销毁整个表(从此之后不再可用) 
 67 Status DestroyList(SqList &L)
 68  {
 69     return ERROR;  
 70  }
 71 
 72 //将表L置空 
 73 Status ClearList(SqList &L)
 74 {
 75   L.length=0;
 76   return OK;
 77 }
 78 
 79 //判断表L是否为空表 
 80 bool ListEmpty(SqList L)
 81 {
 82   if(L.length==0) return true;
 83     else return false;
 84 }
 85 
 86 //求表L的长度 
 87 int ListLength(SqList L)
 88 {
 89   return L.length;
 90 }
 91 
 92 //取表L中的第i个元素,并用e返回. 操作成功返回OK,失败时返回ERROR 
 93 Status GetElem(SqList L, int i, ElemType &e)
 94 { if(i<1||i>L.length) return ERROR;
 95      e=L.elem[i-1];
 96      return OK;
 97 }
 98    
 99 //在表L中定位元素e首次出现的位置. 操作成功返回位序,失败时返回0 ,compare(a,b) 为比较函数,匹配时返回true,否则返回false 
100  
101 int LocateElem(SqList L, ElemType e, bool (*compare)(ElemType,ElemType))
102 {
103     for (int j=0; j<L.length; j++)
104         if ( compare(L.elem[j],e) )  return j;
105     return 0;
106 }
107 
108 
109 //在表L中插入第i个元素e. 操作成功返回OK,失败时返回ERROR
110 Status ListInsert(SqList &L, int i, ElemType e)
111 {
112     if(i<0||i>L.length+1) return ERROR;
113     if(L.length>=L.listsize)
114     {
115         ElemType* newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
116         if(!newbase) return ERROR;
117         L.elem=newbase;
118         L.listsize=L.listsize+LISTINCREMENT;
119     }
120        ElemType* q=&(L.elem[i-1]);
121     for(ElemType* p=&(L.elem[L.length-1]);p>=q;--p)
122      *(p+1)=*p;
123       *q=e;
124       L.length++;
125       return OK;
126 
127 }
128 
129 //删除表L中第i个元素,结果用e返回. 操作成功返回OK,失败时返回ERROR 
130 Status ListDelete(SqList &L, int i, ElemType &e)
131 {   if(i<1||i>L.length) return ERROR;
132     ElemType* p=&(L.elem[i-1]);
133      e=*p;
134     for(ElemType* q=&(L.elem[L.length-1]);p<=q;p++)
135      *p=*(p+1);
136         L.length--;
137      return OK;
138 }
139 
140 //遍历表L,对每个元素调用visit(x). 
141 Status ListTraverse(SqList L, Status (*visit)(ElemType))
142 { 
143     for (int j=0; j<L.length; j++)
144         if ( ! visit(L.elem[j]) )  return ERROR;
145     return OK;
146 }
147 
148 #ifdef ELEMTYPE_TAG
149 #undef ElemType
150 #undef ELEMTYPE_TAG
151 #endif
152 #endif  // SQLIST_H_INCLUDED

 

posted @ 2017-11-03 22:33  北有孤鹜  阅读(389)  评论(0编辑  收藏  举报