第三周项目一3

  1. 烟台大学计算机学院 
  2.  
  3. 作者: 王雪行
  4.  
  5. 问题描述:顺序表建立,插入,销毁 
  6.  
  7. 输入描述:无 
  8.  
  9. 输出描述:顺序表插入后 
  10.  
  11. */  
  12.   
  13.   
  14.   
  15. #include <stdio.h>  
  16. #include <malloc.h>  
  17.   
  18. #define MaxSize 50//存储空间大小宏定义  
  19.   
  20.   
  21. typedef int ElemType;  //定义ElemType为int  
  22. typedef struct  
  23. {  
  24.     ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义  
  25.     int length;  
  26. } SqList;  
  27. void InitList(SqList *&L);  
  28. bool  ListInsert(SqList *&L,int i,ElemType e);  
  29. void DispList(SqList *L);  
  30. bool ListEmpty(SqList *L);  
  31. int main()  
  32. {  
  33.     SqList *sq;  
  34.     InitList(sq);  
  35.     ListInsert(sq, 1, 5);  
  36.     ListInsert(sq, 2, 3);  
  37.     ListInsert(sq, 1, 4);  
  38.     DispList(sq);  
  39.     return 0;  
  40. }  
  41.   
  42. void InitList(SqList *&L)//初始化顺序表函数  
  43. {  
  44.     L=(SqList*)malloc(sizeof(SqList));  
  45.     L->length=0;  
  46. }  
  47.   
  48. bool ListInsert(SqList *&L,int i,ElemType e)//插入线性表函数  
  49. {  
  50.     int j;  
  51.     if(i<1 || i>L->length+1)//判断i是否有误  
  52.     {  
  53.         return false;  
  54.     }  
  55.     i--;  
  56.     for(j=L->length;j>i;j--)//后移  
  57.     {  
  58.   
  59.         L->data[j]=L->data[j-1];  
  60.     }  
  61.     L->data[i]=e;//插入e  
  62.     L->length++;  
  63.     return true;  
  64.   
  65.   
  66.   
  67. }  
  68.   
  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. bool ListEmpty(SqList *L)  
  79. {  
  80.     return(L->length==0);  
  81. }//判断是否为空表  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值