實驗內容
[問題描述]
基本線性表就地逆置是指在基本線性表現有空間的基礎上,將基本線性表中的數據元素交換位置排列,排列完之後,新的順序序列與原來的順序序列剛好相反。如原來順序序列“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");
- }
【運行結果】
注:以上內容僅供參考,如有問題請指正。