第三周项目一(3)

来源:互联网 发布:cdlinux安装软件 编辑:程序博客网 时间:2024/06/06 20:31
  1. /* 
  2. 烟台大学计算机学院 
  3.  
  4. 文件名称:xmyc.cpp 
  5.  
  6. 作者:李恩 
  7.  
  8. 完成日期:2017年9月20日 
  9.  
  10. 问题描述:顺序表建立,插入,销毁 
  11.  
  12. 输入描述:无 
  13.  
  14. 输出描述:顺序表插入后 
  15.  
  16. */  
  17.   
  18.   
  19.   
  20. #include <stdio.h>  
  21. #include <malloc.h>  
  22.   
  23. #define MaxSize 50//存储空间大小宏定义  
  24.   
  25.   
  26. typedef int ElemType;  //定义ElemType为int  
  27. typedef struct  
  28. {  
  29.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  30.     int length;  
  31. } SqList;  
  32. void InitList(SqList *&L);  
  33. bool  ListInsert(SqList *&L,int i,ElemType e);  
  34. void DispList(SqList *L);  
  35. bool ListEmpty(SqList *L);  
  36. int main()  
  37. {  
  38.     SqList *sq;  
  39.     InitList(sq);  
  40.     ListInsert(sq, 1, 5);  
  41.     ListInsert(sq, 2, 3);  
  42.     ListInsert(sq, 1, 4);  
  43.     DispList(sq);  
  44.     return 0;  
  45. }  
  46.   
  47. void InitList(SqList *&L)//初始化顺序表函数  
  48. {  
  49.     L=(SqList*)malloc(sizeof(SqList));  
  50.     L->length=0;  
  51. }  
  52.   
  53. bool ListInsert(SqList *&L,int i,ElemType e)//插入线性表函数  
  54. {  
  55.     int j;  
  56.     if(i<1 || i>L->length+1)//判断i是否有误  
  57.     {  
  58.         return false;  
  59.     }  
  60.     i--;  
  61.     for(j=L->length;j>i;j--)//后移  
  62.     {  
  63.   
  64.         L->data[j]=L->data[j-1];  
  65.     }  
  66.     L->data[i]=e;//插入e  
  67.     L->length++;  
  68.     return true;  
  69.   
  70.   
  71.   
  72. }  
  73.   
  74. void DispList(SqList *L)  
  75. {  
  76.     int i;  
  77.     if (ListEmpty(L))  
  78.         return;  
  79.     for (i=0; i<L->length; i++)  
  80.         printf("%d ",L->data[i]);  
  81.     printf("\n");  
  82. }//输出线性表  
  83. bool ListEmpty(SqList *L)  
  84. {  
  85.     return(L->length==0);  
  86. }//判断是否为空表  
原创粉丝点击