C++数据结构学习之顺序表

时间:2021-10-13 12:25:09

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

C++数据结构学习之顺序表

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

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

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

 #ifndef _C_H_
#define _C_H_
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
using namespace std;
#endif // _C_H_

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

 #ifndef _SEQUENCETABLE_H_
#define _SEQUENCETABLE_H_
template<typename T>class STL{
private:
T *elem; //save the base address of STL
int length; //save the current length of STL;
int listsize; //save the opacity of STL
public:
//a function to create k length STL, if k doesn't exist, it can use default function to create 1 length STL
STL(int k=){
elem = new T[k];
length = ;
listsize = k;
}
//destruct STL
~STL(){
delete[]elem;
}
int getLength();
int getListsize();
void Insert(int k, int data); //a function to insert elem in the specific location
void Delete(int k, int data); //a function to delete elem in the specific location
int getElem(int k); //get elem in the specific location
int getLocation(int data);
void ListEmpty();
void showSTL();
}; template<typename T>
int STL<T>::getListsize(){
return listsize;
} template<typename T>
int STL<T>::getLength(){
return length;
} template<typename T>
void STL<T>::Insert(int k, int data){
//confirm whether the k is reasonable
if(k <= || k > (length+)){
cout<<"k is unreasonable!"<<endl;
}
int t; //an empty bottle
//insert data while satisfy this situation
while(length<listsize && k<=length){
if(k<length){
t = elem[k];
elem[k] = data;
data = elem[k+];
k++;
}
else{
length++;
t = elem[k];
elem[k] = data;
data = elem[k+];
k++;
}
}
if(k==(length+)){
if(length<listsize){
length++;
elem[k] = data;
}
else{
listsize++;
length++;
elem[k] = data;
}
}
} template<typename T>
void STL<T>::Delete(int k, int data){
//confirm whether the k is reasonable
if(k <= || k > (length+)){
cout<<"k is unreasonable!"<<endl;
}
//insert data while satisfy this situation
if(elem[k]==data){
while(k<=length){
if(k<length){
elem[k] = elem[k+];
k++;
}
else{
k++;
}
}
length--;
}
else{
cout<<"delete error!"<<endl;
}
} template<typename T>
int STL<T>::getLocation(int data){
int i = ; //consider when the length is 0 but the listsize is 1
while(i<=length){
if(elem[i] == data){
return i;
}
i++;
}
} template<typename T>
int STL<T>::getElem(int k){
if(k<= || k>=(length+)){
cout<<"k is unreasonable!"<<endl;
}
else{
return elem[k];
} } template<typename T>
void STL<T>::ListEmpty(){
length = ;
} template<typename T>
void STL<T>::showSTL(){
for(int i=;i<=length; i++){
cout<<elem[i]<<endl;
}
}
#endif

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

 #include "C.h"
#include "SequenceTable.h"
typedef int T;
int main(){
STL<T> L;
for(int i=; i<;i++){
L.Insert(i+, i+);
}
cout<<L.getLocation()<<endl;
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
L.showSTL();
int a = L.getElem();
L.Delete(,);
L.showSTL();
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
L.Delete(,);
L.showSTL();
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
}

运行实现:

C++数据结构学习之顺序表