当前位置:   article > 正文

数据结构(C语言版)——线性表顺序存储的应用_假设线性表采取顺序存储结构,写出以下算法并用c语言实现。 (1)定义线性表的数据结

假设线性表采取顺序存储结构,写出以下算法并用c语言实现。 (1)定义线性表的数据结

学习目录:

1.初始化

2.取值

3.查找

4.插入

5.删除

6.完整代码的描述

7.效果截图


学习内容:

1.初始化

(1)定义:构造一个空的顺序表

(2)算法步骤:

  • 为顺序表L动态分配一个预定义大小的数组空间,使elem指向这段空间的基地址
  • 将表的当前长度设置为0

(3)算法描述:

  1. Status InitList(SqList &L)
  2. {
  3. L.elem=new ElemType[MAXSIZE];
  4. if(!L.elem) exit(OVERFLOW);
  5. L.length=0;
  6. return OK;
  7. }

2.取值

(1)定义:根据指定的位置序号i,获取顺序表中第i个数据元素的值。由于顺序存储结构具有随机存取的特点,可以直接通过数组下标定位得到,elem[i-1]单元存储第i个数据元素。

(2)算法步骤:

  • 判断指定的位置序号i值是否合理(1<=i<=L.length),若不合理,则返回ERROR。
  • 若i值合理,则将第i个数据元素L.elem[i-1]赋给参数e,通过e返回第i个数据元素的传值。

(3)算法描述:

  1. Status GetElem(SqList L,int i,ElemType &e)
  2. {
  3. if(i<1||i>L.length) return ERROR;
  4. e=L.elem[i-1];
  5. return OK;
  6. }

3.查找

(1)定义:根据指定的元素值e,查找顺序表中第1个值与e相等的元素。若查找成功,则返回该元素在表中的位置序号;若查找失败,则返回0。

(2)算法步骤:

  • 从第一个元素起,依次将其值和e相比较,若找到值与e相等的元素L.elem[i],则查找成功,返回该元素的序号i+1
  • 若查遍整个顺序表都没有找到,则查找失败,返回0

 (3)算法描述:

  1. Status LocateElem(SqList L,ElemType e)
  2. {
  3. int i;
  4. for(i=0;i<L.length;i++)
  5. if(L.elem[i]==e) return i+1;
  6. return 0;
  7. }

4.插入

(1)定义:在表的第i个位置插入一个新的数据元素e,使长度为n的线性表变成长度为n+1的线性表。

(2)算法步骤:

  • 判断插入位置是否合法(1<=i<=n+1),若不合法则返回ERROR。
  • 判断顺序表的存储空间是否已满,若满则返回ERROR。
  • 将第n个至第i个位置的元素依次向后移动一个位置,空出第i个位置(i=n+1时无需移动)。
  • 将要插入的新元素e放入第i个位置
  • 表长加1

(3)算法描述:

  1. Status ListInsert(SqList &L,int i,ElemType e)
  2. {
  3. int j;
  4. if(i<1||i>L.length+1) return ERROR;
  5. if(L.length==MAXSIZE) return ERROR;
  6. for(j=L.length-1;j>=i-1;j--)
  7. L.elem[j+1]=L.elem[j];
  8. L.elem[i-1]=e;
  9. ++L.length;
  10. return OK;
  11. }

5.删除

(1)定义:将表的第i个元素删去,将长度为n的线性表变成长度为n-1的线性表。

(2)算法步骤:

  • 判断删除位置i是否合法(合法值为1<=i<=n),若不合法则返回ERROR。
  • 将第i+1个至第n个元素依次向前移动一个位置(i=n无需移动)。
  • 表长减1。

(3)算法描述:

  1. Status ListDelete(SqList &L,int i)
  2. {
  3. int j;
  4. if(i<1||i>L.length+1) return ERROR;
  5. for(j=i;j<=L.length;j++)
  6. L.elem[j-1]=L.elem[j];
  7. --L.length;
  8. return OK;
  9. }

6.完整代码的描述

  1. #include <iostream>
  2. using namespace std;
  3. #define MAXSIZE 10
  4. #define OK 1
  5. #define ERROR 0
  6. #define OVERFLOW -2
  7. typedef int Status;
  8. typedef char ElemType;
  9. //顺序表类型SqList的定义
  10. typedef struct
  11. {
  12. ElemType *elem;
  13. int length;
  14. }SqList;
  15. //初始化
  16. Status InitList(SqList &L)
  17. {
  18. L.elem=new ElemType[MAXSIZE];
  19. if(!L.elem) exit(OVERFLOW);
  20. L.length=0;
  21. return OK;
  22. }
  23. //销毁
  24. void DestroyList(SqList &L)
  25. {
  26. if(L.elem) delete L.elem;
  27. }
  28. //判断表是否为空
  29. Status ListEmpty(SqList L)
  30. {
  31. if(L.length==0) return 1;
  32. else return 0;
  33. }
  34. //求表长
  35. Status ListLength(SqList L)
  36. {
  37. if(L.length) return L.length;
  38. else return ERROR;
  39. }
  40. //输出顺序表内容
  41. Status TraverseList(SqList L)
  42. {
  43. cout<<"输出顺序表L:";
  44. int i;
  45. for(i=0;i<L.length;i++)
  46. {
  47. cout<<L.elem[i];
  48. }
  49. }
  50. //按位置取值
  51. Status GetElem(SqList L,int i,ElemType &e)
  52. {
  53. if(i<1||i>L.length) return ERROR;
  54. e=L.elem[i-1];
  55. return OK;
  56. }
  57. //按值查找
  58. Status LocateElem(SqList L,ElemType e)
  59. {
  60. int i;
  61. for(i=0;i<L.length;i++)
  62. if(L.elem[i]==e) return i+1;
  63. return 0;
  64. }
  65. //插入数据元素
  66. Status ListInsert(SqList &L,int i,ElemType e)
  67. {
  68. int j;
  69. if(i<1||i>L.length+1) return ERROR;
  70. if(L.length==MAXSIZE) return ERROR;
  71. for(j=L.length-1;j>=i-1;j--)
  72. L.elem[j+1]=L.elem[j];
  73. L.elem[i-1]=e;
  74. ++L.length;
  75. return OK;
  76. }
  77. //删除数据元素
  78. Status ListDelete(SqList &L,int i)
  79. {
  80. int j;
  81. if(i<1||i>L.length+1) return ERROR;
  82. for(j=i;j<=L.length;j++)
  83. L.elem[j-1]=L.elem[j];
  84. --L.length;
  85. return OK;
  86. }
  87. void menu()
  88. {
  89. cout<<"-------顺序表的定义和基本操作------\n";
  90. cout<<"- 1初始化顺序表 \n";
  91. cout<<"- 2销毁顺序表 \n";
  92. cout<<"- 3判断表是否为空 \n";
  93. cout<<"- 4求表长 \n";
  94. cout<<"- 5输出顺序表的内容 \n";
  95. cout<<"- 6按位置取值 \n";
  96. cout<<"- 7按值查找 \n";
  97. cout<<"- 8插入数据元素 \n";
  98. cout<<"- 9删除数据元素 \n";
  99. cout<<"- 0退出 \n";
  100. cout<<"请输入菜单对应的数字:";
  101. }
  102. int main()
  103. {
  104. SqList L;
  105. int i,k;
  106. ElemType c;
  107. for(;;)
  108. {
  109. menu();
  110. cin>>k;
  111. switch(k)
  112. {
  113. case 1:InitList(L);
  114. cout<<"初始化顺序表L:L.elem="<<&L.elem<<" , L.length="<<L.length<<"\n";
  115. system("pause");//系统函数,暂停界面,显示结果,按任意键继续
  116. system("cls");//系统函数,清除屏幕上的运行结果和上一次输出的菜单
  117. break;
  118. case 2:DestroyList(L);
  119. cout<<"释放顺序表L:L.elem="<<&L.elem<<" , L.length="<<L.length<<"\n";
  120. system("pause");
  121. system("cls");
  122. break;
  123. case 3:cout<<"判断顺序表L当前是否为空:";
  124. if(ListEmpty(L)) cout<<"L为空\n";
  125. else cout<<"L不为空\n";
  126. system("pause");
  127. system("cls");
  128. break;
  129. case 4:cout<<"顺序表L的当前长度为:"<<ListLength(L)<<"\n";
  130. system("pause");
  131. system("cls");
  132. break;
  133. case 5:TraverseList(L);
  134. cout<<"\n";
  135. system("pause");
  136. system("cls");
  137. break;
  138. case 6:cout<<"请输入查找位置1~"<<ListLength(L)<<";";
  139. cin>>i;
  140. if(GetElem(L,i,c))
  141. cout<<"位置上的数据是"<<c<<"\n" ;
  142. else
  143. cout<<"没有找到,请合适查找位置是否正确。\n";
  144. system("pause");
  145. system("cls");
  146. break;
  147. case 7:cout<<"请输入要查找的数据:";
  148. cin>>c;
  149. if(i=LocateElem(L,c))
  150. cout<<c<<"是顺序表中的第"<<i<<"个数据\n";
  151. else
  152. cout<<"不存在此顺序表中\n";
  153. system("pause");
  154. system("cls");
  155. break;
  156. case 8:cout<<"请输入要插入的数据:";
  157. cin>>c;
  158. cout<<"请输入要插入的数据位置(1-"<<ListLength(L)+1<<"):";
  159. cin>>i;
  160. if(ListInsert(L,i,c))
  161. cout<<"插入成功!\n";
  162. else
  163. cout<<"插入失败,请核实插入位置\n";
  164. system("pause");
  165. system("cls");
  166. break;
  167. case 9: cout<<"请输入要删除的数据位置(1-"<<ListLength(L)<<"):";
  168. cin>>i;
  169. if(ListDelete(L,i))
  170. cout<<"删除成功!"<<"\n";
  171. else
  172. cout<<"删除失败,请核实删除位置\n";
  173. system("pause");
  174. system("cls");
  175. break;
  176. case 0:return 0;
  177. }
  178. }
  179. }

7.效果截图

 

 

 

 

 

 

 

 

 

 注:1.每次进入都需要进行初始化,才可以进行其他操作;

        2.在进行销毁操作之后是不能进行其他操作的,需要重新初始化;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/426987?site
推荐阅读
相关标签
  

闽ICP备14008679号