顺序表类模板


#include<iostream>
#include <cstdlib>
using namespace std;
template <typename T,int size>class seqlist{
T slist[size];                                     //存放顺序表的数组
int Maxsize;                                       //最大可容纳项数 
int last;                                          //已存表项的最后位置
public:
seqlist(){last=-1;Maxsize=size;}                   //初始化为空表
int Length() const{return last+1;}                 //计算表长度
int Find(T & x)const;                              //寻找x在表中位置(下标)
bool IsIn(T & x);                                  //判断x是否在表中
bool Insert(T & x,int i);                          //x插入到列表中第i个位置处(下标)
bool Remove(T & x);                                //删除x
int Next(T & x);                                   //寻找x的后继位置
int Prior(T & x);                                  //寻找x的前驱位置
bool IsEmpty(){return last==-1;}                   //判断表是否空
bool IsFull(){return last==Maxsize -1;}            //判断表是否满
T Get(int i){return i<0||i>last?NULL:slist[i];}    //取第i个元素之值
T& operator[](int i);                              //重载下标运算符[]
};
template <typename T,int size> int seqlist<T,size>::Find(T & x)const{
  //注意格式,const表示该函数的this指针为const,即被访问对象的数据不能被修改。
  //如被修改,编译器会警告,防止编程时失误。
int i=0;
while(i<=last && slist[i]!=x) i++;  //顺序查找是否有x 
if (i>last) return -1;              //未找到,返回-1
else return i;                      //找到,返回下标
}
template <typename T,int size> bool seqlist<T,size>::IsIn(T & x){
int i=0;
bool found=0;
while(i<=last && !found)            //换了一种方法来查找
if (slist[i]!=x) i++;
else found=1;                       //找到
return found;
}
template <typename T,int size> bool seqlist<T,size>::Insert(T & x, int i){
int j;
if (i<0||i>last+1||last==Maxsize -1) return false;  //插入位置不合理,不能插入(健壮性)
else{
last++;
for(j=last;j>i;j--) slist[j]=slist[j-1];    //从表最后位置向前依次后移,空出指定位置
slist[i]=x;
return true;
}
}
template <typename T,int size> bool seqlist<T,size>::Remove(T & x){
int i=Find(x),j;                                        //先去找x在哪个位置
if(i>=0){
last--;
for(j=i;j<=last;j++) slist[j]=slist[j+1];         //依次前移,保证表连续
return true;
}
return false;                                         //表中不存在x
}
template <typename T,int size> int seqlist<T,size>::Next(T & x){
int i=Find(x);
if(i>=0 && i<last) return i+1;                        //x后继位置
else return -1;                                       //x不在表中,或在表末尾
}
template <typename T,int size> int seqlist<T,size>::Prior(T & x){
int i=Find(x);
if(i>0 && i<=last)  return i-1;                       //x前驱的位置
else return -1;
}
template <typename T,int size> T& seqlist<T,size>::operator[](int i){
if(i>last+1||i<0||i>=Maxsize){
cout<<"下标出界!"<<endl;
exit(1);
}
if(i>last) last++;  //下标运算符[],只能增加表的元素,不能减少
return slist[i];
}


int main(){
seqlist <int,100> seqlisti;                         //顺序表对象seqlisti的元素为整型
int i,j,k,a[10]={2,3,5,7,11,13,17,19,23,29};
for(j=0;j<10;j++)
if (!seqlisti.Insert(a[j],j)){                      //把素数写入
cout<<"表太大放不下了!"<<endl;
break;
}
j=seqlisti.Length();
for(i=0;i<j;i++) cout<<seqlisti.Get(i)<<' ';        //打印出seqlisti.slist[]-素数表
cout<<endl ;
for(j=0;j<10;j++) seqlisti[j]=0;                    //采用下标运算符运算
for(j=0;j<10;j++) cout<<seqlisti[j]<<' ';
cout<<endl;
for(j=0;j<10;j++) seqlisti[j]=a[j];
seqlisti[10]=31;                                    //实验能否增加元素
for(j=0;j<11;j++) cout<<seqlisti[j]<<' ';
cout<<endl;


k=7;
if (seqlisti.IsIn(k)) cout<<"素数7在顺序表中"<< endl;
//因形参为引用,所以实参不可用整数常量7
else cout <<"素数7不在顺序表中"<<endl;
k=17;
if (seqlisti.Remove (k)) cout<<"删除素数17"<<endl;  //删除素数17
else cout<<"找不到素数17,无法删除";
j=seqlisti.Length( ) ;
for (i=0;i<j;i++) cout<<seqlisti.Get(i)<<' ';      //打印剩下的素数
cout<<endl;
if (seqlisti.Insert(k,j-1)){                       // 把素数17装回去,成功则打印
j=seqlisti.Length ( );
for (i=0;i<j;i++)  cout<<seqlisti.Get(i)<<' '; 
cout<<endl;
}
cout<<"打印17后一个素数:"<<seqlisti.Get(seqlisti.Next(k)) <<endl;
cout<<"打印17前一个素数:"<<seqlisti.Get(seqlisti.Prior(k))<<endl;
cout<<"素数17在表中位置(下标)为:"<<seqlisti.Find(k)<<endl;
if(seqlisti.IsEmpty( ))  cout<<"表是空的"<<endl;
else cout<<"表不空"<<endl;
if (seqlisti.IsFull())  cout<<"表是满的"<<endl; 
else cout<<"表也不满"<<endl;
if (seqlisti.IsIn(k))  cout<<"素数17在表中"<<endl;
return 0;
}
C++中,如果你想创建一个类似于STL `vector`的自定义顺序表类模板,你可以按照以下步骤进行: 首先,你需要定义一个模板类,命名为`CustomSequence`,它有一个动态大小的数组作为存储容器,并提供了一些基本操作,如元素的添加、删除、访问等。这里是一个基础的实现示例: ```cpp template <typename T> class CustomSequence { private: std::size_t capacity; T* elements; // 存储数据的动态数组 public: // 默认构造函数,初始化容量为0,元素指针为空 CustomSequence() : capacity(0), elements(nullptr) {} // 构造函数指定初始容量 CustomSequence(std::size_t initial_capacity = 4) : capacity(initial_capacity), elements(new T[capacity]) {} // 拷贝构造函数,分配足够的空间复制源序列 CustomSequence(const CustomSequence& other) : capacity(other.capacity), elements(new T[capacity]) { for (std::size_t i = 0; i < capacity && i < other.capacity; ++i) { elements[i] = other.elements[i]; } } // 释放内存并清零 ~CustomSequence() { delete[] elements; } // 添加元素到末尾 void push_back(T value) { if (capacity == elements capacity) { resize(capacity * 2); // 当满时,双倍扩容 } elements[capacity++] = value; } // ... 其他成员函数如访问元素、查找、删除等 private: // 扩容函数 void resize(std::size_t new_capacity) { T* new_elements = new T[new_capacity]; for (std::size_t i = 0; i < capacity; ++i) { new_elements[i] = elements[i]; } delete[] elements; elements = new_elements; capacity = new_capacity; } // 禁止直接赋值和拷贝赋值(防止浅拷贝) CustomSequence& operator=(const CustomSequence&) = delete; }; ``` 在这个例子中,我们实现了`push_back`方法来添加元素,以及`resize`方法用于动态调整数组大小。此外,还通过`= delete`禁止了默认的拷贝构造函数和赋值运算符,以防意外的浅拷贝。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值