大師兄的數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記(一):線性結(jié)構(gòu)

大師兄的數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記(二):線性結(jié)構(gòu)

一沟启、 線性表

1. 什么是線性表
  • 線性表是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列勋拟。
  • 表中元素個(gè)數(shù)成為線性表的長(zhǎng)度草慧。
  • 長(zhǎng)度為0時(shí),成為空表
  • 表的起始位置稱表頭舱禽,結(jié)束位置稱表尾
2. 線性表的抽象數(shù)據(jù)類型描述
  • 類型名稱:線性表(list)
  • 數(shù)據(jù)對(duì)象集: 線性表是n(\geq0)個(gè)元素構(gòu)成的有序序列(a_1,a_2...a_n)
  • 操作集:線性表L\in List,整數(shù)i表示位置恩沽,元素 X\in ElementType
  • 主要操作有:
操作 含義
SeqList() 無參數(shù)構(gòu)造方法
SeqList(DataType a[], int n) 有參數(shù)構(gòu)造方法
~SeqList() {} 析構(gòu)函數(shù)
int Length() 獲取長(zhǎng)度
DataType Find(int i) 查找值
int Locate(DataType x) 查找位置
void Insert(int DataType x) 插入元素
DataType Delete(int i) 刪除元素
void PrintList() 遍歷線性表
3. 線性表存儲(chǔ)方式
3.1 順序存儲(chǔ)
  • 利用數(shù)組的連續(xù)存儲(chǔ)空間誊稚,順序存放線性表的各元素。


  • 優(yōu)點(diǎn):
序號(hào) 優(yōu)點(diǎn)
1 隨機(jī)訪問特性,查找O(1)時(shí)間里伯,存儲(chǔ)密度高城瞎。
2 邏輯上相鄰的元素,物理上也相鄰疾瓮。
3 無須為表中元素之間的邏輯關(guān)系而增加額外的存儲(chǔ)空間脖镀。
  • 缺點(diǎn):
序號(hào) 缺點(diǎn)
1 插入和刪除需移動(dòng)大量元素。
2 當(dāng)線性表長(zhǎng)度變化較大時(shí)狼电,難以確定存儲(chǔ)空間的容量蜒灰。
3 造成存儲(chǔ)空間的“碎片”。
  • 代碼實(shí)現(xiàn):
#ifndef SEQLIST_H
#define SEQLIST_H

const int MaxSize = 100;
template <class DataType>
class SeqList
{
public:
    SeqList() { length = 0; }       //無參數(shù)構(gòu)造方法
    SeqList(DataType a[], int n);   //有參數(shù)構(gòu)造方法
    ~SeqList() {};                  //析構(gòu)函數(shù)
    int Length() { return length; } //獲取長(zhǎng)度
    DataType Find(int i);           //查找值
    int Locate(DataType x);         //查找位置
    void Insert(int i, DataType x); //插入元素
    DataType Delete(int i);         //刪除元素
    void PrintList();               //遍歷線性表
private:
    DataType data[MaxSize];
    int length;
};

#endif
#include "seqlist.h"
#include <iostream>

using namespace std;

template <class DataType>
SeqList<DataType>::SeqList(DataType a[], int n)
{
    //有參數(shù)構(gòu)造方法
    if (n > MaxSize) throw "Parameter Over MaxSize";
    for (int i = 0; i < n; i++)
        data[i] = a[i];
    length = n;
}

template <class DataType>
DataType SeqList<DataType>::Find(int i)
{
    //按位查找
    if (i<1 && i>length) throw "Location Over Size";
    else return data[i - 1];
}

template <class DataType>
int SeqList<DataType>::Locate(DataType x)
{
    //按值查找
    for (int i = 0; i < length; i++)
        if (data[i] == x) return i + 1;
    return 0;
}

template <class DataType>
void SeqList<DataType>::Insert(int i, DataType x)
{
    //插入元素
    if (length >= MaxSize) throw "Overflow";
    if (i<1 || i>length + 1) throw "Location";
    for (int j = length + 1; j > i; j--)
        data[j] = data[j - 1];
    data[i - 1] = x;
    length++;
}

template <class DataType>
DataType SeqList<DataType>::Delete(int i)
{
    //刪除元素
    int x;
    if (length == 0) throw "Underflow";
    if (i<1 || i>length) throw "Location";
    x = data[i - 1];
    for (int j = i; j < length; j++)
        data[j - 1] = data[j];
    length--;
    return x;
}

template <class DataType>
void SeqList<DataType>::PrintList()
{
    //遍歷線性表
    for (int i = 0; i < length; i++)
        cout << data[i] << endl;
}
3.2 鏈?zhǔn)酱鎯?chǔ)
  • 不要求邏輯上相鄰的兩個(gè)元素物理上也相鄰肩碟。
  • 通過"鏈"建立起數(shù)據(jù)元素之間的邏輯關(guān)系强窖。
  • 插入刪除不需要移動(dòng)數(shù)據(jù)元素,只需要修改“鏈”削祈。
  • 優(yōu)點(diǎn):
序號(hào) 優(yōu)點(diǎn)
1 插入翅溺、刪除不需移動(dòng)其他元素,只需改變指針髓抑。
2 鏈表各個(gè)節(jié)點(diǎn)在內(nèi)存中空間不要求連續(xù)未巫,空間利用率高。
  • 缺點(diǎn):
序號(hào) 缺點(diǎn)
1 查找需要遍歷操作启昧,比較麻煩叙凡。
  • 代碼實(shí)現(xiàn):
#ifndef SEQCHAIN_H
#define SEQCHAIN_H

template<class DataType>
struct Node
{
    //存儲(chǔ)結(jié)構(gòu)
    DataType data; //數(shù)據(jù)
    Node<DataType>* next; //下一個(gè)節(jié)點(diǎn)的地址
};

template<class DataType>
class SeqList
{
public:
    SeqList();                      //無參數(shù)構(gòu)造方法
    SeqList(DataType a[], int n);   //有參數(shù)構(gòu)造方法
    ~SeqList();                 //析構(gòu)函數(shù)
    int Length();                   //獲取長(zhǎng)度
    DataType Find(int i);           //查找值
    int Locate(DataType x);         //查找位置
    void Insert(int i, DataType x); //插入元素
    DataType Delete(int i);         //刪除元素
    void PrintList();               //遍歷線性表
private:
    Node<DataType>* first;
};

#endif
#include "seqlist.h"
#include <iostream>

using namespace std;

template<class DataType>
SeqList<DataType>::SeqList()
{
    //無參數(shù)構(gòu)造方法
    first = new Node<DataType>;
    first->next = nullptr;
}

template<class DataType>
SeqList<DataType>::SeqList(DataType a[], int n)
{
    //有參數(shù)構(gòu)造方法
    first = new Node<DataType>;
    first->next = nullptr;
    for (int i = 0; i < n; i++)
    {
        Node<DataType>* s = new Node<DataType>;
        s->data = a[i];
        s->next = first->next;
        first->next = s;
    }
}

template<class DataType>
SeqList<DataType>::~SeqList()
{
    //析構(gòu)函數(shù)
    while (first != nullptr)
    {
        Node<DataType>* q = first;
        first = first->next;
        delete q;
    }

}

template<class DataType>
int SeqList<DataType>::Length()
{
    //獲取長(zhǎng)度
    Node<DataType>* p = first->next;
    int count = 0;
    while(p != nullptr)
    {
        p = p->next;
        count++;
    }
    return count;
}

template<class DataType>
DataType SeqList<DataType>::Find(int i)
{
    //查找值
    Node<DataType>* p = first->next;
    int count = 1;
    while (p != nullptr && count < i)
    {
        p = p->next;
        count++;
    }
    if (p == nullptr) throw "Location";
    else return p->data;
}

template<class DataType>
int SeqList<DataType>::Locate(DataType x)
{
    //查找位置
    Node<DataType>* p = first->next;
    int count = 1;
    while (p != nullptr)
    {
        if (p->data == x) return count;
        count++;
    }
    return 0;
}

template<class DataType>
void SeqList<DataType>::Insert(int i, DataType x)
{
    //插入元素
    Node<DataType>* p = first;
    int count = 0;
    while (p != nullptr && count < i - 1)
    {
        p = p->next;
        count++;
    }
    if (p == nullptr) throw "Location";
    else {
        Node<DataType>* s = new Node<DataType>;
        s->data = x;
        s->next = p->next;
        p->next = s;
    }
}

template<class DataType>
DataType SeqList<DataType>::Delete(int i)
{
    //刪除元素
    Node<DataType>* p = first;
    int count = 0;
    while (p != nullptr && count < i - 1)
    {
        p = p->next;
        count++;
    }
    if (p == nullptr || p->next == nullptr) throw "Location";
    else {
        Node<DataType>* q = p->next;
        int x = q->data;
        p->next = q->next;
        return x;
    }
}

template<class DataType>
void SeqList<DataType>::PrintList()
{
    //遍歷線性表
    Node<DataType>* p = first->next;
    while (p != nullptr)
    {
        cout << p->data << endl;
        p = p->next;
    }
}
4. 其他線性表
4.1 廣義表
  • 是線性表的推廣。
  • 在線性表中密末,元素是基本的原元素握爷,而在廣義表中,元素不僅可以是元素严里,也可以是另一個(gè)廣義表新啼。


  • 實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu):
enum  elem_tag
{
    DATA, LIST
};

class GLnode
{
private:
    elem_tag Tag; // 0:元素節(jié)點(diǎn) 1:子表
    union
    {
        char data; // 元素節(jié)點(diǎn)的值域
        struct // 表節(jié)點(diǎn)
        {
            GLnode* hp;
            GLnode* tp;
        }ptr;
    };
};
4.2 多重鏈表
  • 鏈表中的節(jié)點(diǎn)可能同時(shí)隸屬于多個(gè)鏈。


  • 多重鏈表中節(jié)點(diǎn)的指針域會(huì)有多個(gè), 但包含兩個(gè)指針域的鏈表并不一定是多重鏈表刹碾。
  • 經(jīng)常用于樹燥撞、圖等復(fù)雜數(shù)據(jù)結(jié)構(gòu)。
  • 以實(shí)現(xiàn)雙鏈表存儲(chǔ)方法結(jié)構(gòu)為例:
template<class DataType>
struct Node
{
    DataType data;
    Node<DataType>* prior, * next;
};

參考資料



本文作者:大師兄(superkmi)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末迷帜,一起剝皮案震驚了整個(gè)濱河市物舒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌戏锹,老刑警劉巖冠胯,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異锦针,居然都是意外死亡荠察,警方通過查閱死者的電腦和手機(jī)置蜀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悉盆,“玉大人盯荤,你說我怎么就攤上這事』烂耍” “怎么了秋秤?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)京髓。 經(jīng)常有香客問我航缀,道長(zhǎng)商架,這世上最難降的妖魔是什么堰怨? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮蛇摸,結(jié)果婚禮上备图,老公的妹妹穿的比我還像新娘。我一直安慰自己赶袄,他們只是感情好揽涮,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著饿肺,像睡著了一般蒋困。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上敬辣,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天雪标,我揣著相機(jī)與錄音,去河邊找鬼溉跃。 笑死村刨,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的撰茎。 我是一名探鬼主播嵌牺,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼龄糊!你這毒婦竟也來了逆粹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤炫惩,失蹤者是張志新(化名)和其女友劉穎枯饿,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诡必,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡奢方,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年搔扁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蟋字。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡稿蹲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鹊奖,到底是詐尸還是另有隱情苛聘,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布忠聚,位于F島的核電站设哗,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏两蟀。R本人自食惡果不足惜网梢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望赂毯。 院中可真熱鬧战虏,春花似錦、人聲如沸党涕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽膛堤。三九已至手趣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肥荔,已是汗流浹背绿渣。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留次企,地道東北人怯晕。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像缸棵,于是被迫代替她去往敵國(guó)和親舟茶。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355