//顺序线性表的头文件
#include<iostream>
const int MaxSize = 100;
//定义顺序表SeqList的模板类
template<class DataType>
class SeqList {
public:
//顺序表无参构造器(创建一个空的顺序表)
SeqList() { length = 0 }
//顺序表有参构造器(创建一个长度为n的顺序表)
SeqList(DataType array[], int n);
//顺序表析构函数
~SeqList() {}
//求顺序表的长度
int GetLength() { return length; }
//顺序表按位查找,返回i位置的元素
DataType GetElement(int i);
//顺序表按值查找,返回该元素所在的位置
int GetLocal(DataType x);
//顺序表在指定的位置插入指定的元素
void Insert(int i, DataType x);
//顺序表删除元素,返回删除的元素
DataType Delete(int i);
//输出顺序表中的元素
void PrintSeqList();
private:
//一维数组,存放数据元素
DataType data[MaxSize];
//顺序表的长度
int length;
};
//实现单链表的头文件内容
template<class DataType>
struct Node
{
//数据域,存放该结点的数据
DataType data;
//指针域,指向下一个结点
Node<DataType> *next;
};
template<class DataType>
class LinkedList {
public:
//单链表无参构造器
LinkedList();
//单链表有参构造器
LinkedList(DataType array[], int n);
LinkedList(int n, DataType array[]);
//单链表析构函数
~LinkedList();
//获取单链表的长度
int GetLength();
//查找单链表指定元素的序号
int GetLocal(DataType x);
//获取单链表指序号的元素
DataType GetElement(int index);
//单链表中在指定位置插入指定的元素
void Insert(int index, DataType x);
//在单链表中删除指定位置的元素
DataType Delete(int index);
//按序号输出单链表中的元素
void PrintLinkedList();
private:
//声明单链表的头指针
Node<DataType> *first;
};
#include "SeqList.h";
#include<iostream>
using namespace std;
//实现顺序表有参构造器
template<class DataType>
SeqList<DataType>::SeqList(DataType array[], int n)
{
if (n > MaxSize)
{
throw "传入的顺序表长度过长";
}
//给顺序表的存储元素的数组赋值
for (int i = 0; i < n; i++)
{
data[i] = array[i];
}
//给顺序表的长度赋值
length = n;
}
//实现顺序表按位查找
template<class DataType>
DataType SeqList<DataType>::GetElement(int i)
{
//判断是定的位置是否合理
if (i < 1 || i >leng