第三周——顺序表的基本运算

  1. /* 程序头部注释开始 
  2. * 程序的版权和版本声明部分 
  3. * Copyright (c) 2017, 烟台大学计算机学院学生 
  4. * All rights reserved. 
  5. * 文件名称:   shunxvbiao.cpp                           
  6. * 作    者:   胡俊超                           
  7. * 完成日期:   2017 年 9 月 19 日 
  8. * 版 本 号:   v1.0         
  9. * 问题描述:实现顺序表的基本运算
  10. * 程序输出:代码的复杂化
  11. */

    1. #include <stdio.h>  
    2. #include <malloc.h>  
    3.   
    4. #define MaxSize 50     //Maxsize将用于后面定义存储空间的大小  
    5. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
    6. typedef struct  
    7. {  
    8.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
    9.     int length;  
    10. } SqList;  
    11.   
    12. //自定义函数声明部分  
    13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
    14. void DispList(SqList *L);//输出线性表DispList(L)  
    15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
    16. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
    17. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
    18. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)  
    19.   
    20. //实现测试函数  
    21. int main()  
    22. {  
    23.     SqList *sq;  
    24.     ElemType x[6]= {5,8,7,2,4,9};  
    25.     ElemType a;  
    26.     int loc;  
    27.     CreateList(sq, x, 6);  
    28.     DispList(sq);  
    29.   
    30.     printf("表长度:%d\n", ListLength(sq));  //测试求长度  
    31.   
    32.     if(GetElem(sq, 3, a))  //测试在范围内的情形  
    33.         printf("找到了第3个元素值为:%d\n", a);  
    34.     else  
    35.         printf("第3个元素超出范围!\n");  
    36.   
    37.     if(GetElem(sq, 15, a))  //测试不在范围内的情形  
    38.         printf("找到了第15个元素值为:%d\n", a);  
    39.     else  
    40.         printf("第15个元素超出范围!\n");  
    41.   
    42.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
    43.         printf("找到了,值为8的元素是第 %d 个\n", loc);  
    44.     else  
    45.         printf("值为8的元素木有找到!\n");  
    46.   
    47.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
    48.         printf("找到了,值为17的元素是第 %d 个\n", loc);  
    49.     else  
    50.         printf("值为17的元素木有找到!\n");  
    51.   
    52.     return 0;  
    53. }  
    54.   
    55. //下面实现要测试的各个自定义函数  
    56. //用数组创建线性表  
    57. void CreateList(SqList *&L, ElemType a[], int n)  
    58. {  
    59.     int i;  
    60.     L=(SqList *)malloc(sizeof(SqList));  /*赋值号左边L是一个SqList *指针,指向名为SqList的结构体;赋值号右边,  
    61.                                           malloc(sizeof(SqList))是分配一块大小为sizeof(SqList)的内存,并返回首地址并赋值给左边的L指针,  
    62.                                           (SqList *)表示把这个地址强制转化为SqlList *的指针。*/  
    63.     for (i=0; i<n; i++)  
    64.         L->data[i]=a[i];  
    65.     L->length=n;  
    66. }  
    67.   
    68. //输出线性表DispList(L)  
    69. void DispList(SqList *L)  
    70. {  
    71.     int i;  
    72.     if (ListEmpty(L))  
    73.         return;  
    74.     for (i=0; i<L->length; i++)  
    75.         printf("%d ",L->data[i]);  
    76.     printf("\n");  
    77. }  
    78.   
    79. //判定是否为空表ListEmpty(L)  
    80. bool ListEmpty(SqList *L)  
    81. {  
    82.     return(L->length==0);     //巧妙利用等值返回bool型参数  
    83. }  
    84.   
    85. //求线性表的长度ListLength(L)  
    86. int ListLength(SqList *L)  
    87. {  
    88.     return(L->length);        //线性表中存在其自身的长度  
    89. }  
    90.   
    91. //求某个数据元素值GetElem(L,i,e)  
    92. bool GetElem(SqList *L,int i,ElemType &e)//利用引用  
    93. {  
    94.     if (i<1 || i>L->length)  
    95.         return false;  
    96.     e=L->data[i-1];  
    97.     return true;  
    98. }  
    99.   
    100. //按元素值查找LocateElem(L,e)  
    101. int LocateElem(SqList *L, ElemType e)  
    102. {  
    103.     int i=0;  
    104.     while (i<L->length && L->data[i]!=e) i++;  
    105.     if (i>=L->length)  
    106.         return 0;  
    107.     else  
    108.         return i+1;  
    109. }  
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4. #define MaxSize 50     //Maxsize将用于后面定义存储空间的大小  
  5. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  9.     int length;  
  10. } SqList;  
  11.   
  12. //自定义函数声明部分  
  13. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  14. void DispList(SqList *L);//输出线性表DispList(L)  
  15. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  16. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
  17. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
  18. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)  
  19.   
  20. //实现测试函数  
  21. int main()  
  22. {  
  23.     SqList *sq;  
  24.     ElemType x[6]= {5,8,7,2,4,9};  
  25.     ElemType a;  
  26.     int loc;  
  27.     CreateList(sq, x, 6);  
  28.     DispList(sq);  
  29.   
  30.     printf("表长度:%d\n", ListLength(sq));  //测试求长度  
  31.   
  32.     if(GetElem(sq, 3, a))  //测试在范围内的情形  
  33.         printf("找到了第3个元素值为:%d\n", a);  
  34.     else  
  35.         printf("第3个元素超出范围!\n");  
  36.   
  37.     if(GetElem(sq, 15, a))  //测试不在范围内的情形  
  38.         printf("找到了第15个元素值为:%d\n", a);  
  39.     else  
  40.         printf("第15个元素超出范围!\n");  
  41.   
  42.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
  43.         printf("找到了,值为8的元素是第 %d 个\n", loc);  
  44.     else  
  45.         printf("值为8的元素木有找到!\n");  
  46.   
  47.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
  48.         printf("找到了,值为17的元素是第 %d 个\n", loc);  
  49.     else  
  50.         printf("值为17的元素木有找到!\n");  
  51.   
  52.     return 0;  
  53. }  
  54.   
  55. //下面实现要测试的各个自定义函数  
  56. //用数组创建线性表  
  57. void CreateList(SqList *&L, ElemType a[], int n)  
  58. {  
  59.     int i;  
  60.     L=(SqList *)malloc(sizeof(SqList));  /*赋值号左边L是一个SqList *指针,指向名为SqList的结构体;赋值号右边,  
  61.                                           malloc(sizeof(SqList))是分配一块大小为sizeof(SqList)的内存,并返回首地址并赋值给左边的L指针,  
  62.                                           (SqList *)表示把这个地址强制转化为SqlList *的指针。*/  
  63.     for (i=0; i<n; i++)  
  64.         L->data[i]=a[i];  
  65.     L->length=n;  
  66. }  
  67.   
  68. //输出线性表DispList(L)  
  69. void DispList(SqList *L)  
  70. {  
  71.     int i;  
  72.     if (ListEmpty(L))  
  73.         return;  
  74.     for (i=0; i<L->length; i++)  
  75.         printf("%d ",L->data[i]);  
  76.     printf("\n");  
  77. }  
  78.   
  79. //判定是否为空表ListEmpty(L)  
  80. bool ListEmpty(SqList *L)  
  81. {  
  82.     return(L->length==0);     //巧妙利用等值返回bool型参数  
  83. }  
  84.   
  85. //求线性表的长度ListLength(L)  
  86. int ListLength(SqList *L)  
  87. {  
  88.     return(L->length);        //线性表中存在其自身的长度  
  89. }  
  90.   
  91. //求某个数据元素值GetElem(L,i,e)  
  92. bool GetElem(SqList *L,int i,ElemType &e)//利用引用  
  93. {  
  94.     if (i<1 || i>L->length)  
  95.         return false;  
  96.     e=L->data[i-1];  
  97.     return true;  
  98. }  
  99.   
  100. //按元素值查找LocateElem(L,e)  
  101. int LocateElem(SqList *L, ElemType e)  
  102. {  
  103.     int i=0;  
  104.     while (i<L->length && L->data[i]!=e) i++;  
  105.     if (i>=L->length)  
  106.         return 0;  
  107.     else  
  108.         return i+1;  
  109. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值