数据结构---顺序表实现(源码)

复习一下数据结构,希望以后每周能坚持发一章。

第一章   顺序表

  1. /*****************************SqList**********************************
  2. Date:2008.10.22
  3. Author:ZORO
  4. WebSite:http://blog.csdn.net/sevenhu
  5. ======================================================================
  6. 参考资料:
  7. BOOK:数据结构(C语言版)     严蔚敏     清华大学出版社
  8. Page:19
  9. ======================================================================
  10. ADT SqList  {
  11.     InitList(&L)
  12.     DestroyList(&L)
  13.     ListPush(&L,i)
  14.     ClearList(&L)
  15.     ListEmpty(L)
  16.     ListLength(L)
  17.     GetElem(L,i,&e)
  18.     LocateElem(L,e)
  19.     PriorElem(L,cur_e,&pre_e)
  20.     NextElem(L,cur_e,&next_e)
  21.     ListInsert(&L, i, e)
  22.     ListDelete(&L, i, e)
  23.     ListTraverse(L)
  24. }ADT SqList
  25. ======================================================================
  26. note:VC++ 6.0测试通过。
  27. *********************************************************************/
  28. #include <iostream>
  29. #define     OK                  1
  30. #define     FALSE               0
  31. #define     ERROR               0
  32. #define     OVERFLOW            -1
  33. #define     NULL                0
  34. #define     LIST_INIT_SIZE      100     //顺序表存储空间的初始分配量
  35. #define     LISTINCREMENT       10      //顺序表存储空间的分配增量
  36. using namespace std;
  37. typedef int Status;
  38. typedef bool BOOL;
  39. typedef struct SQ{
  40.     int * elem;
  41.     int length;                         //顺序表当前大小
  42.     int listsize;                       //顺序表容量
  43. } SqList;
  44. Status InitList(SqList &L)
  45. {
  46.     //构造表
  47.     L.elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
  48.     
  49.     if (!L.elem)  {                     //空间分配失败
  50.         return OVERFLOW;
  51.     }
  52.     
  53.     L.length = 0;                       //初始化长度为0
  54.     L.listsize = LIST_INIT_SIZE;        //初始存储容量
  55.     return OK;
  56. //InitList_Sq
  57. Status ListPush(SqList &L, int i)
  58. {
  59.     //增加元素
  60.     if (!L.elem)  {
  61.         return ERROR;                   //表不存在
  62.     }
  63.     if (L.length == L.listsize)  {      //空间不足,重新分配
  64.         L.elem = (int *)realloc(L.elem,sizeof(int) * (LISTINCREMENT + L.listsize));
  65.         if (!L.elem)  {
  66.             return OVERFLOW;            //分配失败
  67.         }
  68.         L.listsize += LISTINCREMENT;    //新空间容量
  69.     }
  70.     L.elem [L.length] = i;
  71.     ++L.length;
  72.     return OK;
  73. }
  74. Status ListInsert(SqList &L, int i, int e)
  75. {
  76.     //在顺序表L中第i个位置前插入元素e
  77.     if (!L.elem)  {
  78.         return ERROR;                   //表不存在
  79.     }
  80.     
  81.     if (i > L.length - 1 || i < 1)  {   //i值不合法
  82.         return ERROR;
  83.     }
  84.     if (L.length == L.listsize)  {
  85.         int * newbase;                  //空间不足,重新分配
  86.         newbase = (int *)realloc(L.elem,sizeof(int) * (LISTINCREMENT + L.listsize));
  87.         L.elem = newbase;
  88.         if (!L.elem)  {
  89.             return OVERFLOW;            //分配失败
  90.         }
  91.         L.listsize += LISTINCREMENT;    //新空间容量
  92.     }
  93.     int * q = &L.elem[i-1];             //标记插入位置
  94.     for (int * p = &L.elem[L.length-1]; p >= q; --p)  {
  95.         *(p+1) = *p;                    //插入位置及其后元素后移一位
  96.     }
  97.     *q = e;                             //插入e
  98.     ++L.length;                         //长度加1
  99.     q = p = NULL;
  100.     return OK;
  101. }//ListInsert
  102. Status ListDelete(SqList &L, int i, int &e)
  103. {
  104.     //在顺序表L中删除第i个值并用e返回这个值
  105.     if (!L.elem)  {
  106.         return ERROR;                   //表不存在
  107.     }
  108.     
  109.     if (i < 1 || i > L.length - 1)  {
  110.         return ERROR;                   //i值不合法
  111.     }
  112.     int *p = &L.elem[i-1];              //i位置的元素地址
  113.     e = *p;                             
  114.     int *q = L.elem + L.length - 1;     //L的最后一个元素地址
  115.     for (++p ; p <= q; ++p)  {
  116.         *(p-1) = *p;                    //把i位置后的元素向前移一位
  117.     }
  118.     --L.length;                         //L的长度减1
  119.     return OK;
  120. }//ListDelete
  121. Status DestroyList(SqList &L)
  122. {
  123.     //销毁L表
  124.     if (!L.elem)  {
  125.         return ERROR;                   //表不存在
  126.     }
  127.     free(L.elem);                       //释放内存
  128.     return OK;
  129. }//DestroyList
  130. Status ClearList(SqList &L)
  131. {
  132.     //清空表
  133.     if (!L.elem)  {
  134.         return ERROR;                   //表不存在
  135.     }
  136.     L.length = 0;                       //表长清零
  137.     return OK;
  138. }//ClearList
  139. BOOL ListEmpty(SqList L)
  140. {
  141.     //判断表是否为空
  142.     if (!L.elem)  {
  143.         return ERROR;                   //表不存在
  144.     }
  145.     if (L.length == 0)  {
  146.         return true;
  147.     }
  148.     
  149.     return false;
  150. }//ListEmpty
  151. int ListLength(SqList L)
  152. {
  153.     //表元素的个数
  154.     if (!L.elem)  {
  155.         return ERROR;                   //表不存在
  156.     }
  157.     return L.length;
  158. }//ListLength
  159. Status GetElem(SqList L, int i, int &e)
  160. {
  161.     //用e返回L表中第i个元素
  162.     if (!L.elem)  {
  163.         return ERROR;                   //表不存在
  164.     }
  165.     if (i < 1 || i > L.length - 1)  {
  166.         return ERROR;                   //i值不合法
  167.     }
  168.     int * q = L.elem ;
  169.     e = *(q + i -1);                    //把第i个元素给e
  170.     
  171.     return OK;
  172. }//GetElem
  173. int LocateElem(SqList L, int e)
  174. {
  175.     //返回与e相等的第一个元素的位置
  176.     if (!L.elem)  {
  177.         return ERROR;                   //表不存在
  178.     }
  179.     int i = 1;
  180.     int * p = L.elem ;
  181.     while (i < L.length && *p++ != e ) {//查找与e相等的值的位置
  182.         ++i;
  183.     }
  184.     
  185.     if (i <= L.length )  {
  186.         return i;
  187.     }
  188.     return 0;
  189. }
  190. Status PriorElem(SqList L, int cur_e, int & pre_e)
  191. {
  192.     //若cur_e是L的数据元素,且不是第一个,用pre_e返回它的前驱,否则操作失败,pre_e无定义
  193.     if (!L.elem)  {
  194.         return ERROR;                   //表不存在
  195.     }
  196.     int i = LocateElem(L,cur_e);
  197.     if (i == 1)  {
  198.         return FALSE;
  199.     }
  200.     else if(i == 0)  {
  201.         return FALSE;
  202.     }
  203.     pre_e = L.elem [i-2];               //前驱值给pre_e
  204.     return OK;
  205. }//PriorElem
  206. Status NextElem(SqList L, int cur_e, int & next_e)
  207. {
  208.     //若cur_e是L的数据元素,且不是最后一个,用next_e返回它的后继,否则操作失败,next_e无定义
  209.     if (!L.elem)  {
  210.         return ERROR;                   //表不存在
  211.     }
  212.     int i = LocateElem(L,cur_e);
  213.     if (i == L.length )  {
  214.         return FALSE;
  215.     }
  216.     else if (i == 0)  {
  217.         return FALSE;
  218.     }
  219.     next_e = L.elem [i];                //后继值给next_e
  220.     return OK;
  221. }//NextElem
  222. Status ListTraverse(SqList L)
  223. {
  224.     //遍历L表
  225.     if (!L.elem)  {
  226.         return ERROR;                   //表不存在
  227.     }
  228.     for (int i = 0 ; i < L.length ; ++i)  {
  229.         cout << L.elem[i] << " ";       //输出
  230.     }
  231.     cout << endl;
  232.     return OK;
  233. }//ListTraverse
  234. int main()
  235. {
  236.     SqList L;
  237.     if (InitList(L) == OK)  {
  238.         cout << "成功创建顺序表!" << endl;
  239.     }
  240.     else  {
  241.         cout << "创建顺序表失败!" << endl;
  242.         return FALSE;
  243.     }
  244.     for (int i = 0; i < 15; ++i)  {
  245.         if (ListPush(L,i) != OK)  {
  246.             cout << "录入数据失败!" << endl;
  247.             return FALSE;
  248.         }
  249.     }
  250.     cout << "成功录入数据!" << endl;
  251.     cout << "遍历顺序表!" << endl;
  252.     ListTraverse(L);
  253.     cout << "共" << ListLength(L) << "个元素" << endl;
  254.     cout << "清空顺序表!" << endl;
  255.     ClearList(L);
  256.     cout << "是否是空表?" << endl;   
  257.     cout << ListEmpty(L) << endl;
  258.     for (i = 15; i > 0; --i)  {
  259.         if (ListPush(L,i) != OK)  {
  260.             cout << "录入数据失败!" << endl;
  261.             return FALSE;
  262.         }
  263.     }
  264.     cout << "成功录入数据!" << endl;
  265.     cout << "遍历顺序表!" << endl;
  266.     ListTraverse(L);
  267.     int e;
  268.     GetElem(L,6,e);
  269.     cout << "第6个元素的值:"<< e << endl; 
  270.     PriorElem(L,7,e);
  271.     cout << "元素7的前驱元素:" << e << endl;
  272.     NextElem(L,7,e);
  273.     cout << "元素7的后继元素:" << e << endl;
  274.     cout << "在第4个位置插入元素100" << endl;
  275.     ListInsert(L,4,100);
  276.     ListTraverse(L);
  277.     cout << "删除第6个位置的元素" << endl;
  278.     ListDelete(L,6,e);
  279.     cout << "删除的元素为:" << e << endl;
  280.     ListTraverse(L);
  281.     cout << "元素4的位置是" << LocateElem(L,4) << endl;
  282.     
  283.     if (DestroyList(L)  == OK)  {
  284.         cout << "成功销毁顺序表!" << endl;
  285.         return 0;
  286.     }
  287.     else
  288.     {
  289.         cout << "销毁顺序表失败!" << endl;
  290.         return FALSE;
  291.     }
  292. }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值