当前位置:   article > 正文

数据结构 | 顺序表的基本实现_数据结构顺序表基本算法的实现

数据结构顺序表基本算法的实现

学习教材:《数据结构——从概念到C++实现》

分类专栏:数据结构与算法(C++)

文章目录

前言

正文

1 顺序表概述

1.1 线性表的逻辑结构

1.2 线性表的顺序存储结构

2 顺序表实现


前言

        最近刚在学习《数据结构与算法》,单纯地跟课、看书觉得并不能很好的掌握好这些知识点,所以决定尝试将学习的知识点归纳总结,也将其进行代码实现,强化对知识点的理解。即作为笔记、实践,又作为我的思路分享。因为初学,难免有不正确、不恰当之处,敬请指正!

正文

1 顺序表概述

        线性表(linear list)简称表,是 n( n≥0)个数据元素的有限序列,跟据存储结构的不同可以分为顺序表链表

        线性表的顺序存储结构顺序表(sequential list),其基本思路使用一段地址连续的存储单元依次存储线性表的数据元素,具有随机存取(random access)结构。

2 顺序表实现

        先定义出顺序表类,成员变量实现顺序表存储结构,成员函数实现线性表的基本操作。由于线性表的数据元素类型不确定,采用C++的模板机制,定义出模板类SeqList(本文类模板中的成员函数均使用类内声明、类外定义)。

  1. // Sequential List
  2. const int MaxSize = 100; // 规定顺序表最多可以接受多少元素,可以跟据实际情况修改
  3. template<class DataType> // 定义模板类SeqList
  4. class SeqList
  5. {
  6. public:
  7. // 建立空的顺序表
  8. SeqList();
  9. // 建立长度为length的顺序表
  10. SeqList(DataType arr[],int length);
  11. // 析构函数
  12. ~SeqList() {};
  13. // 按位查找元素
  14. DataType Get(int i);
  15. // 按值查找元素,返回元素序号
  16. int Locate(DataType x);
  17. // 插入操作,在第i个位置插入值为x的元素
  18. void Insert(int i, DataType x);
  19. // 删除操作,删除第i个元素
  20. DataType Delect(int i);
  21. // 判断是否为空表
  22. int Empty();
  23. // 遍历操作,按序号依次输出各元素
  24. void PrintList();
  25. // 返回线性表长度
  26. int Length();
  27. private:
  28. DataType m_Arr[MaxSize];
  29. int m_length;
  30. };

2.1 建立空的顺序表

        在顺序表类中,成员函数m_length用于记录顺序表的长度,因此在建立空的顺序表时,只需要使用无参构造函数,将实例化对象的m_length置为0即可。

  1. // 建立空的顺序表
  2. template<class DataType>
  3. SeqList<DataType>::SeqList()
  4. {
  5. this->m_length = 0;
  6. }

2.2 建立指定长度的顺序表

        建立指定长度的顺序表,需要给定的数据元素传入到顺序表中,并将传入元素个数作为顺序表的长度。将给定的数据元素存放在数组arr[ ]中,并将给定元素个数一并传入。通过使用有参构造函数,传入数组arr[ ]以及顺序表长度。

        同时,为避免传入的给定数据元素个数超出前面设定的MaxSize导致程序异常,使用throw关键字来显式地抛出异常。

  1. // 建立长度为length的顺序表
  2. template<class DataType>
  3. SeqList<DataType>::SeqList(DataType arr[], int length)
  4. {
  5. if (length > MaxSize) throw"参数非法";
  6. for (int i = 0; i < length; i++)
  7. {
  8. this->m_Arr[i] = arr[i];
  9. }
  10. this->m_length = length;
  11. }

2.3 析构函数

        顺序表时静态存储分配,在顺序表变量退出作用域时,自动释放改变了所占内存单元,故无须销毁,析构函数为空即可。

2.4 按位查找元素

        顺序表中第 i 个元素在数组中下标为 i-1 的位置,因此使用顺序表人员实现按位查找。

  1. // 按位查找元素
  2. template<class DataType>
  3. DataType SeqList<DataType>::Get(int i)
  4. {
  5. if (i > this->m_length || i < 1) throw"参数非法";
  6. else return this->m_Arr[i - 1];
  7. }

2.5 按值查找元素

        在顺序表中按值操作,需要对顺序表中的元素依次进行比较,如果查到到相应的值,则将元素的序号(不是数组下标)返回;否则返回0,即查找失败。

  1. // 按值查找元素,返回元素序号
  2. template<class DataType>
  3. int SeqList<DataType>::Locate(DataType x)
  4. {
  5. for (int i = 0; i < this->m_length; i++)
  6. {
  7. if (this->m_Arr[i] == x)
  8. return (i + 1);
  9. }
  10. return 0;
  11. }

2.6 插入操作

        插入操作时,在表的第 i 个位置插入值为 x 的元素,将原有的第 i-1 个位置上的元素依次往后移动,使长度为 n 的顺序表的长度变为 n+1。

        且为避免输入的 i 不符合要求(即不满足 1 ≤ i ≤ m_length),使用throw关键字来显式地抛出异常。

  1. // 插入操作,在第i个位置插入值为x的元素
  2. template<class DataType>
  3. void SeqList<DataType>::Insert(int i, DataType x)
  4. {
  5. if (this->m_length == MaxSize) throw"上溢";
  6. if (i<1 || i>this->m_length) throw"插入位置错误";
  7. for (int j = this->m_length; j >= i; j--)
  8. {
  9. this->m_Arr[j] = this->m_Arr[j - 1];
  10. }
  11. this->m_Arr[i - 1] = x;
  12. this->m_length++;
  13. }

2.7 删除操作

        删除操作时将表的第 i 个元素删除,使长度为 n 的顺序表变成长度为 n-1 的顺序表,且返回删除的元素的值。操作逻辑上,即将第i个元素删除后,后面的元素依次提到前面一个空出的位置上;顺序表长度也做了减1处理。

        且为避免输入的 i 不符合要求(即不满足 1 ≤ i ≤ m_length),使用throw关键字来显式地抛出异常。

  1. template<class DataType>
  2. DataType SeqList<DataType>::Delect(int i)
  3. {
  4. DataType x;
  5. if (this->m_length == 0) throw"上溢";
  6. if (i<1 || i>this->m_length) throw"删除位置错误";
  7. x = this->m_Arr[i - 1];
  8. for (int j = i; j < this->m_length; j++)
  9. {
  10. this->m_Arr[j - 1] = this->m_Arr[j];
  11. }
  12. this->m_length--;
  13. return x;
  14. }

2.8 判断空表

        顺序表类成员变量 m_length 中存储着顺序表的长度,判断空表操作只需判断长度 m_length 为0即可。

  1. // 判断是否为空表
  2. template<class DataType>
  3. int SeqList<DataType>::Empty()
  4. {
  5. if (this->m_length != 0)
  6. return 1;
  7. else return 0;
  8. }

2.9 遍历操作

        在顺序表中,遍历操作即按下标一次输出各元素,因此使用循环语句实现。

  1. // 遍历操作,按序号依次输出各元素
  2. template<class DataType>
  3. void SeqList<DataType>::PrintList()
  4. {
  5. for (int i = 0; i < this->m_length; i++)
  6. {
  7. cout << this->m_Arr[i] << " " ;
  8. }
  9. cout << endl;
  10. }

2.10 返回顺序表长度

        如判断空表中所述,返回顺序表长度同样只需返回成员函数 m_length 的值即可。

  1. template<class DataType>
  2. int SeqList<DataType>::Length()
  3. {
  4. return this->m_length;
  5. }

3 全部代码

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5. // Sequential List
  6. // 规定顺序表最多可以接受多少元素
  7. const int MaxSize = 100;
  8. template<class DataType> // 定义模板类SeqList
  9. class SeqList
  10. {
  11. public:
  12. // 建立空的顺序表
  13. SeqList();
  14. // 建立长度为length的顺序表
  15. SeqList(DataType arr[],int length);
  16. // 析构函数
  17. ~SeqList() {};
  18. // 按位查找元素
  19. DataType Get(int i);
  20. // 按值查找元素,返回元素序号
  21. int Locate(DataType x);
  22. // 插入操作,在第i个位置插入值为x的元素
  23. void Insert(int i, DataType x);
  24. // 删除操作,删除第i个元素
  25. DataType Delect(int i);
  26. // 判断是否为空表
  27. int Empty();
  28. // 遍历操作,按序号依次输出各元素
  29. void PrintList();
  30. // 返回线性表长度
  31. int Length();
  32. private:
  33. DataType m_Arr[MaxSize];
  34. int m_length;
  35. };
  36. // 建立空的顺序表
  37. template<class DataType>
  38. SeqList<DataType>::SeqList()
  39. {
  40. this->m_length = 0;
  41. }
  42. // 建立长度为length的顺序表
  43. template<class DataType>
  44. SeqList<DataType>::SeqList(DataType arr[], int length)
  45. {
  46. if (length > MaxSize) throw"参数非法";
  47. for (int i = 0; i < length; i++)
  48. {
  49. this->m_Arr[i] = arr[i];
  50. }
  51. this->m_length = length;
  52. }
  53. // 按位查找元素
  54. template<class DataType>
  55. DataType SeqList<DataType>::Get(int i)
  56. {
  57. if (i > MaxSize || i < 1) throw"参数非法";
  58. else return this->m_Arr[i - 1];
  59. }
  60. // 按值查找元素,返回元素序号
  61. template<class DataType>
  62. int SeqList<DataType>::Locate(DataType x)
  63. {
  64. for (int i = 0; i < this->m_length; i++)
  65. {
  66. if (this->m_Arr[i] == x)
  67. return (i + 1);
  68. }
  69. return 0;
  70. }
  71. // 插入操作,在第i个位置插入值为x的元素
  72. template<class DataType>
  73. void SeqList<DataType>::Insert(int i, DataType x)
  74. {
  75. if (this->m_length == MaxSize) throw"上溢";
  76. if (i<1 || i>this->m_length) throw"插入位置错误";
  77. for (int j = this->m_length; j >= i; j--)
  78. {
  79. this->m_Arr[j] = this->m_Arr[j - 1];
  80. }
  81. this->m_Arr[i - 1] = x;
  82. this->m_length++;
  83. }
  84. // 删除操作,删除第i个元素
  85. template<class DataType>
  86. DataType SeqList<DataType>::Delect(int i)
  87. {
  88. DataType x;
  89. if (this->m_length == 0) throw"上溢";
  90. if (i<1 || i>this->m_length) throw"删除位置错误";
  91. x = this->m_Arr[i - 1];
  92. for (int j = i; j < this->m_length; j++)
  93. {
  94. this->m_Arr[j - 1] = this->m_Arr[j];
  95. }
  96. this->m_length--;
  97. return x;
  98. }
  99. // 判断是否为空表
  100. template<class DataType>
  101. int SeqList<DataType>::Empty()
  102. {
  103. if (this->m_length != 0)
  104. return 1;
  105. else return 0;
  106. }
  107. // 遍历操作,按序号依次输出各元素
  108. template<class DataType>
  109. void SeqList<DataType>::PrintList()
  110. {
  111. for (int i = 0; i < this->m_length; i++)
  112. {
  113. cout << this->m_Arr[i] << " " ;
  114. }
  115. cout << endl;
  116. }
  117. template<class DataType>
  118. int SeqList<DataType>::Length()
  119. {
  120. return this->m_length;
  121. }
  122. // 测试代码
  123. /*void test01()
  124. {
  125. int r[5] = { 1,2,3,4,5 };
  126. SeqList<int> s(r, 5);
  127. cout << "当前线性表的数据为: ";
  128. s.PrintList();
  129. cout << "插入数据中......" << endl;
  130. s.Insert(3, 7);
  131. cout << "当前线性表的长度为:" << s.Length() << endl;
  132. cout << "当前线性表的数据为: ";
  133. s.PrintList();
  134. int num = 0;
  135. cout << "请输入查找的数据:" << endl;
  136. cin >> num;
  137. int i = s.Locate(num);
  138. if (i == 0)cout << "查找失败!!" << endl;
  139. else cout << "元素:" << num << "的位置为:" << i << endl;
  140. cout << "请输入查找元素的序号:" << endl;
  141. cin >> num;
  142. cout << "第" << num << "个元素为:" << s.Get(num) << endl;
  143. cout << "请输入要删除的元素序号:" << endl;
  144. cin >> num;
  145. i = s.Delect(num);
  146. cout << "删除的元素为:" << i << ",删除后线性表的数据为:" << endl;
  147. s.PrintList();
  148. }*/
  149. int main()
  150. {
  151. // test01(); // 测试代码
  152. return 0;
  153. }

初学数据结构与算法,若有不正确之处,敬请指正啦~

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号