成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

基本線性數(shù)據(jù)結(jié)構(gòu)的Python實現(xiàn)

alanoddsoff / 3516人閱讀

摘要:本篇主要實現(xiàn)四種數(shù)據(jù)結(jié)構(gòu),分別是數(shù)組堆棧隊列鏈表。后面的一些結(jié)構(gòu)也將用來實現(xiàn)。由于堆疊數(shù)據(jù)結(jié)構(gòu)只允許在一端進(jìn)行操作,因而按照后進(jìn)先出的原理運作。

本篇主要實現(xiàn)四種數(shù)據(jù)結(jié)構(gòu),分別是數(shù)組、堆棧、隊列、鏈表。我不知道我為什么要用Python來干C干的事情,總之Python就是可以干。

所有概念性內(nèi)容可以在參考資料中找到出處

數(shù)組 數(shù)組的設(shè)計

數(shù)組設(shè)計之初是在形式上依賴內(nèi)存分配而成的,所以必須在使用前預(yù)先請求空間。這使得數(shù)組有以下特性:

請求空間以后大小固定,不能再改變(數(shù)據(jù)溢出問題);

在內(nèi)存中有空間連續(xù)性的表現(xiàn),中間不會存在其他程序需要調(diào)用的數(shù)據(jù),為此數(shù)組的專用內(nèi)存空間;

在舊式編程語言中(如有中階語言之稱的C),程序不會對數(shù)組的操作做下界判斷,也就有潛在的越界操作的風(fēng)險(比如會把數(shù)據(jù)寫在運行中程序需要調(diào)用的核心部分的內(nèi)存上)。

因為簡單數(shù)組強(qiáng)烈倚賴電腦硬件之內(nèi)存,所以不適用于現(xiàn)代的程序設(shè)計。欲使用可變大小、硬件無關(guān)性的數(shù)據(jù)類型,Java等程序設(shè)計語言均提供了更高級的數(shù)據(jù)結(jié)構(gòu):ArrayList、Vector等動態(tài)數(shù)組。

Python的數(shù)組

從嚴(yán)格意義上來說:Python里沒有嚴(yán)格意義上的數(shù)組。
List可以說是Python里的數(shù)組,下面這段代碼是CPython的實現(xiàn)List的結(jié)構(gòu)體:

typedef struct {
    PyObject_VAR_HEAD
    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */
    PyObject **ob_item;

    /* ob_item contains space for "allocated" elements.  The number
     * currently in use is ob_size.
     * Invariants:
     *     0 <= ob_size <= allocated
     *     len(list) == ob_size
     *     ob_item == NULL implies ob_size == allocated == 0
     * list.sort() temporarily sets allocated to -1 to detect mutations.
     *
     * Items must normally not be NULL, except during construction when
     * the list is not yet visible outside the function that builds it.
     */
    Py_ssize_t allocated;
} PyListObject;

取自CPython-Github

還有一篇文章講List實現(xiàn),感興趣的朋友可以去看看。中文版。

當(dāng)然,在Python里它就是數(shù)組。
后面的一些結(jié)構(gòu)也將用List來實現(xiàn)。

堆棧 什么是堆棧

堆棧(英語:stack),也可直接稱棧,在計算機(jī)科學(xué)中,是一種特殊的串列形式的數(shù)據(jù)結(jié)構(gòu),它的特殊之處在于只能允許在鏈接串列或陣列的一端(稱為堆疊頂端指標(biāo),英語:top)進(jìn)行加入資料(英語:push)和輸出資料(英語:pop)的運算。另外堆疊也可以用一維陣列或連結(jié)串列的形式來完成。堆疊的另外一個相對的操作方式稱為佇列。

由于堆疊數(shù)據(jù)結(jié)構(gòu)只允許在一端進(jìn)行操作,因而按照后進(jìn)先出(LIFO, Last In First Out)的原理運作。

特點

先入后出,后入先出。

除頭尾節(jié)點之外,每個元素有一個前驅(qū),一個后繼。

操作

從原理可知,對堆棧(棧)可以進(jìn)行的操作有:

top():獲取堆棧頂端對象

push():向棧里添加一個對象

pop():從棧里推出一個對象

實現(xiàn)
class my_stack(object):
    def __init__(self, value):
        self.value = value
        # 前驅(qū)
        self.before = None
        # 后繼
        self.behind = None

    def __str__(self):
        return str(self.value)


def top(stack):
    if isinstance(stack, my_stack):
        if stack.behind is not None:
            return top(stack.behind)
        else:
            return stack


def push(stack, ele):
    push_ele = my_stack(ele)
    if isinstance(stack, my_stack):
      stack_top = top(stack)
      push_ele.before = stack_top
      push_ele.before.behind = push_ele
    else:
      raise Exception("不要亂扔?xùn)|西進(jìn)來好么")


def pop(stack):
    if isinstance(stack, my_stack):
        stack_top = top(stack)
        if stack_top.before is not None:
            stack_top.before.behind = None
            stack_top.behind = None
            return stack_top
        else:
            print("已經(jīng)是棧頂了")
隊列 什么是隊列

和堆棧類似,唯一的區(qū)別是隊列只能在隊頭進(jìn)行出隊操作,所以隊列是是先進(jìn)先出(FIFO, First-In-First-Out)的線性表

特點

先入先出,后入后出

除尾節(jié)點外,每個節(jié)點有一個后繼

(可選)除頭節(jié)點外,每個節(jié)點有一個前驅(qū)

操作

push():入隊

pop():出隊

實現(xiàn) 普通隊列
class MyQueue():
    def __init__(self, value=None):
        self.value = value
        # 前驅(qū)
        # self.before = None
        # 后繼
        self.behind = None

    def __str__(self):
        if self.value is not None:
            return str(self.value)
        else:
            return "None"


def create_queue():
    """僅有隊頭"""
    return MyQueue()


def last(queue):
    if isinstance(queue, MyQueue):
        if queue.behind is not None:
            return last(queue.behind)
        else:
            return queue


def push(queue, ele):
    if isinstance(queue, MyQueue):
        last_queue = last(queue)
        new_queue = MyQueue(ele)
        last_queue.behind = new_queue


def pop(queue):
    if queue.behind is not None:
        get_queue = queue.behind
        queue.behind = queue.behind.behind
        return get_queue
    else:
        print("隊列里已經(jīng)沒有元素了")

def print_queue(queue):
    print(queue)
    if queue.behind is not None:
        print_queue(queue.behind)
鏈表 什么是鏈表

鏈表(Linked list)是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表,但是并不會按線性的順序存儲數(shù)據(jù),而是在每一個節(jié)點里存到下一個節(jié)點的指針(Pointer)。由于不必須按順序存儲,鏈表在插入的時候可以達(dá)到O(1)的復(fù)雜度,比另一種線性表順序表快得多,但是查找一個節(jié)點或者訪問特定編號的節(jié)點則需要O(n)的時間,而順序表相應(yīng)的時間復(fù)雜度分別是O(logn)和O(1)。

特點

使用鏈表結(jié)構(gòu)可以克服數(shù)組鏈表需要預(yù)先知道數(shù)據(jù)大小的缺點,鏈表結(jié)構(gòu)可以充分利用計算機(jī)內(nèi)存空間,實現(xiàn)靈活的內(nèi)存動態(tài)管理。但是鏈表失去了數(shù)組隨機(jī)讀取的優(yōu)點,同時鏈表由于增加了結(jié)點的指針域,空間開銷比較大。

操作

init():初始化

insert(): 插入

trave(): 遍歷

delete(): 刪除

find(): 查找

實現(xiàn)

此處僅實現(xiàn)雙向列表

class LinkedList():
    def __init__(self, value=None):
        self.value = value
        # 前驅(qū)
        self.before = None
        # 后繼
        self.behind = None

    def __str__(self):
        if self.value is not None:
            return str(self.value)
        else:
            return "None"


def init():
    return LinkedList("HEAD")


def delete(linked_list):
    if isinstance(linked_list, LinkedList):
        if linked_list.behind is not None:
            delete(linked_list.behind)
            linked_list.behind = None
            linked_list.before = None
        linked_list.value = None


def insert(linked_list, index, node):
    node = LinkedList(node)
    if isinstance(linked_list, LinkedList):
        i = 0
        while linked_list.behind is not None:
            if i == index:
                break
            i += 1
            linked_list = linked_list.behind
        if linked_list.behind is not None:
            node.behind = linked_list.behind
            linked_list.behind.before = node
        node.before, linked_list.behind = linked_list, node


def remove(linked_list, index):
    if isinstance(linked_list, LinkedList):
        i = 0
        while linked_list.behind is not None:
            if i == index:
                break
            i += 1
            linked_list = linked_list.behind
        if linked_list.behind is not None:
            linked_list.behind.before = linked_list.before
        if linked_list.before is not None:
            linked_list.before.behind = linked_list.behind
        linked_list.behind = None
        linked_list.before = None
        linked_list.value = None


def trave(linked_list):
    if isinstance(linked_list, LinkedList):
        print(linked_list)
        if linked_list.behind is not None:
            trave(linked_list.behind)


def find(linked_list, index):
    if isinstance(linked_list, LinkedList):
        i = 0
        while linked_list.behind is not None:
            if i == index:
                return linked_list
            i += 1
            linked_list = linked_list.behind
        else:
            if i < index:
                raise Exception(404)
            return linked_list

以上所有源代碼均在Github共享,歡迎提出issue或PR,希望與大家共同進(jìn)步!


參考資料

Wiki百科: 數(shù)據(jù)結(jié)構(gòu)、數(shù)組、隊列、鏈表


EOF

轉(zhuǎn)載請注明出處:https://zhuanlan.zhihu.com/p/...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/38135.html

相關(guān)文章

  • 數(shù)據(jù)結(jié)構(gòu)線性

    摘要:線性表是最基本的數(shù)據(jù)結(jié)構(gòu)之一,在實際程序中應(yīng)用非常廣泛,它還經(jīng)常被用作更復(fù)雜的數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)基礎(chǔ)。鏈表之單鏈表線性表的定義,它是一些元素的序列,維持著元素之間的一種線性關(guān)系。 線性表學(xué)習(xí)筆記,python語言描述-2019-1-14 線性表簡介 在程序中,經(jīng)常需要將一組(通常是同為某個類型的)數(shù)據(jù)元素作為整體管理和使用,需要創(chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含...

    leoperfect 評論0 收藏0
  • LSTM分類相關(guān)

    摘要:而檢驗?zāi)P陀玫降脑牧希ㄑυ评蠋熖峁┑拿膳ED痰脑u論,以及從網(wǎng)絡(luò)購買的某款手機(jī)的評論數(shù)據(jù)見附件。不同行業(yè)某些詞語的詞頻會有比較大的差別,而這些詞有可能是情感分類的關(guān)鍵詞之一。這是由于文本情感分類的本質(zhì)復(fù)雜性所致的。 文本情感分類--傳統(tǒng)模型(轉(zhuǎn)) showImg(https://segmentfault.com/img/bVKjWF?w=2192&h=534); 傳統(tǒng)的基于情感詞典...

    MartinHan 評論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)線性表:Python語言描述

    摘要:線性表的和采用了順序表的實現(xiàn)技術(shù),具有順序表的所有性質(zhì)。刪除鏈表應(yīng)丟棄這個鏈表里的所有結(jié)點。在語言中,就是檢查相應(yīng)變量的值是否為。也就是說,插入新元素的操作是通過修改鏈接,接入新結(jié)點,從而改變表結(jié)構(gòu)的方式實現(xiàn)的。 1.線性表 Python的list和tuple采用了順序表的實現(xiàn)技術(shù),具有順序表的所有性質(zhì)。 2.鏈接表 單向鏈接表 的結(jié)點是一個二元組。 其表元素域elem保存著作為表元素...

    wua_wua2012 評論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)與算法Python實現(xiàn)(二)——線性表之順序表

    摘要:文章首發(fā)于公眾號一件風(fēng)衣在編程中,我們常使用一組有順序的數(shù)據(jù)來表示某個有意義的數(shù)據(jù),這種一組元素的序列的抽象,就是線性表,簡稱表,是很多復(fù)雜數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)基礎(chǔ),在中,和就可以看作是線性表的實現(xiàn)。 文章首發(fā)于公眾號一件風(fēng)衣(ID:yijianfengyi) 在編程中,我們常使用一組有順序的數(shù)據(jù)來表示某個有意義的數(shù)據(jù),這種一組元素的序列的抽象,就是線性表,簡稱表,是很多復(fù)雜數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)基...

    TerryCai 評論0 收藏0
  • 第7期 Datawhale 組隊學(xué)習(xí)計劃

    馬上就要開始啦這次共組織15個組隊學(xué)習(xí) 涵蓋了AI領(lǐng)域從理論知識到動手實踐的內(nèi)容 按照下面給出的最完備學(xué)習(xí)路線分類 難度系數(shù)分為低、中、高三檔 可以按照需要參加 - 學(xué)習(xí)路線 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...

    dinfer 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<