list c++template

以一个现成的模板实现了线性表的顺序结构实现,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<Type>& LA,SeqList<Type>& LB)
求两个数组的交集


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

*/
//SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H

template< class Type> class SeqList
{
public:
SeqList(int MaxSize=6);//构造函数定义和实现
~SeqList() {delete []data; }//析构函数定义和实现
int Length() const {return last+1;}//计算表长度定义和实现
Type Get(int i)      //取第i个元素的值
{if (i<0||i>last)
cerr<<"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<Type>& LA,SeqList<Type>& LB);//合并LA,LB,重复元素只留一下
void Intersection(SeqList<Type>& LA,SeqList<Type>& LB);//求LA,LB中的共有元素
void PrintList();//输出顺序表的数据
void InputList();//输入顺序表的数据

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

}

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

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

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

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

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

//调用程序

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

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

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

//add check get(x) code
int tv;
cout<<"Get(x):enter a int:";
cin>>tv;
cout<<"get(x)="<<Intseqlist.Get(tv)<<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<Type>& LA,SeqList<Type>& LB)
求两个数组的交集


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

*/
//SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H

template< class Type> class SeqList
{
public:
SeqList(int MaxSize=6);//构造函数定义和实现
~SeqList() {delete []data; }//析构函数定义和实现
int Length() const {return last+1;}//计算表长度定义和实现
Type Get(int i)      //取第i个元素的值
{if (i<0||i>last)
cerr<<"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<Type>& LA,SeqList<Type>& LB);//合并LA,LB,重复元素只留一下
void Intersection(SeqList<Type>& LA,SeqList<Type>& LB);//求LA,LB中的共有元素
void PrintList();//输出顺序表的数据
void InputList();//输入顺序表的数据

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

}

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

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

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

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

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

//调用程序

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

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

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

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

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

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ templates are a powerful feature of the C++ programming language that allow generic programming. Templates enable the creation of functions and classes that can work with different data types without the need for separate implementations for each data type. Templates are defined using the keyword "template" followed by a list of template parameters enclosed in angle brackets "< >". The template parameters can be either type parameters or non-type parameters, depending on whether they represent a data type or a value. For example, a type parameter might be used to specify the data type of a container class, while a non-type parameter might be used to specify the size of an array. Here is an example of a simple function template that returns the maximum of two values: ```c++ template<typename T> T max(T a, T b) { return a > b ? a : b; } ``` In this example, the "typename" keyword is used to indicate that T is a type parameter. The function can be used with any data type for which the ">" operator is defined. Templates can also be used to define class templates, which are similar to regular classes but can work with different data types. Here is an example of a simple class template for a stack: ```c++ template<typename T> class Stack { public: void push(T value); T pop(); private: std::vector<T> data_; }; template<typename T> void Stack<T>::push(T value) { data_.push_back(value); } template<typename T> T Stack<T>::pop() { T value = data_.back(); data_.pop_back(); return value; } ``` In this example, the class template is defined with a single type parameter T. The member functions push and pop are defined outside the class definition using the scope resolution operator "::". Templates are a powerful tool that can greatly simplify code and make it more reusable. However, they can also be complex and difficult to debug. It is important to use templates judiciously and to thoroughly test them with a variety of data types.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值