第三周实践项目1(2)--顺序表的基本运算插入.删除数据,初始化.销毁线性表

来源:互联网 发布:java in是什么意思啊 编辑:程序博客网 时间:2024/06/14 05:15
  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. //声明自定义函数  
  14. void InitList(SqList *&L);   //初始化顺序表  
  15. void ListInsert(SqList *L,int i,int b);  //插入函数  
  16. void DispList(SqList *L);    //输出函数  
  17. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  18. void DestroyList(SqList *L);//释放线性表L占用的内存空间  
  19. int ListDelete(SqList *, int , int &);  
  20. bool ListEmpty(SqList *L);  
  21. int ListLength(SqList *L);  
  22. int LocateElem(SqList *l, ElemType e);  
  23. bool GetElem(SqList *l,int i,ElemType &e);  
  24. int main()  
  25. {  
  26.     SqList *sq;  
  27.     int e,a,loc;  
  28.     int i=2;  
  29.     InitList(sq);  
  30.     ListInsert(sq, 1, 5);  
  31.     ListInsert(sq, 2, 3);  
  32.     ListInsert(sq, 1, 4);  
  33.     DispList(sq);  
  34.     printf("线性表的长度:%d\n",ListLength(sq));  
  35.     if(GetElem(sq, 3, a))  //测试在范围内的情形     
  36.         printf("找到了第3个元素值为:%d\n", a);    
  37.     else    
  38.       printf("第3个元素超出范围!\n");    
  39.     if((loc=LocateElem(sq, 8))>0)  //测试能找到的情形     
  40.         printf("找到了,值为8的元素是第 %d 个\n", loc);    
  41.     else    
  42.        printf("值为8的元素木有找到!\n");    
  43.   
  44.     ListDelete(sq,i,e);  
  45.     printf("删除的元素是:%d\n",e);  
  46.     printf("删除后的线性表:");  
  47.     DispList(sq);  
  48.   
  49.   
  50.     DestroyList(sq);  
  51.     return 0;  
  52. }  
  53.   
  54. //输出线性表DispList(L)  
  55. void DispList(SqList *L)  
  56. {  
  57.     int i;  
  58.     if (ListEmpty(L))  
  59.         return;  
  60.     for (i=0; i<L->length; i++)  
  61.         printf("%d ",L->data[i]);  
  62.     printf("\n");  
  63. }  
  64.   
  65. //判定是否为空表ListEmpty(L)  
  66. bool ListEmpty(SqList *L)  
  67. {  
  68.     return(L->length==0);  
  69. }  
  70.   
  71. //初始化顺序表InitList(*L)  
  72. void InitList(SqList *&L)  
  73. {  
  74.     L=(SqList *)malloc(sizeof(SqList));//这里申请了结点空间  
  75.     L->length=0;  
  76.   
  77. }  
  78.   
  79. void ListInsert(SqList *L,int i,int b)   //插入函数  
  80. {  
  81.    int j;  
  82.   
  83.     if(i<1)  
  84.     {  
  85.         printf("插入位置非法/n");  
  86.     }  
  87.  i=i-1;  
  88.  if(L->length==0)  
  89.  {  
  90.    
  91.   L->data[i]=b;  
  92.   L->length++;  
  93.  }  
  94.   
  95.   
  96.  else   
  97.  {  
  98.     for(j=L->length;j>i;j--)  
  99.     {  
  100.         L->data[j]=L->data[j-1];  
  101.     }  
  102.     L->data[i]=b;  
  103.     L->length++;  
  104.  }  
  105.   
  106. }  
  107.   
  108. void DestroyList(SqList *L)  
  109. {  
  110.     free(L);  
  111.     printf("线性表已经被释放!线性表的长度:%d",L->length);  
  112.     printf("\n(由于线性表已经被释放所以显示的长度为随机值)");  
  113. }  
  114.   
  115. int ListDelete(SqList *L, int i, int &e)  
  116. {  
  117.     i--;  
  118.     int n=0;  
  119.     while(n<i)  
  120.         n++;  
  121.     if(L->length>n)  
  122.     {  
  123.         e=L->data[n];  
  124.         while(n<L->length-1)  
  125.         {  
  126.             L->data[n]=L->data[n+1];  
  127.             n++;  
  128.           
  129.         }  
  130.         L->length--;  
  131.           
  132.     }  
  133.     return e;  
  134.   
  135. }  
  136. int ListLength(SqList *L)  
  137. {  
  138.     return L->length;  
  139. }  
  140.   
  141. bool GetElem(SqList *l,int i,ElemType &e)    
  142. {    
  143.     if(i<1||i>l->length)    
  144.         return false;    
  145.     e=l->data[i-1];    
  146.         return true;    
  147.    
  148. }    
  149. int LocateElem(SqList *l, ElemType e)    
  150. {    
  151.     int i=0;    
  152.     while(l->length>i&&l->data[i]!=e)    
  153.    {    
  154.         i++;    
  155.    }    
  156.    if(i>=l->length)    
  157.   {    
  158.         return 0;    
  159.     }    
  160.    return i+1;    

  1. }   
  2. 运行结果:



     插入元素ListInsert(&L,i, &e):在线性表L中的第i个位置插入元素e;

      删除元素ListDelete(&L,i, &e):在线性表L中删除第i个元素,有e返回删除的值;

    学习心得:

         1.链表的的删除机制是先通过另一条通道与数据建立联系,再把原先的联系通道删掉,最后把要删除的数据free

          2.插入元素也是通过另一条通道插入

      3.通过画图理解

阅读全文
0 0
原创粉丝点击