---
title: "list c++template"
author: "xpp02"
date: "2013-02-02T14:08:00Z"
tags: []
slug: "list-c-template"
draft: false
lastmod: "2013-02-02T21:33:59Z"
id: 1784219
url: "https://xpp02.iteye.com/blog/1784219"
---

以一个现成的模板实现了线性表的顺序结构实现，VC6.0调试OK

请大家以开源的方式来完善这个算法 ，以跟贴方式来添加代码

请大家往这个下面继续添加完整的可以运行的线性表的顺序结构实现代码

/*

线性表的顺序结构实现，数组C++实现法，VC调试OK  
线性表可以用顺序结构（是用数组线性表来实现）来实现，也可以用链式结构来实现。  
我们以顺序结构为例：  
线性表的基本的操作：  
(1)创建线性表  
(2)初始化线性表SeqList()  
(3)插入元素Insert(Type& x ,int i)  
(4)删除元素Remove(Type& x)  
(5)查找元素  
按位置查找对象Get(int i)  
按对象查找位置Find(Type& x)  
(6)计算表长度Length();

扩展功能：  
顺序表满判断IsFull()  
顺序表空判断IsEmpty()  
输出顺序表的数据PrintList()  
输入顺序表的数据InputList()  
判断x是否在表中IsIn(Type& x)  
寻找x的后继Next(Type& x)  
寻找x的前驱Prior(Type& x)  
合并两个数组Union(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB)  
求两个数组的交集

  
注意：  
(1)插入元素时要把插入位置后的所有元素“从后往前”各向后移动一个位置。  
(2)删除元素时要把删除位置后的所有元素“从前往后”各向前移动一个位置。

\*/  
//SeqList.h  
#ifndef SEQLIST\_H  
#define SEQLIST\_H

template&lt; class Type&gt; class SeqList  
{  
public:  
SeqList(int MaxSize=6);//构造函数定义和实现  
~SeqList() {delete \[]data; }//析构函数定义和实现  
int Length() const {return last+1;}//计算表长度定义和实现  
Type Get(int i)　　　　　　//取第i个元素的值  
{if (i&lt;0||i&gt;last)  
cerr&lt;&lt;"out of range...";  
else  
return data\[i];  
}  
int Find(Type& x) const;//定位函数：找x在表中的位置  
int Insert(Type& x ,int i);//插入x在表中第i个位置处  
int Remove(Type& x);//删除x  
//新添加功能  
int Next(Type& x);//寻找x的后继  
int Prior(Type& x);//寻找x的前驱  
int IsEmpty() {return last==-1;}//判断顺序表是否为空，空则返回1；否则返回0  
int IsFull() {return last==MaxSize-1;} //判断顺序表满否，满则返回1；否则返回0  
int IsIn(Type& x);//判断x是否在表中  
void Union(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB);//合并LA,LB,重复元素只留一下  
void Intersection(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB);//求LA,LB中的共有元素  
void PrintList();//输出顺序表的数据  
void InputList();//输入顺序表的数据

private:  
Type* data;//存放顺序表的数组  
int MaxSize;//顺序表最大可容纳项数  
int last;//顺序表当前是已存表项的最后位置  
};  
//构造函数，通过描写参数sz定义数组的长度。  
template &lt;class Type&gt; SeqList&lt;Type&gt;::SeqList(int sz)  
{  
if(sz&gt;0)  
MaxSize=sz;  
else  
MaxSize=6;  
last=MaxSize - 1;  
data=new Type\[MaxSize];

}

//定位，找x在表中位置 ，若查找成功，函数 返回表项的位置，否则函数返回-1  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Find(Type& x) const  
{  
int i=0;  
while(i&lt;=last&&data\[i]!=x)  
i++;  
if(i&gt;last)  
return -1;  
else  
return i;  
}

//判断x是否在表中  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::IsIn(Type& x)  
{  
int i=0,found=0;  
while(i&lt;==last&&!found)  
if(data\[i]!=x)  
i++;  
else  
found=1;  
return found;}

//插入x在表中第i个位置处。函数返回插入是否成功的信息，若为0则插入不成功。  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Insert(Type& x,int i)  
{  
if(i&lt;0||i&gt;last+1||last==MaxSize-1)  
return 0;  
else  
{  
last++;  
for(int j=last;j&gt;i;j--)  
data\[j]=data\[j-1];  
data\[i]=x;  
return 1;}  
}  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Remove(Type& x)  
{  
int i=Find(x);  
if(i&gt;=0)  
{  
last--;  
for(int j=i;j&lt;=last;j++)  
data\[j]=data\[j+1];  
return 1;  
}  
return 0;  
}  
//寻找x的后继数据  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Next(Type& x)  
{  
if(i&gt;=0&&i&lt;last)  
return i+1;  
else  
return -1;  
}  
//寻找x的前驱数据  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Prior(Type& x)  
{  
int i=Find(x);  
if(i&gt;0&&i&lt;=last)  
return i-1;  
else  
return -1;  
}

//合并顺序表LA与LB，重复元素只留一下。这个值得深入研究  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::Union(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB)  
{  
int n=LA.Length();  
int m=LB.Length();  
for(int i=0;i&lt;m;i++)  
{  
Type x=LB.Get(i);  
int k=LA.Find(x);  
if(k==-1)  
{  
LA.Insert(n+1,x);  
n++;  
}  
}  
}  
//求顺序表LA与LB中的共有元素，其实也是寻找交集元素  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::Intersection(SeqList&lt;Type&gt; &LA,SeqList&lt;Type&gt;& LB)  
{  
int n=LA.Length();  
int m=LB.Length();  
int i=0;  
while(i&lt;n)  
{  
Type x=LA.Get(i);  
int k=LB.Find(x);  
if(-1==k)  
{  
LA.Remove(x);  
n--;  
}  
else  
i++;  
}  
}

//自己写的输出数据方法  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::PrintList()  
{  
int l=Length()-1;  
for(int i=0;i&lt;l;i++)  
cout&lt;&lt;"list"&lt;&lt;i&lt;&lt;"= "&lt;&lt;data\[i]&lt;&lt;endl;;  
}  
//自己写的输入数据方法  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::InputList()  
{  
int l=Length()-1;  
for(int i=0;i&lt;l;i++)  
{  
cout&lt;&lt;"enter data\["&lt;&lt;i&lt;&lt;"]:";  
cin&gt;&gt;data\[i];  
}}  
#endif

//调用程序

//seqlist.cpp  
/* 测试主程序 * \*- \*/  
#include &lt;iostream.h&gt;  
#include "SeqList.h"  
//const defaultSize=8;  
void main()  
{  
SeqList&lt;int&gt; Intseqlist(8);  
cout&lt;&lt;"Intseqlist.length:"&lt;&lt;Intseqlist.Length()&lt;&lt;endl;  
cout&lt;&lt;"未初始化的数据是没有规则的如下："&lt;&lt;endl;  
Intseqlist.PrintList();

cout&lt;&lt;"输入int到数组:"&lt;&lt;endl;  
Intseqlist.InputList();

cout&lt;&lt;"新的数据:"&lt;&lt;endl;  
Intseqlist.PrintList();

//add check get(x) code  
int tv;  
cout&lt;&lt;"Get(x):enter a int:";  
cin&gt;&gt;tv;  
cout&lt;&lt;"get(x)="&lt;&lt;Intseqlist.Get(tv)&lt;&lt;endl;

//请各位多写测试代码，争取把上面的类的功能都用一次  
//这就是一个简单的程序测试工程师的一部分工作内容

}

//欢迎大家从这个线性表的数组实现代码中更加完美

以一个现成的模板实现了线性表的顺序结构实现，VC6.0调试OK

请大家以开源的方式来完善这个算法 ，以跟贴方式来添加代码

请大家往这个下面继续添加完整的可以运行的线性表的顺序结构实现代码

/*

线性表的顺序结构实现，数组C++实现法，VC调试OK  
线性表可以用顺序结构（是用数组线性表来实现）来实现，也可以用链式结构来实现。  
我们以顺序结构为例：  
线性表的基本的操作：  
(1)创建线性表  
(2)初始化线性表SeqList()  
(3)插入元素Insert(Type& x ,int i)  
(4)删除元素Remove(Type& x)  
(5)查找元素  
按位置查找对象Get(int i)  
按对象查找位置Find(Type& x)  
(6)计算表长度Length();

扩展功能：  
顺序表满判断IsFull()  
顺序表空判断IsEmpty()  
输出顺序表的数据PrintList()  
输入顺序表的数据InputList()  
判断x是否在表中IsIn(Type& x)  
寻找x的后继Next(Type& x)  
寻找x的前驱Prior(Type& x)  
合并两个数组Union(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB)  
求两个数组的交集

  
注意：  
(1)插入元素时要把插入位置后的所有元素“从后往前”各向后移动一个位置。  
(2)删除元素时要把删除位置后的所有元素“从前往后”各向前移动一个位置。

\*/  
//SeqList.h  
#ifndef SEQLIST\_H  
#define SEQLIST\_H

template&lt; class Type&gt; class SeqList  
{  
public:  
SeqList(int MaxSize=6);//构造函数定义和实现  
~SeqList() {delete \[]data; }//析构函数定义和实现  
int Length() const {return last+1;}//计算表长度定义和实现  
Type Get(int i)　　　　　　//取第i个元素的值  
{if (i&lt;0||i&gt;last)  
cerr&lt;&lt;"out of range...";  
else  
return data\[i];  
}  
int Find(Type& x) const;//定位函数：找x在表中的位置  
int Insert(Type& x ,int i);//插入x在表中第i个位置处  
int Remove(Type& x);//删除x  
//新添加功能  
int Next(Type& x);//寻找x的后继  
int Prior(Type& x);//寻找x的前驱  
int IsEmpty() {return last==-1;}//判断顺序表是否为空，空则返回1；否则返回0  
int IsFull() {return last==MaxSize-1;} //判断顺序表满否，满则返回1；否则返回0  
int IsIn(Type& x);//判断x是否在表中  
void Union(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB);//合并LA,LB,重复元素只留一下  
void Intersection(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB);//求LA,LB中的共有元素  
void PrintList();//输出顺序表的数据  
void InputList();//输入顺序表的数据

private:  
Type* data;//存放顺序表的数组  
int MaxSize;//顺序表最大可容纳项数  
int last;//顺序表当前是已存表项的最后位置  
};  
//构造函数，通过描写参数sz定义数组的长度。  
template &lt;class Type&gt; SeqList&lt;Type&gt;::SeqList(int sz)  
{  
if(sz&gt;0)  
MaxSize=sz;  
else  
MaxSize=6;  
last=MaxSize - 1;  
data=new Type\[MaxSize];

}

//定位，找x在表中位置 ，若查找成功，函数 返回表项的位置，否则函数返回-1  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Find(Type& x) const  
{  
int i=0;  
while(i&lt;=last&&data\[i]!=x)  
i++;  
if(i&gt;last)  
return -1;  
else  
return i;  
}

//判断x是否在表中  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::IsIn(Type& x)  
{  
int i=0,found=0;  
while(i&lt;==last&&!found)  
if(data\[i]!=x)  
i++;  
else  
found=1;  
return found;}

//插入x在表中第i个位置处。函数返回插入是否成功的信息，若为0则插入不成功。  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Insert(Type& x,int i)  
{  
if(i&lt;0||i&gt;last+1||last==MaxSize-1)  
return 0;  
else  
{  
last++;  
for(int j=last;j&gt;i;j--)  
data\[j]=data\[j-1];  
data\[i]=x;  
return 1;}  
}  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Remove(Type& x)  
{  
int i=Find(x);  
if(i&gt;=0)  
{  
last--;  
for(int j=i;j&lt;=last;j++)  
data\[j]=data\[j+1];  
return 1;  
}  
return 0;  
}  
//寻找x的后继数据  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Next(Type& x)  
{  
if(i&gt;=0&&i&lt;last)  
return i+1;  
else  
return -1;  
}  
//寻找x的前驱数据  
template &lt;class Type&gt; int SeqList&lt;Type&gt;::Prior(Type& x)  
{  
int i=Find(x);  
if(i&gt;0&&i&lt;=last)  
return i-1;  
else  
return -1;  
}

//合并顺序表LA与LB，重复元素只留一下。这个值得深入研究  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::Union(SeqList&lt;Type&gt;& LA,SeqList&lt;Type&gt;& LB)  
{  
int n=LA.Length();  
int m=LB.Length();  
for(int i=0;i&lt;m;i++)  
{  
Type x=LB.Get(i);  
int k=LA.Find(x);  
if(k==-1)  
{  
LA.Insert(n+1,x);  
n++;  
}  
}  
}  
//求顺序表LA与LB中的共有元素，其实也是寻找交集元素  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::Intersection(SeqList&lt;Type&gt; &LA,SeqList&lt;Type&gt;& LB)  
{  
int n=LA.Length();  
int m=LB.Length();  
int i=0;  
while(i&lt;n)  
{  
Type x=LA.Get(i);  
int k=LB.Find(x);  
if(-1==k)  
{  
LA.Remove(x);  
n--;  
}  
else  
i++;  
}  
}

//自己写的输出数据方法  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::PrintList()  
{  
int l=Length()-1;  
for(int i=0;i&lt;l;i++)  
cout&lt;&lt;"list"&lt;&lt;i&lt;&lt;"= "&lt;&lt;data\[i]&lt;&lt;endl;;  
}  
//自己写的输入数据方法  
template &lt;class Type&gt; void SeqList&lt;Type&gt;::InputList()  
{  
int l=Length()-1;  
for(int i=0;i&lt;l;i++)  
{  
cout&lt;&lt;"enter data\["&lt;&lt;i&lt;&lt;"]:";  
cin&gt;&gt;data\[i];  
}}  
#endif

//调用程序

//seqlist.cpp  
/* 测试主程序 * \*- \*/  
#include &lt;iostream.h&gt;  
#include "SeqList.h"  
//const defaultSize=8;  
void main()  
{  
SeqList&lt;int&gt; Intseqlist(8);  
cout&lt;&lt;"Intseqlist.length:"&lt;&lt;Intseqlist.Length()&lt;&lt;endl;  
cout&lt;&lt;"未初始化的数据是没有规则的如下："&lt;&lt;endl;  
Intseqlist.PrintList();

cout&lt;&lt;"输入int到数组:"&lt;&lt;endl;  
Intseqlist.InputList();

cout&lt;&lt;"新的数据:"&lt;&lt;endl;  
Intseqlist.PrintList();

//add check get(x) code  
int tv;  
cout&lt;&lt;"Get(x):enter a int:";  
cin&gt;&gt;tv;  
cout&lt;&lt;"get(x)="&lt;&lt;Intseqlist.Get(tv)&lt;&lt;endl;

//请各位多写测试代码，争取把上面的类的功能都用一次  
//这就是一个简单的程序测试工程师的一部分工作内容

}

//欢迎大家从这个线性表的数组实现代码中更加完美