顺序表(约瑟夫环)

          概念:线性表的顺序存储结构称为顺序表,顺序表中的每一种数据类型属于同构,你要知道第一个数据元素的起始地址,就可以计算出其任一元素的地址  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
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值