顺序表实现集合及大整数运算
2017-04-07 14:26
309 查看
#include<iostream>
using namespace std;
const int MaxSize=5;
template<class DataType>
class Seqlist
{
public:
Seqlist(){length=0;}
Seqlist(DataType a[],int n);
~Seqlist(){}
int Length(){return length;}
DataType Get(int i);
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
DataType data[MaxSize];
int length;
};
template<class DataType>//顺序表一开始已经确定了顺序表的最大存储容量,所以要判断是否会上溢
Seqlist<DataType>::Seqlist(DataType a[],int n)
{
if(n>MaxSize)
cout<<"参数非法"<<endl;
else
{
for(int i=0;i<n;i++)
{
data[i]=a[i];
}
length=n;//length=n写在循环外面
}
}
template<class DataType>
DataType Seqlist<DataType>::Get(int i)//查找位置会涉及位置非法的问题
{
if(i<1||i>length)
{
cout<<"查找位置非法";
}
else return data[i-1];
}
template<class DataType>
int Seqlist<DataType>::Locate(DataType x)
{
for(int i=0;i<length;i++)
{
if(data[i]==x)
return i+1;
}
return 0;//注意考虑到还有找不到的情况,注意区分各种情况
}
template<class DataType>//插入算法会占据空间可能造成上溢
void Seqlist<DataType>::Insert(int i,DataType x)//位置插入也会涉及位置非法的问题
{
if(i<1||i>length+1)cout<<"插入位置非法"<<endl;
if(length>=MaxSize)cout<<"上溢"<<endl;
for(int j=length;j>=i-1;j--)
{
data[j+1]=data[j];
}
data[i-1]=x;
length++;//不要忘了还有长度这个变量
}
template<class DataType>
DataType Seqlist<DataType>::Delete(int i)//插入和删除都不要忘了改Length
{
if(i<1||i>length)
cout<<"位置非法"<<endl;
if(length==0)
cout<<"下溢"<<endl;
DataType x=data[i-1];
for(int j=i;j<length;j++)
{
data[j-1]=data[j];
}
length--;
return x;
}
template<class DataType>
void Seqlist<DataType>::PrintList()
{
for(int i=0;i<length;i++)
{
cout<<data[i];
}
}
int main()
{
int a[5]={1,2,3,4,5};
Seqlist<int> s(a,5);
/*cout<<s.Get(5);
cout<<endl;
s.PrintList();
cout<<endl;
s.Delete(2);
s.PrintList();
cout<<endl;*/
s.Insert(2,2);
s.PrintList();
cout<<endl;
cout<<s.Locate(2);
}
using namespace std;
const int MaxSize=5;
template<class DataType>
class Seqlist
{
public:
Seqlist(){length=0;}
Seqlist(DataType a[],int n);
~Seqlist(){}
int Length(){return length;}
DataType Get(int i);
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
DataType data[MaxSize];
int length;
};
template<class DataType>//顺序表一开始已经确定了顺序表的最大存储容量,所以要判断是否会上溢
Seqlist<DataType>::Seqlist(DataType a[],int n)
{
if(n>MaxSize)
cout<<"参数非法"<<endl;
else
{
for(int i=0;i<n;i++)
{
data[i]=a[i];
}
length=n;//length=n写在循环外面
}
}
template<class DataType>
DataType Seqlist<DataType>::Get(int i)//查找位置会涉及位置非法的问题
{
if(i<1||i>length)
{
cout<<"查找位置非法";
}
else return data[i-1];
}
template<class DataType>
int Seqlist<DataType>::Locate(DataType x)
{
for(int i=0;i<length;i++)
{
if(data[i]==x)
return i+1;
}
return 0;//注意考虑到还有找不到的情况,注意区分各种情况
}
template<class DataType>//插入算法会占据空间可能造成上溢
void Seqlist<DataType>::Insert(int i,DataType x)//位置插入也会涉及位置非法的问题
{
if(i<1||i>length+1)cout<<"插入位置非法"<<endl;
if(length>=MaxSize)cout<<"上溢"<<endl;
for(int j=length;j>=i-1;j--)
{
data[j+1]=data[j];
}
data[i-1]=x;
length++;//不要忘了还有长度这个变量
}
template<class DataType>
DataType Seqlist<DataType>::Delete(int i)//插入和删除都不要忘了改Length
{
if(i<1||i>length)
cout<<"位置非法"<<endl;
if(length==0)
cout<<"下溢"<<endl;
DataType x=data[i-1];
for(int j=i;j<length;j++)
{
data[j-1]=data[j];
}
length--;
return x;
}
template<class DataType>
void Seqlist<DataType>::PrintList()
{
for(int i=0;i<length;i++)
{
cout<<data[i];
}
}
int main()
{
int a[5]={1,2,3,4,5};
Seqlist<int> s(a,5);
/*cout<<s.Get(5);
cout<<endl;
s.PrintList();
cout<<endl;
s.Delete(2);
s.PrintList();
cout<<endl;*/
s.Insert(2,2);
s.PrintList();
cout<<endl;
cout<<s.Locate(2);
}
相关文章推荐
- 我的C实践(3):用宏和位运算来实现整数集合
- 我的C实践(3):用宏和位运算来实现整数集合
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例
- 我的C实践(3):用宏和位运算来实现整数集合
- 顺序表实现集合运算
- 整数集合的若干种实现
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- inux shell 实现 四则运算(整数及浮点) 简单方法
- 重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算
- C#实现超长位整数运算
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- 大整数的运算实现
- 计算器C#代码(实现整数的四则运算)
- 习题10:参照Windows系统“附件”中的“计算器”,自行编写一个简易的计算器。要求:可以实现由0~4构成的整数的加减运算。
- 重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算
- 线性表的顺序储存及运算实现-------------顺序表
- java是实现的大整数运算!
- 大整数四则运算算法与实现(C++)
- 整数集合的实现
- 《C链表实现集合的差运算》