第四周项目1-顺序表的基本运算

来源:互联网 发布:怎么用记事本编程 编辑:程序博客网 时间:2024/06/05 13:21

*   

* 烟台大学计算机与控制工程学院   

* 作者:王雪松 

* 完成日期:2016年9月22日   

* 问题及代码

* 问题描述:(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. int main()  
  18. {  
  19.     SqList *sq;  
  20.     ElemType x[6]= {5,8,7,2,4,9};  
  21.     CreateList(sq, x, 6);  
  22.     DispList(sq);  
  23.     return 0;  
  24. }  
  25.   
  26. //下面实现要测试的各个自定义函数  
  27. //用数组创建线性表  
  28. void CreateList(SqList *&L, ElemType a[], int n)  
  29. {  
  30.     int i;  
  31.     L=(SqList *)malloc(sizeof(SqList));  
  32.     for (i=0; i<n; i++)  
  33.         L->data[i]=a[i];  
  34.     L->length=n;  
  35. }  
  36.   
  37. //输出线性表DispList(L)  
  38. void DispList(SqList *L)  
  39. {  
  40.     int i;  
  41.     if (ListEmpty(L))  
  42.         return;  
  43.     for (i=0; i<L->length; i++)  
  44.         printf("%d ",L->data[i]);  
  45.     printf("\n");  
  46. }  
  47.   
  48. //判定是否为空表ListEmpty(L)  
  49. bool ListEmpty(SqList *L)  
  50. {  
  51.     return(L->length==0);  
  52. }  

运行结果:

(2)在已经创建线性表的基础上,求线性表的长度ListLength、求线性表L中指定位置的某个数据元素GetElem、查找元素LocateElem的算法都可以实现了。就在原程序的基础上增加:增加求线性表的长度ListLength的函数并测试;增加求线性表L中指定位置的某个数据元素GetElem的函数并测试;增加查找元素LocateElem的函数并测试

  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.   
  16.   
  17. //实现测试函数  
  18. int main()  
  19. {  
  20.     SqList *sq;  
  21.     InitList(sq);  
  22.     ListInsert(sq, 1, 5);  
  23.     ListInsert(sq, 2, 3);  
  24.     ListInsert(sq, 1, 4);  
  25.     DispList(sq);  
  26.     return 0;  
  27. }  
  28. //下面实现要测试的各个自定义函数  
  29. //用数组创建线性表  
  30. void CreateList(SqList *&L, ElemType a[], int n)  
  31. {  
  32.     int i;  
  33.     L=(SqList *)malloc(sizeof(SqList));  
  34.     for (i=0; i<n; i++)  
  35.         L->data[i]=a[i];  
  36.     L->length=n;  
  37. }  
  38.   
  39. //输出线性表DispList(L)  
  40. void DispList(SqList *L)  
  41. {  
  42.     int i;  
  43.     if (ListEmpty(L))  
  44.         return;  
  45.     for (i=0; i<L->length; i++)  
  46.         printf("%d ",L->data[i]);  
  47.     printf("\n");  
  48. }  
3
4
L
i
s
t
I
n
s
e
r
t
L
i
s
t
D
e
l
e
t
e
线
I
n
i
t
L
i
s
t
线
D
e
s
t
r
o
y
L
i
s
t
线
3
4
L
i
s
t
I
n
s
e
r
t
L
i
s
t
D
e
l
e
t
e
线
I
n
i
t
L
i
s
t
线
D
e
s
t
r
o
y
L
i
s
t
线
(3)其余的4个基本运算:插入数据元素ListInsert、删除数据元素ListDelete、初始化线性表InitList、销毁线性表DestroyList都可以同法完成,请自行安排实践路线。

  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4.   
  5. #define MaxSize 50    //Maxsize将用于后面定义存储空间的大小  
  6. typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int  
  7. typedef struct  
  8. {  
  9.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  10.     int length;  
  11. } SqList;  
  12.   
  13.    
  14.   
  15.    
  16.   
  17. //用数组创建线性表  
  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.   
  27. //初始化线性表InitList(L)  
  28. void InitList(SqList *&L)   //引用型指针  
  29. {  
  30.     L=(SqList *)malloc(sizeof(SqList));  
  31.     //分配存放线性表的空间  
  32.     L->length=0;  
  33. }  
  34.   
  35.    
  36.   
  37.   
  38. //销毁线性表DestroyList(L)  
  39. void DestroyList(SqList *&L)  
  40. {  
  41.     free(L);  
  42. }  
  43.   
  44. //判定是否为空表ListEmpty(L)  
  45. bool ListEmpty(SqList *L)  
  46. {  
  47.     return(L->length==0);  
  48. }  
  49.   
  50. //求线性表的长度ListLength(L)  
  51. int ListLength(SqList *L)  
  52. {  
  53.     return(L->length);  
  54. }  
  55.   
  56. //输出线性表DispList(L)  
  57. void DispList(SqList *L)  
  58. {  
  59.     int i;  
  60.     if (ListEmpty(L)) return;  
  61.     for (i=0; i<L->length; i++)  
  62.         printf("%d ",L->data[i]);  
  63.     printf("\n");  
  64. }  
  65.   
  66. //求某个数据元素值GetElem(L,i,e)  
  67. bool GetElem(SqList *L,int i,ElemType &e)  
  68. {  
  69.     if (i<1 || i>L->length)  return false;  
  70.     e=L->data[i-1];  
  71.     return true;  
  72. }  
  73.   
  74. //按元素值查找LocateElem(L,e)  
  75. int LocateElem(SqList *L, ElemType e)  
  76. {  
  77.     int i=0;  
  78.     while (i<L->length && L->data[i]!=e) i++;  
  79.     if (i>=L->length)  return 0;  
  80.     else  return i+1;  
  81. }  
  82.   
  83. //插入数据元素ListInsert(L,i,e)  
  84. bool ListInsert(SqList *&L,int i,ElemType e)  
  85. {  
  86.     int j;  
  87.     if (i<1 || i>L->length+1)  
  88.         return false;   //参数错误时返回false  
  89.     i--;            //将顺序表逻辑序号转化为物理序号  
  90.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
  91.         L->data[j]=L->data[j-1];  
  92.     L->data[i]=e;           //插入元素e  
  93.     L->length++;            //顺序表长度增1  
  94.     return true;            //成功插入返回true  
  95. }  
  96.   
  97. //删除数据元素ListDelete(L,i,e)  
  98. bool ListDelete(SqList *&L,int i,ElemType &e)  
  99. {  
  100.     int j;  
  101.     if (i<1 || i>L->length)  //参数错误时返回false  
  102.         return false;  
  103.     i--;        //将顺序表逻辑序号转化为物理序号  
  104.     e=L->data[i];  
  105.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
  106.         L->data[j]=L->data[j+1];  
  107.     L->length--;              //顺序表长度减1  
  108.     return true;              //成功删除返回true  
  109. }  
  110.   
  111.    
  112.   
  113. int main()  
  114. {  
  115.     SqList *sq;  
  116.     InitList(sq);  
  117.     ListInsert(sq, 1, 5);  
  118.     ListInsert(sq, 2, 3);  
  119.     ListInsert(sq, 1, 4);  
  120.     DispList(sq);  
  121.     return 0;  
  122. }  


运行结果:

 

学习总结:

       要熟用各个函数。
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
1
线
C
r
e
a
t
e
L
i
s
t
线
D
i
s
p
L
i
s
t
D
i
s
p
L
i
s
t
线
线
L
i
s
t
E
m
p
t
y
m
a
i
n
4
m
a
i
n
0 0