• 线性表之顺序存储结构


    线性表的顺序存储是指用一组地址连续的存储单元一次存储线性表的数据元素。在C语言中,可以使用动态数组来实现线性表的顺序存储。

    定义:

       1: #define LIST_INIT_SIZE     100
       2: #define LIST_INCREMENT    10
       3:  
       4: typedef struct {
       5:     ElemType     *elem;
       6:     int         length;
       7:     int         listsize;    
       8: }SqList;
    操作:
       1: InitList(L);                          /* 构造线性表 */
       2: DestroyList(L);  /* 销毁线性表 */  
       3: ClearList(L);                        /* 将线性表置空 */
       4: ListEmpty(L);                        /* 判断线性表是否为空 */
       5: ListLength(L);                       /* 返回线性表长度 */
       6: GetElem(L, i, &e);                   /* 用e返回L中第i个元素 */
       7: LocateElem(L, e, compare());         /* 返回L中第一个与e满足关系compare()的元素 */
       8: PriorElem(L, cur_e, &pre_e);         /* 返回cur_e的前驱元素 */
       9: NextElem(L, cur_e, &next_e);         /* 返回cur_e的后继元素 */
      10: ListInsert(L, i, e);                 /* 在L中位置i之前插入元素e */
      11: ListDelete(L, i, &e);                /* 删除L中第i个元素 */
      12: ListTraverse(L, visit());            /* 对每个元素调用visit() */
    实现:
       1: typedef int ElemType;
       2:  
       3: #define LIST_INIT_SIZE     100
       4: #define LIST_INCREMENT    10
       5:  
       6: typedef struct {
       7:     ElemType     *elem;
       8:     int         length;
       9:     int         listsize;    
      10: }SqList;
      11:  
      12: int InitList(SqList *L)
      13: {
      14:     L->elem = (ElemType *)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
      15:     if (!L->elem)
      16:         return -1;
      17:     L->length = 0;
      18:     L->listsize = LIST_INIT_SIZE;
      19:     
      20:     return 0;
      21: }
      22:  
      23: void DestroyList(SqList *L)
      24: {
      25:     free(L->elem);
      26:     L->length = 0;
      27:     L->listsize = 0;
      28: }
      29:  
      30: void ClearList(SqList *L)
      31: {
      32:     L->length = 0;
      33: }
      34:  
      35: bool ListEmpty(SqList *L)
      36: {
      37:     return (L->length == 0);
      38: }
      39:  
      40: int ListLength(SqList *L)
      41: {
      42:     return L->length;
      43: }
      44:  
      45: int GetElem(SqList *L, int i, ElemType *e)
      46: {
      47:     if (i < 0 || i >= L->length)
      48:         return -1;
      49:     
      50:     *e = L->elem[i];
      51:     
      52:     return 0;    
      53: }
      54:  
      55: int LocateElem(SqList *L, ElemType e, bool (*compare)(ElemType a, ElemType b))
      56: {
      57:     int i;
      58:     
      59:     for (i = 0; i < L->length; ++i) {
      60:         if (compare(L->elem[i], e))
      61:             return i;
      62:     }    
      63:     
      64:     return -1;
      65: }
      66:  
      67: int PriorElem(SqList *L, ElemType e, ElemType *pre_e)
      68: {
      69:     int i;
      70:     
      71:     for (i = 1; i < L->length; ++i) {
      72:         if (e == L->elem[i]) {
      73:             *pre_e = L->elem[i-1];
      74:             return 0;
      75:         }
      76:     }
      77:     
      78:     return -1;
      79: }
      80:  
      81: int NextElem(SqList *L, ElemType e, ElemType *next_e)
      82: {
      83:     int i;
      84:     
      85:     for (i = 0; i < L->length - 1; ++i) {
      86:         if (e == L->elem[i]) {
      87:             *next_e = L->elem[i+1];
      88:             return 0;
      89:         }
      90:     }
      91:     
      92:     return -1;
      93: }
      94:  
      95: int ListInsert(SqList *L, int i, ElemType e)
      96: {
      97:     if (i < 0 || i > L->length) {
      98:         return -1;
      99:     }
     100:     
     101:     if (L->length >= L->listsize) {
     102:         ElemType *newbase = (ElemType *)realloc(L->elem, sizeof(ElemType) * (L->listsize + LIST_INCREMENT));
     103:         if (!newbase) {
     104:             return -1;
     105:         }
     106:         L->elem = newbase;        
     107:         L->listsize += LIST_INCREMENT;
     108:     }
     109:     
     110:     ElemType *p, *q;
     111:     q = L->elem + i;
     112:     for (p = L->elem + L->length - 1; p >= q; --p)
     113:         *(p+1) = *p;
     114:         
     115:     *q = e;    
     116:     L->length += 1;
     117:     
     118:     return 0;    
     119: }
     120:  
     121: int ListDelete(SqList *L, int i, ElemType *e)
     122: {
     123:     if (i < 0 || i >= L->length) {
     124:         return -1;
     125:     }    
     126:     
     127:     *e = L->elem[i];
     128:     
     129:     ElemType *p;
     130:     for (p = L->elem + i; p < L->elem + L->length - 1; ++p) {
     131:         *p = *(p+1);        
     132:     }
     133:     L->length -= 1;
     134:     
     135:     return 0;
     136: }
     137:  
     138: int ListTraverse(SqList *L, int (*visit)(ElemType e))
     139: {
     140:     int i, ret;
     141:     
     142:     for (i = 0; i < L->length; ++i) {
     143:         ret = visit(L->elem[i]);
     144:         if (ret != 0)
     145:             return ret;
     146:     }
     147:         
     148:     return 0;        
     149: }
     
  • 相关阅读:
    Mysql添加用户和数据库
    Ubuntu Apache vhost不执行php小记
    buff/cache内存占用过多
    yii2 返回json和文件下载
    yii2 activeform 替換 form-gruop
    VSCode+Ionic+Apache Ripple开发环境搭建
    安装ionic出现node-sass无法下载的解决方法
    VS2015 + Cordova Html5开发使用Crosswalk Web引擎
    visual studio 2015 + Cordova 开发环境搭建
    ADSL自动更换IP地址源代码
  • 原文地址:https://www.cnblogs.com/newth/p/2478116.html
Copyright © 2020-2023  润新知