第三周项目2

本文介绍了一个简单的线性表(顺序表)的创建及基本操作,包括初始化、销毁、判断空表、获取表长、显示表内容等,并演示了如何通过C语言实现这些功能。
  1. /* 
  2. *Copyright (c) 2017,烟台大学计算机与控制工程学院 
  3. *All rights reserved. 
  4. *文件名称: 
  5. *作    者:常璐 
  6. *完成日期:2017年9月20日 
  7. *版 本 号:v1.0 
  8. * 
  9. *问题描述:建立自己的算法库,一个源文件包涵main函数,一个123.h头文件,一个123.cpp实现各种算法的定义 
  10. *输入描述:六个数据元素 
  11. *程序输出:顺序表的存储内容 
  12. */  
  13. //主函数  
  14. #include "123.h"  
  15. int main()  
  16. {  
  17.     SqList *sq;  
  18.     ElemType x[6]= {1,2,3,2,8,7};  
  19.     CreateList(sq, x, 6);  
  20.     DispList(sq);  
  21.     return 0;  
  22. }  
[cpp]  view plain  copy
  1. <pre code_snippet_id="2577424" snippet_file_name="blog_20170914_2_9979714" name="code" class="cpp">//算法实现  
  2. #include <stdio.h>  
  3. #include <malloc.h>  
  4. #include "123.h"  
  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.   
  16. //初始化线性表InitList(L)  
  17. void InitList(SqList *&L)   //引用型指针  
  18. {  
  19.     L=(SqList *)malloc(sizeof(SqList));  
  20.     //分配存放线性表的空间  
  21.     L->length=0;  
  22. }  
  23.   
  24. //销毁线性表DestroyList(L)  
  25. void DestroyList(SqList *&L)  
  26. {  
  27.     free(L);  
  28. }  
  29.   
  30. //判定是否为空表ListEmpty(L)  
  31. bool ListEmpty(SqList *L)  
  32. {  
  33.     return(L->length==0);  
  34. }  
  35.   
  36. //求线性表的长度ListLength(L)  
  37. int ListLength(SqList *L)  
  38. {  
  39.     return(L->length);  
  40. }  
  41.   
  42. //输出线性表DispList(L)  
  43. void DispList(SqList *L)  
  44. {  
  45.     int i;  
  46.     if (ListEmpty(L)) return;  
  47.     for (i=0; i<L->length; i++)  
  48.         printf("%d ",L->data[i]);  
  49.     printf("\n");  
  50. }  
  51.   
  52. //求某个数据元素值GetElem(L,i,e)  
  53. bool GetElem(SqList *L,int i,ElemType &e)  
  54. {  
  55.     if (i<1 || i>L->length)  return false;  
  56.     e=L->data[i-1];  
  57.     return true;  
  58. }  
  59.   
  60. //按元素值查找LocateElem(L,e)  
  61. int LocateElem(SqList *L, ElemType e)  
  62. {  
  63.     int i=0;  
  64.     while (i<L->length && L->data[i]!=e) i++;  
  65.     if (i>=L->length)  return 0;  
  66.     else  return i+1;  
  67. }  
  68.   
  69. //插入数据元素ListInsert(L,i,e)  
  70. bool ListInsert(SqList *&L,int i,ElemType e)  
  71. {  
  72.     int j;  
  73.     if (i<1 || i>L->length+1)  
  74.         return false;   //参数错误时返回false  
  75.     i--;            //将顺序表逻辑序号转化为物理序号  
  76.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
  77.         L->data[j]=L->data[j-1];  
  78.     L->data[i]=e;           //插入元素e  
  79.     L->length++;            //顺序表长度增1  
  80.     return true;            //成功插入返回true  
  81. }  
  82.   
  83. //删除数据元素ListDelete(L,i,e)  
  84. bool ListDelete(SqList *&L,int i,ElemType &e)  
  85. {  
  86.     int j;  
  87.     if (i<1 || i>L->length)  //参数错误时返回false  
  88.         return false;  
  89.     i--;        //将顺序表逻辑序号转化为物理序号  
  90.     e=L->data[i];  
  91.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
  92.         L->data[j]=L->data[j+1];  
  93.     L->length--;              //顺序表长度减1  
  94.     return true;              //成功删除返回true  
  95. }  
  96. </pre><pre code_snippet_id="2577424" snippet_file_name="blog_20170914_4_2455733" name="code" class="cpp"></pre><pre code_snippet_id="2577424" snippet_file_name="blog_20170914_4_2455733" name="code" class="cpp"></pre>  
  97. <pre></pre>  
  98. <p><br>  
  99. </p>  
  100. <p></p><pre code_snippet_id="2577424" snippet_file_name="blog_20170914_5_8137334" name="code" class="cpp">//各种定义  
  101. #define MaxSize 50  
  102. typedef int ElemType;  
  103. typedef struct  
  104. {  
  105.     ElemType data[MaxSize];  
  106.     int length;  
  107. } SqList;  
  108. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  109. void InitList(SqList *&L);//初始化线性表InitList(L)  
  110. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
  111. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  112. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
  113. void DispList(SqList *L);//输出线性表DispList(L)  
  114. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
  115. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  116. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  117. bool ListDelete(SqList *&L,int i,ElemType &e);//删除  
  118. </pre><br>  
  119. <img src="https://img-blog.youkuaiyun.com/20170914110057494" alt=""><br>  
  120. <p></p> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值