数据结构与算法作业0:顺序表的基本操作

举报
nimo的小舔狗 发表于 2022/05/11 01:09:21 2022/05/11
【摘要】 有以下程序段,先改错,最后再编程实现所有函数的功能。 注:main()函数已给出,不得修改,提交时需要提交main函数。 #include<iostream.h>#include<stdlib.h>typedef int T class SeqList{private:T data;in...

有以下程序段,先改错,最后再编程实现所有函数的功能。

注:main()函数已给出,不得修改,提交时需要提交main函数。


  
  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. typedef int
  4. class SeqList
  5. {
  6. private:
  7. T data;
  8. int MaxSize; //顺序表最多可以存放的元素个数。
  9. int last; //顺序表最后一个元素的下标,初始值为-1。
  10. void SeqList(int sz);
  11. void Input();//首先输入元素的个数,然后顺次输入元素的值。
  12. void Output();//输出线性表的所有元素。
  13. void Insert(const T& x, int i );//在线性表中第i个位置插入值为x的元素。
  14. int Remove ( T & x );//从线性表中删除第一个值等于x的元素。
  15. }
  16. SeqList(int sz){data = new T[sz];MaxSize = sz; SeqList.last = -1; }
  17. int main() 
  18. {
  19. SeqList myList(100);
  20. myList.Input();
  21. myList.Output ();
  22. int i;
  23. for( i=0;i<5;i++)
  24.    myList.Insert(i+10,i);
  25. myList.Output ();
  26. for( i=10;i<15;i++)
  27.    myList.Remove(i);
  28. myList.Output (); 
  29. return 0;
  30. }

例如:

输入 Result
5
1
2
3
4
5
The elements are:
1
2
3
4
5
The elements are:
10
11
12
13
14
1
2
3
4
5
The elements are:
1
2
3
4
5
4
4
3
2
1
The elements are:
4
3
2
1
The elements are:
10
11
12
13
14
4
3
2
1
The elements are:
4
3
2
1

我的答案:


  
  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. typedef int T;
  5. class SeqList
  6. {
  7. public:
  8. T *data;
  9. int MaxSize;
  10. int last;
  11. SeqList(int sz);
  12. void Input();
  13. void Output();
  14. void Insert(const T & x,int i);
  15. int Remove(T & x);
  16. };
  17. SeqList::SeqList(int sz)
  18. {
  19. MaxSize = sz;last = -1;
  20. data = new T[sz];
  21. }
  22. void SeqList::Input()
  23. {
  24. int a;
  25. cin>>a;
  26. for(int i=0;i<a;i++)
  27. {
  28. cin>>data[i];
  29. last++;
  30. }
  31. }
  32. void SeqList::Output()
  33. {
  34. cout<<"The elements are:"<<endl;
  35. for(int i=0;i<=last;i++)
  36. {
  37. cout<<data[i]<<endl;
  38. }
  39. }
  40. void SeqList::Insert(const T & x,int i)
  41. {
  42. if(last == MaxSize-1)
  43. {
  44. cout<<"顺序表已无法插入"<<endl;
  45. }
  46. if(i<0||i>last+1)
  47. {
  48. cout<<"i越界"<<endl;
  49. }
  50. else
  51. {
  52. last++;
  53. for(int j=last;j>i;j--)
  54. data[j] = data[j-1];
  55. data[i] = x;
  56. }
  57. }
  58. int SeqList::Remove(T & x)
  59. {
  60. for(int i=0;i<=last;i++)
  61. {
  62. if(data[i]==x)
  63. {
  64. last--;
  65. for(int j=i;j<=last;j++)
  66. data[j] = data[j+1];
  67. return 1;
  68. }
  69. }
  70. return 0;
  71. }
  72. int main()
  73. {
  74. SeqList myList(100);
  75. myList.Input();
  76. myList.Output();
  77. int i;
  78. for(i=0;i<5;i++)
  79. myList.Insert(i+10,i);
  80. myList.Output();
  81. for(i=10;i<15;i++)
  82. myList.Remove(i);
  83. myList.Output();
  84. return 0;
  85. }

 

文章来源: blog.csdn.net,作者:渣渣ye,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yyfloveqcw/article/details/123757610

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。