实验内容
[问题描述]
基本线性表就地逆置是指在基本线性表现有空间的基础上,将基本线性表中的数据元素交换位置排列,排列完之后,新的顺序序列与原来的顺序序列刚好相反。如原来顺序序列“abcdef”,就地逆置后的新顺序序列为“fedcba”。根据基本线性表的链式和顺序两种存储结构分别完成:
(1) 顺序结构的就地逆置。
(2) 链式结构的就地逆置。
[基本要求]
充分理解题目要求,在对基本线性表逆置时,必须是在基本线性表原有空间的基础上进行,不能借助临时变量所生成的临时空间,也不能借助其他形式的临时空间。且算法时间复杂度要求为O(n)。
【代码】
- #include<iostream>
- #define DefaultListSize 255
- using namespace std;
- template <class Elem> class List
- {
- public:
- virtual bool insert(const Elem&) = 0;
- virtual bool append(const Elem&) = 0;
- virtual bool remove(Elem&) = 0;
- virtual void setStart() = 0;
- virtual void setEnd() = 0;
- virtual void prev() = 0;
- virtual void next() = 0;
- virtual int leftLength() = 0;
- virtual int rightLength() = 0;
- virtual bool setPos(int pos) = 0;
- virtual bool getValues(Elem&) = 0;
- virtual void print() const = 0;
- };
- template <class Elem>
- class AList : public List<Elem>
- {
- private:
- int maxSize;
- int listSize;
- int fence;
- Elem *listArray;
- public:
- AList(int size=DefaultListSize)
- {
- maxSize = size;
- listSize = fence = 0;
- listArray = new Elem[maxSize];
- }
- ~AList()
- {
- delete [] listArray;
- }
- bool insert(const Elem&);
- bool append(const Elem&);
- bool remove(Elem&);
- void setStart();
- void setEnd();
- void prev();
- void next();
- int leftLength();
- int rightLength();
- bool setPos(int pos);
- bool getValues(Elem&);
- void print() const;
- void reverlist();
- };
- template <class Elem>
- bool AList<Elem>::insert(const Elem& item) //在fence后插入元素
- {
- if(listSize==maxSize)
- return false;
- for(int i=listSize;i>fence;i--)
- listArray[i] = listArray[i-1];
- listArray[fence]=item;
- listSize++;
- return true;
- }
- template <class Elem>
- bool AList<Elem>::append(const Elem& item) //在表尾插入元素
- {
- if(listSize == maxSize)
- return false;
- listArray[listSize++]=item;
- return true;
- }
- template <class Elem>
- bool AList<Elem>::remove(Elem& it) //移出fence后元素
- {
- if(rightLength() == 0)
- return false;
- it=listArray[fence];
- for(int i=fence;i<maxSize;i++)
- listArray[i]=listArray[i+1];
- listSize--;
- return true;
- }
- template <class Elem>
- void AList<Elem>::setStart() //将fence设置为表的开始位置
- {
- fence=0;
- }
- template <class Elem>
- void AList<Elem>::setEnd() //将fence设置为表尾位置
- {
- fence=listSize;
- }
- template <class Elem>
- void AList<Elem>::prev() //将fence前移一位
- {
- if(fence!=0)
- fence--;
- }
- template <class Elem>
- void AList<Elem>::next() //将fence后移一位
- {
- if(fence<=listSize)
- fence++;
- }
- template <class Elem>
- int AList<Elem>::leftLength() //fence左侧元素数
- {
- return fence;
- }
- template <class Elem>
- int AList<Elem>::rightLength() //fence右侧元素数
- {
- return (listSize-fence);
- }
- template <class Elem>
- bool AList<Elem>::setPos(int pos) //设置fence位置
- {
- if((pos >= 0)&&(pos <=listSize))
- fence=pos;
- return ((pos >=0)&&(pos <=listSize));
- }
- template <class Elem>
- bool AList<Elem>::getValues(Elem& it) //获取fence后面一个元素值
- {
- if(listSize==0)
- return false;
- it=listArray[fence];
- return true;
- }
- template <class Elem>
- void AList<Elem>::print() const //打印线性表
- {
- int temp=0;
- cout<<"In AList:<";
- while(temp < fence)
- cout<<listArray[temp++]<<" ";
- cout<<"|";
- while(temp < listSize)
- cout<<listArray[temp++]<<" ";
- cout<<">\n";
- }
- template<class Elem>
- void AList<Elem>::reverlist() //转置
- {
- int i;
- if(listSize==0)
- {
- cout<<"线性表为空!";
- }
- if(listSize==DefaultListSize)//线性表为满时利用异或操作进行转置
- {
- for(i=0;i<listSize/2;i++)
- {
- listArray[i]^=listArray[listSize-i-1];
- listArray[listSize-i-1]^=listArray[i];
- listArray[i]^=listArray[listSize-i-1];
- }
- }
- if(listSize<DefaultListSize)//线性表不为空时进行转置
- {
- for(i=0;i<listSize/2;i++)
- {
- listArray[listSize+1]=listArray[i];//将第listSize+1号单元作为中间存储单元
- listArray[i]=listArray[listSize-i-1];
- listArray[listSize-i-1]= listArray[listSize+1];
- }
- }
- }
- int main()
- {
- AList<int> a;
- a.append(1);
- a.append(10);
- a.append(20);
- a.append(30);
- a.append(60);
- cout<<"原始表:"<<endl;
- a.print();
- cout<<"转置后:"<<endl;
- a.reverlist();
- a.print();
- system("pause");
- }
【运行结果】
注:以上内容仅供参考,如有问题请指正。