实验内容
[问题描述]
基本线性表经常进行的运算操作有创建基本线性表、求基本线性表的长度、在基本线性表中查找某个数据元素、在某个位置插入一个新数据元素、在某个线性表中删除某个数据元素以及基本线性表的输出等操作。试编程实现基本线性表的这些基本运算。
[基本要求]
实现基本线性表的基本运算可以采用链式存储方式实现,也可以采用顺序存储的方式实现,在此给出这两种存储方式的实现方法,学生可任选其一进行具体实现。
【代码】
- #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;
- };
- 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";
- }
- int main()
- {
- AList<int> a;
- int b;
- cout<<"测试append函数:"<<endl;
- a.append(2);
- a.append(5);
- a.append(12);
- a.append(10);
- a.append(22);
- a.append(30);
- cout<<"append后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试insert函数:"<<endl;
- a.insert(11);
- a.insert(23);
- cout<<"insert后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试remove函数:"<<endl;
- a.remove(b);
- cout<<"remove后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试setEnd函数:"<<endl;
- a.setEnd();
- cout<<"setEnd后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试setStart函数:"<<endl;
- a.setStart();
- cout<<"setStart后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试next函数:" <<endl;
- a.next();
- a.next();
- cout<<"next后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试prev函数:"<<endl;
- a.prev();
- cout<<"prev后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试leftLength函数:"<<endl;
- cout<<"leftLength = "<<a.leftLength()<<endl;
- cout<<"--------------------------------------"<<endl;
- cout<<"测试rightLength函数:"<<endl;
- cout<<"rightLength = "<<a.rightLength()<<endl;
- cout<<"--------------------------------------"<<endl;
- cout<<"测试setPos函数:"<<endl;
- a.setPos(4);
- cout<<"setPos后:"<<endl;
- a.print();
- cout<<"--------------------------------------"<<endl;
- cout<<"测试getValues函数:"<<endl;
- a.getValues(b);
- cout<<"获得的值为:"<<b<<endl;
- cout<<"--------------------------------------"<<endl;
- system("pause");
- }
【运行结果】
注:以上内容仅供参考,如有问题请指正。
转载于:https://blog.51cto.com/sincerecorner/689237