顺序表(约瑟夫环)
来源:互联网 发布:大数据o2o概念股龙头 编辑:程序博客网 时间:2024/06/05 11:17
概念:线性表的顺序存储结构称为顺序表,顺序表中的每一种数据类型属于同构,你要知道第一个数据元素的起始地址,就可以计算出其任一元素的地址 lOC(ai)=Loc(a0)+i*c 其中c=sizeof(T) 1.顺序表的各个数据元素的逻辑顺序与其储存的物理顺序一致 2.顺序表的数据元素即可进行顺序访问,也可以进行随机访问
1.三个成员必须是私有属性,防止错误操作引发的错误
2.类模板中T代表顺序表的元素的数据类型,声明对象时T必须是确定数据类型
3.自动扩充数组,若果插入元素时数组已满,需要重新创建一个数组容量是原来容量的2倍的新数组,并将原来的数据元素复制到新数组中
4.如果操作元素序号i不在合法范围内,通过改变序号i为0,Length或直接以抛出异常的方式达到正确执行的目的
顺序表性能分析:
在等概率情况下,在顺序表插入和删除一个元素,平均需要移动一半左右的元素,时间复杂度是O(n),在n较大的情况下,插入和删除算法的时间效率并不是很高,而且插入元素时,如果数组空间溢出而导致数组扩容,则复制元素占用的时间较多,使得插入效率更低
#include <iostream.h>
template <class T> //顺序表类 T是指定元素类型
class SequenceList
{
private:
T *Element; //定义一维数组存储顺序表
int MaxSize; //顺序表数组大小
int Length; // 顺序表长度
public:
SequenceList(int MaxSize = 64); //默认构造函数,构造制定大小的控表
SequenceList(T Value [], int n); //由指定数组构造顺序表
~SequenceList();
bool isEmpty (); //判断顺序表是否为空
int GetLength (); //求顺序表长度
T GetElem (int i); //求第i个元素的值
bool SertElem (int i,T x); //将第i个元素设置为x
void InsertElem (int i, T x ); //插入x作为第i个元素
void InsertElem(T x); //在顺序表最后插入x
bool RemoveElem (int i, T& Old); //删除第i个元素,被删除元素值存放于Old变量中
void Clear (); //清空顺序表
friend ostream& operator<<(ostream& out,SequenceList<T>&list);
**{
out<<"(";
if(List.Length>0)
{
out<<List.Element[0];
for(int i=1;i<List.Length;i++)
out<<","<<List.Element[i];
}
out<<")\n";
return out;
}**////运算符重载,输出顺序表所有元素
};
template<class T>
SequenceList<T>::SequenceList(int Size)//构造指定大小的空表
{
this->MaxSize = Size<64?64:Size;
this->Element = new T[this->MaxSize];
this->Length = 0;
}
template <class T>
SequenceList<T>::SequenceList(T Value[], int n) //由指定数组Value构造顺序表
{
if(n>0)
{
this->Element = new T[2*n];
this->MaxSize = 2*n;
for(int i=0; i<n; i++)
this->Element[i] = Value[i];
this->Length = n;
}
}
template <class T>
SequenceList<T>::~SequenceList()
{
delete[]this->Element;
}
template <class T>
bool SequenceList<T>::isEmpty()
{
return Length == 0;
}
template <class T>
int SequenceList<T>::GetLength()
{
return Length;
}
template <class T>
T SequenceList<T>::GetElem(int i)
{
if(i>=0 && i<Length)
return Element[i];
throw "参数i指定元素序号无效";
}
template <class T>
bool SequenceList<T>::SertElem(int i, T x)
{
if(i>=0 && i<length)
{
Element[i]=x;
return true;
}
elae
return false;
}
template <class T>
ostream& operator <<(ostream& out,SequenceList<T>&List)
{
out<<"(";
if(List.Length>0)
{
out<<List.Element[0];
for(int i=1;i<List.Length;i++)
out<<","<<List.Element[i];
}
out<<")\n";
return out;
}
template<class T>
void SequenceList<T>::InsetElem(int i, T x)
{
if(Length == MaxSize)
{
T *Temp =Element;
Element = new T[MaxSize*2];
for(int i=0;i<MaxSize;i++)
Element[i] = Temp[i];
MaxSize*=2;
}
if(i<0)
i=0;
else
if(i>Length)
i=Length;
for(int j=Length-1;j>=i;j--)
Element[j+1] = Element[j];
Element[i] = x;
Length++;
}
template<class T>
void SequenceList<T>::InsertElem(T x)
{
InsertElem(Length, x);
}
template <class T>
bool SequenceList<T>::RemoveElem(int i, T &Old)//删除第i个元素
{
if(Length>0 && i>=0 && i<Length)
{
Old =Element [i];
for(int j=i+1;j<Length; j++)
Element[j-1]=Element[j];
Length--;
return true; //操作成功
}
else
return false;
}
template<class T>
void SequenceList<T>::Clear()
{
Length = 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include"SequenceList.h"
void Josephus (int number, int start, int distance)
{
SequenceList<char>Jose(number);
int i=0;
for(i=0; i<number; i++)
Jose.InsertElem('A'+i);
cout<<"约瑟夫环("<<number<<","<<start<<","<<distance<<"),"<<Jose;
i=start;
while(Jose.GetLength()>1)
{
i=(i+distance-1)%Jose.GetLength();
char DelNumber;
if(Jose.RemoveElem(i,DelNumber))
cout<<"删除"<<DelNumber<<",";
cout<<Jose;
}
void main()
{
JosePhus(5,0,2);
}
}
0 0
- 顺序表(约瑟夫环)
- 顺序表之约瑟夫环(josephus)
- 顺序表求约瑟夫环。
- 约瑟夫环-顺序表-C++
- 约瑟夫问题(顺序表)
- 0010用顺序表实现约瑟夫环(Josephus)问题
- 顺序表实现Josephus(约瑟夫斯)环问题
- 两种方法实现约瑟夫环(链表,顺序表)
- 使用顺序表求解约瑟夫环问题
- 用顺序表实现约瑟夫环问题
- 使用顺序表求解约瑟夫环问题 (自定义顺序表)
- 约瑟夫环:出队顺序
- 1067:顺序排号(约瑟夫环)
- 使用顺序表求解约瑟夫环问题 (利用java中的ArrayList)
- 使用顺序表类SeqList求解约瑟夫环问题
- 约瑟夫环问题(顺序表)——C++实现
- 【学习总结】顺序表模拟约瑟夫问题
- 数据结构之顺序表应用-----《约瑟夫问题》
- 《Redis实战》读后感
- Windows下安装TensorFlow
- 浅析name==null, "".equals(name)和name.length==0三者的区别
- 简介Opencv在Python中的使用
- c++单元测试框架Catch
- 顺序表(约瑟夫环)
- Kotlin初探
- poj 1141Brackets Sequence
- C++面试之GetMemory问题
- Nim游戏博弈(收集完全版)
- Flink容错机制源码分析
- what is a processor (上)
- start stop reload
- Java关键字static,面试经常会问到(二)