第三周项目一 顺序表的基本运算

来源:互联网 发布:mysql 0 1 true false 编辑:程序博客网 时间:2024/06/05 02:56
  1. /* 
  2. *Copyright (c) 2016, 烟台大学计算机与控制工程学院 
  3. *All rights reserved. 
  4. *文件名称:aa 
  5. *作    者:申鹏鹏 
  6. *完成日期:2016年9月17日 
  7. *问题描述:在数据结构的学习中,掌握基本运算是一个基础性的工作。这种“抽象”级别的成果,适用于各种应用场合,也是训练计算思维的根本依托之一。 
  8. (1)初始化线性表InitList(&L):构造一个空的线性表L 
  9. (2)销毁线性表DestroyList(&L):释放线性表L占用的内存空间 
  10. (3)判线性表是否为空表ListEmpty(L):若L为空表,则返回真,否则返回假 
  11. (4)求线性表的长度ListLength(L):返回L中元素个数 
  12. (5)输出线性表DispList(L):当线性表L不为空时,顺序显示L中各节点的值域 
  13. (6)求线性表L中指定位置的某个数据元素GetElem(L,i,&e):用e返回L中第 i 个元素的值 
  14. (7)查找元素LocateElem(L,e):返回线性表L中第1个与e相等的序号,找不到返回0 
  15. (8)插入元素ListInsert(&L, i, &e):在线性表L中的第i个位置插入元素e; 
  16. (9)删除元素ListDelete(&L, i, &e):在线性表L中删除第i个元素,有e返回删除的值; 
  17. */  
  18. (1)目的是要测试“建立线性表”的算法CreateList,为查看建表的结果,需要实现“输出线性表”的算法DispList。在研习DispList中发现,要输出线性表,还要判断表是否为空,这样,实现判断线性表是否为空的算法ListEmpty成为必要。这样,再加上main函数,这个程序由4个函数构成。main函数用于写测试相关的代码。
    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.   
    17. //实现测试函数  
    18. int main()  
    19. {  
    20.     SqList *sq;  
    21.     ElemType x[6]= {5,8,7,2,4,9};  
    22.     CreateList(sq, x, 6);  
    23.     DispList(sq);  
    24.     return 0;  
    25. }  
    26.   
    27. //下面实现要测试的各个自定义函数  
    28. //用数组创建线性表  
    29. void CreateList(SqList *&L, ElemType a[], int n)  
    30. {  
    31.     int i;  
    32.     L=(SqList *)malloc(sizeof(SqList));  
    33.     for (i=0; i<n; i++)  
    34.         L->data[i]=a[i];  
    35.     L->length=n;  
    36. }  
    37.   
    38. //输出线性表DispList(L)  
    39. void DispList(SqList *L)  
    40. {  
    41.     int i;  
    42.     if (ListEmpty(L))  
    43.         return;  
    44.     for (i=0; i<L->length; i++)  
    45.         printf("%d ",L->data[i]);  
    46.     printf("\n");  
    47. }  
    48.   
    49. //判定是否为空表ListEmpty(L)  
    50. bool ListEmpty(SqList *L)  
    51. {  
    52.     return(L->length==0);  
    53. }
    54. 运行结果:
    55. (2)在已经创建线性表的基础上,求线性表的长度ListLength、求线性表L中指定位置的某个数据元素GetElem、查找元素LocateElem的算法都可以实现了。就在原程序的基础上增加:
         增加求线性表的长度ListLength的函数并测试; 
         增加求线性表L中指定位置的某个数据元素GetElem的函数并测试; 
         增加查找元素LocateElem的函数并测试

      1.head.h的代码

      1. #include <malloc.h>
      2. #include <stdio.h>    
      3. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
      4. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
      5. typedef struct  
      6. {  
      7.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
      8.     int length;  
      9. } SqList;  
      10.   
      11. //自定义函数声明部分  
      12. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
      13. void DispList(SqList *L);//输出线性表DispList(L)  
      14. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
      15. int ListLength(SqList *L); //求线性表的长度ListLength(L)  
      16. bool GetElem(SqList *L,int i,ElemType &e); //求某个数据元素值GetElem(L,i,e)  
      17. int LocateElem(SqList *L, ElemType e); //按元素值查找LocateElem(L,e)
       
    56. 2.main.cpp中的代码

      1. #include "head.h"  
      2. int main()  
      3. {  
      4.     SqList *sq;  
      5.     ElemType x[6]= {5,8,7,2,4,9};  
      6.     ElemType a;  
      7.     int loc;  
      8.     CreateList(sq, x, 6);  
      9.     DispList(sq);  
      10.   
      11.     printf("表长度:%d\n", ListLength(sq));  //测试求长度  
      12.   
      13.     if(GetElem(sq, 3, a))  //测试在范围内的情形  
      14.         printf("找到了第3个元素值为:%d\n", a);  
      15.     else  
      16.         printf("第3个元素超出范围!\n");  
      17.   
      18.     if(GetElem(sq, 15, a))  //测试不在范围内的情形  
      19.         printf("找到了第15个元素值为:%d\n", a);  
      20.     else  
      21.         printf("第15个元素超出范围!\n");  
      22.   
      23.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形  
      24.         printf("找到了,值为8的元素是第 %d 个\n", loc);  
      25.     else  
      26.         printf("值为8的元素木有找到!\n");  
      27.   
      28.     if((loc=LocateElem(sq, 17))>0)  //测试不能找到的情形  
      29.         printf("找到了,值为17的元素是第 %d 个\n", loc);  
      30.     else  
      31.         printf("值为17的元素木有找到!\n");  
      32.   
      33.     return 0;  
      34. }
      3.创建线性表的基础代码

      1. //自定义函数声明部分    
      2. void CreateList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
      3. void DispList(SqList *L);//输出线性表DisList(L)    
      4. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
      5.     
      6. //实现自定义函数    
      7. void CreateList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
      8. {    
      9.     int i;    
      10.     L=(SqList * )malloc(sizeof(SqList));    
      11.     for(i=0;i<n;i++)    
      12.         L->data[i]=a[i];    
      13.     L->length=n;     
      14. }    
      15. void DispList(SqList *L)//输出线性表DisList(L)    
      16. {    
      17.     int i;    
      18.     for(i=0;i<L->length;i++)    
      19.         printf("%d ",L->data[i]);    
      20.     printf("\n");    
      21. }    
      22. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
      23. {    
      24.     return(L->length==0);    
      25. }
      4.查找,求长度等的代码

      1. #include"head.h"    
      2. //实现测试函数    
      3. int ListLength(SqList *L)//求线性表的长度ListLength(L)    
      4. {    
      5.     return (L->length);    
      6. }    
      7. bool GetElem(SqList *L,int i,ElemType &e) //求某个数据元素值GetElem(L,i,e)    
      8. {    
      9.     if(i<1||i>L->length)    
      10.         return false;    
      11.     e=L->data[i-1];    
      12.     return true;    
      13. }    
      14. int LocateElem(SqList *L, ElemType e) //按元素值查找LocateElem(L,e)    
      15. {    
      16.     int i=0;    
      17.     while(i<L->length &&L->data[i]!=e)    
      18.         i++;    
      19.     if(i>=L->length)    
      20.         return 0;    
      21.     else    
      22.         return i+1;    
      23. }
      运行结果:


    57. (3)其余的4个基本运算:插入数据元素ListInsert、删除数据元素ListDelete、初始化线性表InitList、销毁线性表DestroyList都可以同法完成。

    58. 1.初始化及插入的代码

      1. #include<stdio.h>    
      2. #include<malloc.h>    
      3. #define Maxsize 50    
      4. typedef int ElemType;    
      5. typedef struct    
      6. {    
      7.     ElemType data[Maxsize];    
      8.     int length;    
      9. }SqList;    
      10. //自定义函数声明部分    
      11. void CreateList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
      12. void DispList(SqList *L);//输出线性表DisList(L)    
      13. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
      14. void InitList(SqList *&L);//初始化线性表    
      15. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素    
      16. void CreateList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
      17. {    
      18.     int i;    
      19.     L=(SqList * )malloc(sizeof(SqList));    
      20.     for(i=0;i<n;i++)    
      21.         L->data[i]=a[i];    
      22.     L->length=n;     
      23. }    
      24. void DispList(SqList *L)//输出线性表DisList(L)    
      25. {    
      26.     int i;    
      27.     for(i=0;i<L->length;i++)    
      28.         printf("%d ",L->data[i]);    
      29.     printf("\n");    
      30. }    
      31. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
      32. {    
      33.     return(L->length==0);    
      34. }    
      35.     
      36. void InitList(SqList *&L)//初始化线性表    
      37. {    
      38.     L=(SqList *)malloc(sizeof(SqList));    
      39.     L->length=0;    
      40. }    
      41. bool ListInsert(SqList *&L,int i,ElemType e)//插入数据元素    
      42. {    
      43.     int j;    
      44.     if(i<1||i>L->length+1)    
      45.         return false;    
      46.     i--;    
      47.     for(j=L->length;j>i;j--)    
      48.         L->data[j]=L->data[j-1];    
      49.     L->data[i]=e;    
      50.     L->length++;    
      51.     return true;    
      52. }    
      53. int main()    
      54. {    
      55.     SqList *sq;    
      56.     InitList(sq);    
      57.     ListInsert(sq, 1, 5);    
      58.     ListInsert(sq, 2, 3);    
      59.     ListInsert(sq, 1, 4);    
      60.     DispList(sq);    
      61.     return 0;    
      62. }
      运行结果:


    59. 2.删除及销毁的代码

      1. #include<stdio.h>    
      2. #include<malloc.h>    
      3. #define Maxsize 50    
      4. typedef int ElemType;    
      5. typedef struct    
      6. {    
      7.     ElemType data[Maxsize];    
      8.     int length;    
      9. }SqList;    
      10. //自定义函数声明部分    
      11. void CreateList(SqList *&L,ElemType a[],int n);//用数组创建线性表    
      12. void DispList(SqList *L);//输出线性表DisList(L)    
      13. bool ListEmpty(SqList *L);//判断是否为空表ListEmpty(L)    
      14. void InitList(SqList *&L);//初始化线性表    
      15. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素    
      16. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素    
      17. void DestroyList(SqList *&L);//销毁线性表    
      18. void CreateList(SqList *&L,ElemType a[],int n)//用数组创建线性表    
      19. {    
      20.     int i;    
      21.     L=(SqList * )malloc(sizeof(SqList));    
      22.     for(i=0;i<n;i++)    
      23.         L->data[i]=a[i];    
      24.     L->length=n;     
      25. }    
      26. void DispList(SqList *L)//输出线性表DisList(L)    
      27. {    
      28.     int i;    
      29.     for(i=0;i<L->length;i++)    
      30.         printf("%d ",L->data[i]);    
      31.     printf("\n");    
      32. }    
      33. bool ListEmpty(SqList *L)//判断是否为空表ListEmpty(L)    
      34. {    
      35.     return(L->length==0);    
      36. }    
      37.     
      38. void InitList(SqList *&L)//初始化线性表    
      39. {    
      40.     L=(SqList *)malloc(sizeof(SqList));    
      41.     L->length=0;    
      42. }    
      43. bool ListInsert(SqList *&L,int i,ElemType e)//插入数据元素    
      44. {    
      45.     int j;    
      46.     if(i<1||i>L->length+1)    
      47.         return false;    
      48.     i--;    
      49.     for(j=L->length;j>i;j--)    
      50.         L->data[j]=L->data[j-1];    
      51.     L->data[i]=e;    
      52.     L->length++;    
      53.     return true;    
      54. }    
      55. bool ListDelete(SqList *&L,int i,ElemType e)//删除数据元素    
      56. {    
      57.     int j;    
      58.     if(i<1||i>L->length-1)    
      59.         return false;    
      60.     i--;    
      61.     e=L->data[i];    
      62.     for(j=i;j<L->length-1;j++)    
      63.         L->data[j]=L->data[j+1];    
      64.     L->length--;    
      65.     return true;    
      66. }    
      67. void DestroyList(SqList *&L)    
      68. {    
      69.     free(L);    
      70. }    
      71. int main()    
      72. {    
      73.     SqList *sq;    
      74.     InitList(sq);    
      75.     ListInsert(sq, 1, 5);    
      76.     ListInsert(sq, 2, 3);    
      77.     ListInsert(sq, 1, 4);    
      78.     printf("插入后的:\n");    
      79.     DispList(sq);    
      80.     ListDelete(sq, 1, 5);    
      81.     ListDelete(sq, 2, 3);    
      82.     printf("删除后的:\n");    
      83.     DispList(sq);    
      84.     
      85.     DestroyList(sq);    
      86.     printf("销毁后的:\n");    
      87.     DispList(sq);    
      88.     return 0;    
      89. }
      90. 运行结果:

      91. 总结:

        实现线性表的基本运算:

        1)初始化线性表InitList(&L):构造一个空的线性表L

        2)销毁线性表DestroyList(&L):释放线性表L占用的内存空间

        3)判线性表是否为空表ListEmpty(L):若L为空表,则返回真,否则返回假

        4)求线性表的长度ListLength(L):返回L中元素个数

        5)输出线性表DispList(L):当线性表L不为空时,顺序显示L中各节点的值域

        6)求线性表L中指定位置的某个数据元素GetElem(L,i,&e):用e返回L中第i个元素的值

        7)查找元素LocateElem(L,e):返回线性表L中第1个与e相等的序号,找不到返回0

        (8)插入数据元素ListInsert(SqList *&L,int i,ElemType e):在L的第i个元素之前插入新的元素e,L的长度增加1

        (9)删除数据元素ListDele(&L,i,&e):删除L的第i个数据元素,并用e返回其值,L的长度减1

        (10)销毁线性表DestroyList(SqList *&L):释放线性表L占用的内存空间

        学习心得:

        当我看到这个工程的时候,我是拒绝的!因为拼写的错误导致走了弯路,所幸终于写完了,让自己有所收获。


0 0
原创粉丝点击