C++資料結構與演算法_1_線性表,資料結構與演算法_1

來源:互聯網
上載者:User

C++資料結構與演算法_1_線性表,資料結構與演算法_1
順序表的實現與分析引 --線性表的抽象基類:

template <typename T>class LinearList{public:    LinearList();    ~LinearList();    virtual int Size() const = 0;   //返回線性表所能夠儲存的最大長度    virtual int Length() const = 0; //當前線性表的長度    virtual int Search(T &x) const = 0;    virtual int Locate(int i) const = 0;    virtual bool getData(int i,T &x) const = 0;    virtual void setData(int i,T &x) = 0;    virtual bool Insert(int i,T &x) = 0;    virtual bool Remove(int i,T &x) = 0;    virtual bool IsFull() const = 0;    virtual bool IsEmpty() const = 0;    virtual void input() = 0;    virtual void output() const = 0;    virtual void Sort() = 0;    virtual LinearList<T> operator=(LinearList<T> &L) = 0;  //支援線性表的複製};

本文 --順序表的實現

對於順序表,其特點有如下公式:

Loc(i) = Loc(j) + (i-j) * sizeof(T)

類定義如下:

//註:對於某些不知含義的C++關鍵詞的含義以及用法,//請參考系列部落格:http://blog.csdn.net/column/details/zjf666.htmlconst int defaultSize = 10;template <class T>class SeqList : public LinearList<T>{public:    SeqList(int sz = defaultSize);    SeqList(SeqList<T> &L);    ~SeqList()    {        delete []data;    }    int Size() const    {        return maxSize;    }    int Length() const    {        return last + 1;    }    int Search(T &x) const;    int Locate(int i) const;    bool getData(int i,T &x)    {        if (i > 0 && i <= last+1)        {            x = data[i-1];            return true;        }        else        {            return false;        }    }    void setData(int i,T &x)    {        if (i > 0 && i <= last+1)        {            data[i-1] = x;        }    }    bool Insert(int i,T &x);    bool Remove(int i,T &x);    bool IsFull() const    {        return last == maxSize-1;    }    bool isEmpty() const    {        return last == -1;    }    void input();    void output();    void Sort();    SeqList<T> operator=(SeqList<T> &L);protected:    T *data;                    //數組    int maxSize;                //最大可容納量    int last;                   //當前已存元素的最後一個位置    void reSize(int newSize);   //改變data數組的空間大小};

 

--各個操作的實現與分析:

1) 建構函式與複製建構函式

template <typename T>SeqList<T>::SeqList(int sz)     //構造順序表{    if (sz > 0)    {        maxSize = sz;        last = -1;              //表的實際長度置空        data = new T[sz];        checkNew(data);         //檢查數組是否分配成功    }}template <typename T>SeqList<T>::SeqList(SeqList<T> &L){    maxSize = L.Size();    last = L.Length() -1;    data = new T[maxSize];    checkNew(data);    T value;    for (int i = 1; i <= L.Length(); ++i)    {        L.getData(i,value);         //將資料從原表中取出        data[i-1] = value;          //將資料插入到新的表中    }}

 

2) 私人操作:擴充順序表的的儲存數組空間大小

template <typename T>void SeqList<T>::reSize(int newSize){    if (newSize <= 0)   //無效的數組長度    {        cerr << "invalid size!" << endl;        exit(1);    }    if (newSize != maxSize)    {        T *newArr = new T[newSize]; //重新分配新的數組        checkNew(newArr);        int times = last + 1;       //獲得原數組長度        T *srcPtr = data;        T *destPtr = newArr;        while (times --)            //將原數組內容複寫到新的數組        {            *destPtr ++ = *srcPtr ++;        }        delete []data;              //不要忘記釋放原data記憶體!        data = newArr;        maxSize = newSize;    }}//在應用調用具有SeqList類型傳回值的函數時,都要使用複製建構函式,用以返回運算結果

 

3) 搜尋和定位操作

template <typename T>int SeqList<T>::Search(T &x) const{    for (int i = 0; i <= last ; ++i)    {        if (data[i] == x)        {            return i + 1;   //表項序號恰比它在數組中的實際存放的位置序號少1        }    }    return 0;}template <typename T>int SeqList<T>::Locate(int i) const{    if (i >= 1 && i <= last)        return i;    return 0;}

 

4) 順序表的插入與刪除操作

template <typename T>bool SeqList<T>::Insert(int i,T &x){    if (last + 1 == maxSize)    //表滿        return false;    if (i < 0 || i > last + 1)  //表項參數錯誤        return false;    for (int index = last; index >= i; -- index)    //依次後移,空出i號位置    {        data[index] = data[index - 1];    }    data[i] = x;    ++ last;            //表長+1[勿忘]    return true;}template <typename T>bool SeqList<T>::Remove(int i,T &x){    if (last == -1)                 //表空        return false;    if (i < 1 || i > last + 1)      //表項參數錯誤        return false;    x = data[i - 1];    for (int index = i; index <= last ; ++index)    {        data[index - 1] = data[index];  //迴圈前移,將i-1位置填充    }    -- last;    //修改表長    return true;}

 

5) 輸入輸出操作

template <class T>void SeqList<T>::input(){    while (1)    {        cin >> last;    //輸入數組最後一個元素所在位置        if (last <= maxSize - 1)            break;        cout << "The value of last is error,please try again!" << endl;    }    for (int i = 0; i <= last; ++i)    {        cout << i + 1 << ": ";        cin >> data[i];    }}template <class T>void SeqList<T>::output() const{    for (int i = 0; i <= last; ++i)    {        cout << "#" << i + 1 << ": " << data[i] << endl;    }}

 

6) 賦值操作

template <class T>SeqList<T> &SeqList<T>::operator=(const SeqList<T> &L){    if (maxSize < L.Length())    {        reSize(L.Length() * 2);    }    maxSize = L.Length() * 2;    last = L.Length() - 1;    T value;    for (int i = 1;i <= last + 1; ++i)    {        L.getData(i,value);        data[i - 1] = value;    }    return * this;}

--順序表的效能分析

順序表的所有操作的實現中,最複雜、最耗時的就是搜尋、插入和刪除運算的實現。搜尋的時間代價主要看重搜尋所採用的方法,是採用順序搜尋,還是二叉搜尋等,而順序表的插入和刪除的時間代價主要是看迴圈內的資料移動次數。


資料結構演算法21兩個線性表LA與LB,新的集合那個,C語言版完整程式怎寫

我寫的,你編譯一下,改一改就行了。

void Union(LinkList *L1,LinkList *L2,LinkList *&L3)//交集
{
LinkList *p=L1->next,*q=L2->next,*s,*c;
L3=(LinkList *)malloc(sizeof(LinkList));
L3->next=NULL;
c=L3;
while(p!=NULL&&q!=NULL)
{ if(p->data<q->data)
{ s=(LinkList *)malloc(sizeof(LinkList));//複製結點
s->data=p->data;
c->next=s;c=s;
p=p->next;
}
else if(p->data>q->data)
{ s=(LinkList *)malloc(sizeof(LinkList));
s->data=q->data;
c->next=s;c=s;
q=q->next;
}
else
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=p->data;
c->next=s;c=s;
p=p->next;
q=q->next;
}
}

while(q!=NULL)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=q->data;
c->next=s;c=s;
q=q->next;
}
c->next=NULL;

while(p!=NULL)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=p->data;
c->next=s;c=s;
p=p->next;
}
c->next=NULL;

}

 
資料結構演算法實現:利用兩個線性表LA與LB分別表示兩個集合A與B,現要一個新的集合A=A並B

我寫的,你編譯一下,改一改就行了。

void Union(LinkList *L1,LinkList *L2,LinkList *&L3)//交集
{
LinkList *p=L1->next,*q=L2->next,*s,*c;
L3=(LinkList *)malloc(sizeof(LinkList));
L3->next=NULL;
c=L3;
while(p!=NULL&&q!=NULL)
{ if(p->data<q->data)
{ s=(LinkList *)malloc(sizeof(LinkList));//複製結點
s->data=p->data;
c->next=s;c=s;
p=p->next;
}
else if(p->data>q->data)
{ s=(LinkList *)malloc(sizeof(LinkList));
s->data=q->data;
c->next=s;c=s;
q=q->next;
}
else
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=p->data;
c->next=s;c=s;
p=p->next;
q=q->next;
}
}

while(q!=NULL)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=q->data;
c->next=s;c=s;
q=q->next;
}
c->next=NULL;

while(p!=NULL)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=p->data;
c->next=s;c=s;
p=p->next;
}
c->next=NULL;

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.