顺序表是数据结构中最基本也是应用相当广泛的一种数据结构类型。它通常包含三个私有成分,即指向数据数组的头指针、当前表长以及表的实际容量。表的头指针通常指向数据数组的基地址,通过数组的形式进行访问数据数组中的每个元素。其基本结构类型如下图所示:

从上图可以看到,其基本结构包含一个指向数据数组的头指针,当前表长为5,但是该顺序表实际表的容量有7。

下面附上实现该数据结构的各项操作代码

在C.h文件中存放可能要用到的C++库:

 1 #ifndef _C_H_
 2 #define _C_H_
 3 #include<iostream>
 4 #include<fstream>
 5 #include<iomanip>
 6 #include<cmath>
 7 #include<vector>
 8 #include<list>
 9 #include<stack>
10 #include<queue>
11 #include<deque>
12 #include<string>
13 #include<bitset>
14 #include<algorithm>
15 #include<ctime>
16 #include<cstdarg>
17 #include<assert.h>
18 using namespace std;
19 #endif // _C_H_

在SequenceTable.h存放对应的数据结构类型:

  1 #ifndef _SEQUENCETABLE_H_
  2 #define _SEQUENCETABLE_H_
  3 template<typename T>class STL{
  4 private:
  5     T *elem;   //save the base address of STL
  6     int length;     //save the current length of STL;
  7     int listsize;   //save the opacity of STL
  8 public:
  9     //a function to create k length STL, if k doesn't exist, it can use default function to create 1 length STL
 10     STL(int k=1){
 11         elem = new T[k];
 12         length = 0;
 13         listsize = k;
 14     }
 15     //destruct STL
 16     ~STL(){
 17         delete[]elem;
 18     }
 19     int getLength();
 20     int getListsize();
 21     void Insert(int k, int data);   //a function to insert elem in the specific location
 22     void Delete(int k, int data);   //a function to delete elem in the specific location
 23     int getElem(int k);     //get elem in the specific location
 24     int getLocation(int data);
 25     void ListEmpty();
 26 };
 27 
 28 template<typename T>
 29 int STL<T>::getListsize(){
 30     return listsize;
 31 }
 32 
 33 template<typename T>
 34 int STL<T>::getLength(){
 35     return length;
 36 }
 37 
 38 template<typename T>
 39 void STL<T>::Insert(int k, int data){
 40     //confirm whether the k is reasonable
 41     if(k <= 0 || k > (length+1)){
 42         cout<<"k is unreasonable!"<<endl;
 43     }
 44     int t;     //an empty bottle
 45     //insert data while satisfy this situation
 46     while(length<listsize && k<=length){
 47         if(k<length){
 48             t = elem[k];
 49             elem[k] = data;
 50             data = elem[k+1];
 51         }
 52         else{
 53             length++;
 54             t = elem[k];
 55             elem[k] = data;
 56             data = elem[k+1];
 57         }
 58     }
 59     while(k==(length+1)){
 60         if(length<listsize){
 61             length++;
 62             t = elem[k];
 63             elem[k] = data;
 64             data = elem[k+1];
 65         }
 66         else{
 67             listsize++;
 68             length++;
 69             t = elem[k];
 70             elem[k] = data;
 71             data = elem[k+1];
 72         }
 73     }
 74 }
 75 
 76 template<typename T>
 77 void STL<T>::Delete(int k, int data){
 78     //confirm whether the k is reasonable
 79     if(k <= 0 || k > (length+1)){
 80         cout<<"k is unreasonable!"<<endl;
 81     }
 82     int t;     //an empty bottle
 83     //insert data while satisfy this situation
 84     while(k<=length){
 85         if(k<length){
 86             t = elem[k];
 87             elem[k] = elem[k+1];
 88         }
 89         else{
 90             length--;
 91         }
 92     }
 93 }
 94 
 95 template<typename T>
 96 int STL<T>::getLocation(int data){
 97     int i = 0; //consider when the length is 0 but the listsize is 1
 98     while(i<=length){
 99         if(elem[i] == data){
100             return i;
101         }
102         i++;
103     }
104 }
105 
106 template<typename T>
107 int STL<T>::getElem(int k){
108     if(k<=0 || k>=(length+1)){
109         cout<<"k is unreasonable!"<<endl;
110     }
111     else{
112         return elem[k];
113     }
114 
115 }
116 #endif

然后再main.cpp文件中实现对该数据结构的调用:

 1 #include "C.h"
 2 #include "SequenceTable.h"
 3 typedef int T;
 4 int main(){
 5     STL<T> L;
 6     L.Insert(1,5);
 7     cout<<L.getLocation(5)<<endl;
 8     cout<<L.getLength()<<endl;
 9     cout<<L.getListsize()<<endl;
10     int a = L.getElem(0);
11     L.Delete(1,5);
12     cout<<L.getLength()<<endl;
13     cout<<L.getListsize()<<endl;
14 }

运行实现:

 

版权声明:本文为sgatbl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sgatbl/p/8456677.html