数据结构相关-----C++实现线性表
使用顺序存储方式实现
C++代码如下
#include <iostream>
#include<string>
using namespace std;
class outOfRange :public exception //检查数据是否超出范围
{
public:
const char* what()throw()
{
return "ERROR! OUT OF RANGE .\n";
}
};
class badSize :public exception //检查数据的长度是否满足要求
{
public:
const char* what()throw()
{
return "ERROR! BAD SIZE .\n";
}
};
//抽象类:
template<class T>
class List
{
public:
virtual void clear_function() = 0; //清空线性表
virtual bool empty_function()const = 0;//判断是否为空
virtual int size_function()const = 0; //返回里面元素的个数
virtual void insert_function(int i, const T& value) = 0;
virtual void remove_function(int i) = 0;
virtual int search_function(const T & value)const = 0;
virtual T visit_function(int i)const = 0;
virtual void traverse_function()const = 0;
virtual void inverse_function() = 0;
virtual ~List() //析构函数一定要有申明和实现,否则无法实例化
{
;
}
};
template<class elemType>
class seqList :public List<elemType>
{
private:
elemType* data;
int curLength;
int maxSize;
void resieze_function();
public:
seqList(int initSize = 100); //ok
seqList(const seqList & s1); //ok
~seqList() //ok
{
curLength = 0;
maxSize = 0;
delete[]data;
}
void clear_function() //只用将curlength清为0就可以了 ok!!!!!!!!!!!!!!!!!!!!!!!!
{
curLength = 0;
}
bool empty_function()const //ok
{
return curLength == 0;
}
int size_function()const //ok
{
return curLength;
}
void insert_function(int i, const elemType& value); //ok
void remove_function(int i); //ok
int search_function(const elemType& value)const; //ok
elemType visit_function(int i)const; //ok
void traverse_function()const; //ok
void inverse_function(); //ok
bool union_function(const seqList & s2);
};
template<class elemType>
seqList<elemType>::seqList(int initSize)
{
if (initSize < 0)
{
throw badSize();
}
curLength = 0;
maxSize = initSize;
data = new elemType[maxSize];
}
template<class elemType>
seqList<elemType>::seqList(const seqList & s1)
{
maxSize = s1.maxSize;
curLength = s1.curLength;
data = new elemType[maxSize];
for (int i=0;i < curLength;i++)
{
data[i] = s1.data[i];
}
}
template<class elemType>
void seqList<elemType>::traverse_function()const
{
if (empty_function())
{
cout << "this list is empty" << endl;
}
else
{
for (int i = 0;i < curLength;i++)
{
cout << data[i] << " ";
}
cout << endl;
}
}
template<class elemType>
int seqList<elemType>::search_function(const elemType& value)const
{
for (int i = 0;i < curLength;i++)
{
if (value == data[i])
{
return i;
}
}
return -1;
}
template<class elemType>
void seqList<elemType>::insert_function(int i, const elemType& value)
{
if (i<0 || i>curLength)
{
throw outOfRange();
}
if (curLength == maxSize)
{
resieze_function();
}
for (int j = curLength;j > i;j--)
{
data[j] = data[j - 1];
}
data[i] = value;
curLength++;
}
template<class elemType>
elemType seqList<elemType>::visit_function(int i)const
{
if (i<0 || i>curLength-1)
{
throw outOfRange();
}
else
{
return data[i];
}
}
template<class elemType>
void seqList<elemType>::remove_function(int i)
{
if (i<0 || i>curLength - 1)
{
throw outOfRange();
}
for (int j = i;j < curLength;j++)
{
data[j] = data[j + 1];
}
curLength--;
}
template<class elemType>
void seqList<elemType>::inverse_function()
{
elemType temp;
for (int i = 0;i < curLength / 2;i++)
{
temp = data[i];
data[i] = data[curLength - i - 1];
data[curLength - i - 1] = temp;
}
}
template<class elemType>
void seqList<elemType>::resieze_function()
{
elemType* temp = data;
maxSize = maxSize * 2;
data = new elemType[maxSize]; //开辟一个新的范围进行扩展*2
for (int i = 0;i < curLength;i++)
{
data[i] = temp[i];
}
delete [] temp;
}
template<class elemType>
bool seqList<elemType>::union_function(const seqList & s2)
{
int m, n, k;
m = this->curLength;
n = s2.curLength;
k = m + n - 1;
while ((m + n) > maxSize)
{
resieze_function();
}
for (int j =0;j < n;j++)
{
data[m+j] = s2.data[j];
}
curLength = m + n;
return true;
}
int main()
{
cout << "hello world" << endl;
seqList<int> seq_1;
seq_1.insert_function(0, 12);
seqList<int> seq_2;
seq_2.insert_function(0, 12);
seq_2.insert_function(1, 13);
seq_1.union_function(seq_2);
seq_1.traverse_function();
cout << seq_1.empty_function() << endl;
try
{
cout << seq_1.visit_function(0) << endl;
}
catch (outOfRange & error_1)
{
cout << error_1.what() << endl;
}
system("pause");
}
大家相互学习,共同进步。