线性表的顺序表示

线性表的顺序表示终于马马虎虎的勉强写完了,

写的不是很完整,开始时比较不理解就是动态分

配内存,现在懂一点点了,数据结构落下很多了,

这几天要好好整了可怜可怜

  1. <PRE class=cpp name="code" sizcache="4" sizset="2"><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 24px">#include<iostream>  
  2. #include<stdlib.h>   
  3. #include<malloc.h>   
  4. using namespace std;  
  5.   
  6. #define    OK         1   
  7. #define    ERROR      0   
  8. #define    TRUE       1   
  9. #define    FALSE      0   
  10.   
  11. typedef  int  Status;  
  12. typedef  int  ElemType;  
  13. typedef struct{  
  14.         int* elem;  
  15.         int length;  
  16.         int listsize;  
  17. }Sqlist;  
  18.   
  19. Status InitList(Sqlist &L)       //线性表的初始化   
  20. {  
  21.        L.elem = (int *)malloc(10 * sizeof(ElemType));  
  22.        L.length = 0;  
  23.        L.listsize = 10;  
  24.        return OK;  
  25. }  
  26.   
  27. Status CreatList(Sqlist &L)      //线性表的创建   
  28. {  
  29.     Status i,n;  
  30.     cout<<"please input list length:"<<endl;  
  31.     cin>>n;  
  32.     L.length = n;  
  33.     cout<<"please input L.elem:"<<endl;  
  34.     for(i=0;i<n;i++)  
  35.        cin>>L.elem[i];  
  36.     return OK;     
  37. }   
  38.   
  39. Status DisplayList(Sqlist &L)      //线性表元素展示   
  40. {  
  41.        Status i;  
  42.        for(i=0;i<L.length;i++)  
  43.            cout<<L.elem[i]<<" ";  
  44.        cout<<endl;  
  45.        return OK;  
  46. }  
  47.   
  48. Status ClearList(Sqlist &L)          //线性表的清空   
  49. {  
  50.     Status i;  
  51.     for(i=0;i<L.length;i++)  
  52.         L.elem[i] = 0;  
  53.     L.length = 0;  
  54.     return OK;  
  55. }   
  56.   
  57. Status ListEmpty(Sqlist &L)                      //判断线性表是否为空   
  58. {  
  59.      if(L.length == 0)  
  60.          return TRUE;  
  61.      else  
  62.          return FALSE;                                
  63. }  
  64.   
  65. Status ListLength(Sqlist &L)             //求线性表的长度   
  66. {  
  67.         return L.length;  
  68. }   
  69.   
  70. Status GetElem(Sqlist &L, Status &num, ElemType &e)            //用e返回线性表中第i个元素的值    
  71. {  
  72.        e = L.elem[num-1];  
  73.        return e;  
  74. }   
  75.   
  76. Status LocateElem(Sqlist &L, ElemType &e)             //返回元素e的位序,若不存在返回0   
  77. {  
  78.        Status i;  
  79.        for(i=0;i<L.length;i++)  
  80.        {  
  81.             if(L.elem[i] == e)  
  82.             break;  
  83.        }  
  84.        if(i<L.length)  
  85.           return (i+1);  
  86.        else  
  87.           return 0;  
  88. }   
  89.   
  90. Status PriorElem(Sqlist &L, ElemType &cur_e, ElemType  &pre_e)         //求元素的前驱   
  91. {  
  92.        Status i;  
  93.        i = LocateElem(L,cur_e);  
  94.        if(i == 1)  
  95.        {  
  96.            //free(pre_e);   
  97.            return ERROR;  
  98.        }  
  99.        else  
  100.           {  
  101.               pre_e = L.elem[i-2];  
  102.               return pre_e;  
  103.           }  
  104. }   
  105.   
  106. Status NextElem(Sqlist &L, ElemType  &cur_e,  ElemType  &next_e)            //求元素后继    
  107. {  
  108.        Status i;  
  109.        i = LocateElem(L, cur_e);  
  110.        if(i == L.length)  
  111.        {  
  112.            //free(next_e);   
  113.            return ERROR;  
  114.        }  
  115.        else  
  116.           {  
  117.               next_e = L.elem[i];  
  118.               return next_e;  
  119.           }       
  120. }  
  121.   
  122. Status ListInsert(Sqlist &L, Status &num, ElemType  &e)                 //在线性表i前插入元素e   
  123. {  
  124.        if(num<1 || num>L.length)  
  125.             return ERROR;  
  126.        else  
  127.        {  
  128.            Status j;  
  129.            if(L.length == 10)  
  130.            {  
  131.                L.elem = (int *)realloc(L.elem, (10+20)*sizeof(ElemType));  
  132.                L.listsize = 10 + 20;  
  133.            }  
  134.            for(j=L.length-1;j>=num-1;j--)  
  135.               L.elem[j+1] = L.elem[j];  
  136.            L.elem[num-1] = e;  
  137.            L.length++;  
  138.            return OK;  
  139.        }  
  140. }   
  141.   
  142. Status ListDelete(Sqlist &L, Status &num, ElemType &e)            //删除第i个数据元素   
  143. {  
  144.         Status j;  
  145.         if(num<1 || num>L.length)  
  146.             return ERROR;  
  147.         else  
  148.         {  
  149.             e = L.elem[num-1];  
  150.             for(j=num;j<L.length;j++)  
  151.                 L.elem[j-1] = L.elem[j];  
  152.             L.length--;  
  153.             return e;  
  154.         }  
  155. }   
  156.   
  157. Status DistroyList(Sqlist &L)        //线性表的销毁   
  158. {  
  159.     free(L.elem);  
  160.     return OK;  
  161. }   
  162.   
  163. Status main()  
  164. {  
  165.        int temp;  
  166.          
  167.        Sqlist L;  
  168.        ElemType e, cur_e, next_e;  
  169.        Status num;  
  170.        InitList(L);  
  171.        CreatList(L);  
  172.        DisplayList(L);  
  173.        cin>>num;  
  174.        temp = ListDelete(L, num, e);  
  175.        //ClearList(L);   
  176.        DisplayList(L);  
  177.        //temp = ListEmpty(L);   
  178.        cout<<temp<<endl;  
  179.        DistroyList(L);  
  180.        system("pause");  
  181.        return 0;  
  182. }   
  183. </SPAN></PRE>  
  184. <PRE></PRE>  
  185. <BR>  
  186. <PRE></PRE>  
  187. <PRE></PRE>  
  188. <P> </P>  
  189.   
  190. </PRE>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值