第三周项目2

来源:互联网 发布:四大名著 知乎 编辑:程序博客网 时间:2024/06/05 20:08
  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="http://img.blog.csdn.net/20170914110057494" alt=""><br>  
  120. <p></p> 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小比熊鼻子不黑怎么办 狗狗鼻子起皮怎么办 金鱼身子弯了是怎么办 属狗的纹龙怎么办 卫生间的墙空的怎么办 花生苗长得好怎么办 菊花上面的白虫怎么办 小狗不吃东西还吐怎么办 小狗呕吐不吃东西没精神怎么办 小狗生病了不吃东西怎么办 小兔子腿摔了怎么办 刺猬葡萄我们骄傲我们该怎么办 小狗被邻居家大狗咬死了怎么办 狗狗死胎在腹中怎么办 小狗不吃饭没精神怎么办 虎皮鹦鹉生蛋了怎么办 钢笔替换芯干了怎么办 水芯钢笔不出水怎么办 被红斑蛇咬了怎么办 狗生完小狗不爰吃饭怎么办 比熊见了狗就叫怎么办 小狗到新主人家里吐怎么办 床上有小绿叶蝉怎么办 腰椎间盘轻微突出怎么办 养的小白兔死了怎么办 小鸡嘴边起很大的疙瘩怎么办 兔子下牙齿断了怎么办 刚种的花蔫了怎么办 鲜切花花朵蔫了怎么办 兔子扭伤脚肿了怎么办 兔子的耳朵肿了怎么办 家里养兔子大了怎么办 幼兔不吃兔粮怎么办 大兔子咬小兔子怎么办 买的小兔子拉稀怎么办 半个月的小兔子怎么办 母兔下崽没奶怎么办 母松鼠下崽后没有奶怎么办 母猫下崽后小猫没奶吃怎么办 母兔产后没奶水怎么办 兔子生崽了不管怎么办