线性表的顺序存储及操作实现

来源:互联网 发布:钢铁侠网络寄生虫主机 编辑:程序博客网 时间:2024/05/01 03:06

1.数组中的每个位置称为单元(cell)或结点(node)。

2.loc(ai)=loc(a1)+(i-1)d (1<=i<=n)

3.顺序表具有按数据元素序号随机存取的特点。

4.下面的代码给出了相应的C++类定义,建立抽象类AbstractList的目的是增加软件的重用。

定义的模板类中,用户指定元素的数据类型为T。

Insert和Delete均返回一个线性表的引用。

具体实现时首先会修改表*this,然后返回一个引用(指向修改后的表)。

因此,同时组合多个表的操作是可行的,如X.Insert(0,a).Delete(3,b)。

//文件abslist.h:类AbstractList#ifndef AbstractList_#define AbstractList_template<class T>class AbstractList {public:    virtual bool IsEmpty() const=0;    virtual int Length() const=0;    virtual bool Find(int k,T& x) const=0;    virtual int Search(const T& x) const=0;    virtual AbstractList<T>& Delete(int k,T& x)=0;    virtual AbstractList<T>& Insert(int k,const T& x)=0;    virtual void Output(ostream& out) const=0;};#endif

//文件llist.h: 类LinearList#ifndef LinearList_#define LinearList_#include<cstdlib>#include<iostream>#include"abslist.h"template<class T>class LinearList : AbstractList<T>{public:    LinearList(int MaxListSize=10);    ~LinearList(){delete[]element;}    bool IsEmpty() const {return length==0;}    int Length() const {return length;}    bool Find(int k,T& x) const;    int Search(const T& x) const;    AbstractList<T>& Delete(int k,T& x);    AbstractList<T>& Insert(int k,const T& x);    void Output(ostream& out) const;private:    int length;    int MaxSize;    T *element;};//......#endif // LinearList_

顺序表上基本操作的实现

1.初始化

template<class T>LinearList<T>::LinearList(int MaxListSize){    MaxSize=MaxListSize;    element=new T[MaxSize];    length=0;}
2.查找

顺序表是一个随机存取的数据结构

(1)按序号查找

//把第k个元素取至x中template<class T>bool LinearList<T>::Find(int k,T& x) const{    //如果不存在第k个元素,则返回false,否则返回true    if(k<1||k>length)         return false;    x=element[k-1];    return true;}//时间复杂度O(1)

(2)按值查找

template<class T>int LinearList<T>::Search(const T& x) const{    for(int i=0;i<length;i++)    {        if(element[i]==x) return ++i;        return 0;    }}//时间复杂度O(n)
3.插入(?)

template <class T>LinearList<T>& LinearList<T>::Insert(int k,const T& x){    //在第k个位置插入x,如果不存在第k个位置,则引发异常OutOfBounds    if(k<1||k>length) throw OutOfBounds();    //如果表已满,则引发异常NoMem    if(length==MaxSize) throw NoMem();    for(int i=length-1;i>=k-1;i++)        element[i+1]=element[i];    element[k-1]=x;    length++;    return *this;}//时间复杂度O(n)

4.删除

template<class T>LinearList<T>& LinearList<T>::Delete(int k,T& x){    //如果不存在第k个元素,则引发异常OutOfBounds    if(k<1||k>length) throw OutOfBounds();//length等于表长n;     //包括了对表空(length=0)的检查。    x=element[k-1];    for(int i=k;i<length;i++)    {        element[i-1]=element[i];    }    length--;    return *this;}//时间复杂度O(n)


5.顺序表的应用举例

比较两个线性表的大小

template<class T> int compare(LinearList<T> A,LinearList<T> B,int m,int n){    LinearList<T> AS,BS;    //AS,BS分别为A和B中除去最大公共前缀后的子表    int i=0,j,ms=0,ns=0;    while(A.element[i]==B.element[i]) i++;    for(j=i;j<n;j++)    {        AS.element[j-i]=A.element[j];        ms++;    }    for(j=i;j<n;j++)    {        BS.element[j-i]=B.element[j];        ns++;    }    if(ms==ns&&ms==0) return 0;//A=B;    else if(ms==0&&ns>0||ms>0&&ns>0&&AS.element[0]<BS.element[0])        return -1;//A<B;    else return 1;//A>B;}//时间复杂度O(m+n)




0 0
原创粉丝点击