成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

線性結(jié)構(gòu) 數(shù)組與鏈表

edagarli / 2844人閱讀

摘要:線性結(jié)構(gòu)數(shù)組與鏈表線性結(jié)構(gòu)線性數(shù)據(jù)結(jié)構(gòu)有兩端,有時(shí)被稱為左右,某些情況被稱為前后。將兩個線性數(shù)據(jù)結(jié)構(gòu)區(qū)分開的方法是添加和移除項(xiàng)的方式,特別是添加和移除項(xiàng)的位置。相對于數(shù)組,鏈表的好處在于,添加或移除元素的時(shí)候不需要移動其他元素。

線性結(jié)構(gòu) 數(shù)組與鏈表 線性結(jié)構(gòu)

線性數(shù)據(jù)結(jié)構(gòu)有兩端,有時(shí)被稱為左右,某些情況被稱為前后。你也可以稱為頂部和底部,名字都不重要。將兩個線性數(shù)據(jù)結(jié)構(gòu)區(qū)分開的方法是添加和移除項(xiàng)的方式,特別是添加和移除項(xiàng)的位置。例如一些結(jié)構(gòu)允許從一端添加項(xiàng),另一些允許從另一端移除項(xiàng)。

數(shù)組或列表

數(shù)組(Array)是編程界最常見的數(shù)據(jù)結(jié)構(gòu),有些編程語言被稱作位列表(List)。幾乎所有編程語言都原生內(nèi)置數(shù)組類型,只是形式向略有不同,因?yàn)閿?shù)組是最簡單的內(nèi)存數(shù)據(jù)結(jié)構(gòu)。

數(shù)組的定義是:一個存儲元素的線性集合(Collection),元素可以通過索引(Index)來任意存取,索引通常是數(shù)字,用來計(jì)算元素之間存儲位置的偏移量。

鏈表

數(shù)組的缺點(diǎn):要存儲多個元素,數(shù)組(或列表)可能是最常見的數(shù)據(jù)結(jié)構(gòu)。但是數(shù)組不總是組織數(shù)據(jù)的最佳結(jié)構(gòu)。在大多數(shù)編程語言中,數(shù)組的大小是固定的,所以當(dāng)數(shù)組被填滿時(shí),再要加入新的元素會非常困難。并且從數(shù)組起點(diǎn)或中間插入或移除元素的成本很高,因?yàn)樾枰獙?shù)組中的其他元素向前后平移。

鏈表(Linked list)中的元素在內(nèi)存中不是連續(xù)存放的。鏈表是由一組節(jié)點(diǎn)(Node)組成的集合,每個節(jié)點(diǎn)由元素本身和一個指向下一個元素的引用(也被稱作鏈接或指針)組成。相對于數(shù)組,鏈表的好處在于,添加或移除元素的時(shí)候不需要移動其他元素。

鏈表的種類

單向鏈表(Singly linked list):是最基本的鏈表,每個節(jié)點(diǎn)一個引用,指向下一個節(jié)點(diǎn)。單向鏈表的第一個節(jié)點(diǎn)稱為頭節(jié)點(diǎn)(head node),最后一個節(jié)點(diǎn)稱為尾節(jié)點(diǎn)(tail node),尾節(jié)點(diǎn)的引用為空(None),不指向下一個節(jié)點(diǎn)。

雙向鏈表(Doubly linked list)和單向鏈表的區(qū)別在于,在鏈表中的節(jié)點(diǎn)引用是雙向的,一個指向下一個元素,一個指向上一個元素。

循環(huán)鏈表(Circular linked list)和單向鏈表類似,節(jié)點(diǎn)類型都一樣。唯一的區(qū)別是 ,鏈表的尾節(jié)點(diǎn)引用指向頭節(jié)點(diǎn)。

雙向循環(huán)鏈表:類似于雙向鏈表,尾節(jié)點(diǎn)的后置引用指向頭節(jié)點(diǎn),頭節(jié)點(diǎn)的前置引用指向尾節(jié)點(diǎn)。

單向鏈表的操作
方法 操作
append 向鏈表尾部添加一個元素
insert 在鏈表的指定位置插入一個元素
pop 從鏈表特定位置刪除并返回元素
remove 從鏈表中刪除給定的元素
find 返回元素的索引
iter 迭代鏈表元素
size 獲取鏈表大小
clear 清空鏈表
Python實(shí)現(xiàn)單向鏈表
# python3
class Node:
    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next


class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.size = 0

    def append(self, value):
        node = Node(value)
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            self.tail.next = node
            self.tail = node
        self.size += 1

    def insert(self, index, value):
        if 0 <= index <= self.size:
            node = Node(value)
            current = self.head
            previous = Node(next=current)
            count = 0
            while count < index:
                previous = current
                current = current.next
                count += 1
            previous.next = node
            node.next = current
            if previous.value is None:
                self.head = node
            if node.next is None:
                self.tail = node
            self.size += 1
            return True
        else:
            return False

    def pop(self, index):
        if 0 <= index <= self.size and self.head is not None:
            current = self.head
            previous = Node(next=current)
            count = 0
            while count < index:
                previous = current
                current = current.next
                count += 1
            previous.next = current.next
            if previous.value is None:
                self.head = current.next
            if current.next is None:
                self.tail = previous
            self.size -= 1
            return current.value
        else:
            return None

    def remove(self, item):
        found = False
        current = self.head
        previous = Node(next=current)
        index = 0
        while not found and current is not None:
            if current.value == item:
                found = True
            else:
                previous = current
                current = current.next
            index += 1
        if found:
            previous.next = current.next
            if previous.value is None:
                self.head = current.next
            if current.next is None:
                self.tail = previous
            self.size -= 1
            return index
        else:
            return -1

    def find(self, item):
        current = self.head
        count = 0
        while current is not None:
            if current.value == item:
                return count
            else:
                current = current.next
                count += 1
        return -1
        
    def iter(self):
        current = self.head
        while current is not None:
            yield current.value
            current = current.next

    def size(self):
        return self.size

    def clear(self):
        self.head = None
        self.tail = None
        self.size = 0

    def is_empty(self):
        return self.size == 0
        
    def __len__(self):
        return self.size()

    def __iter__(self):
        iter self.iter()

    def __getitem__(self, index):
        return self.find(index)

    def __contains__(self, item):
        return self.find(item) != -1
JavaScript實(shí)現(xiàn)單向鏈表
// ES6
class Node {
    constructor(value=null, next=null) {
        this.value = value;
        this.next = next;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }
    append(value) {
        let node = new Node(value);
        if (this.head === null) {
            this.head = node;
            this.tail = node;
        } else {
            this.tail.next = temp;
            this.tail = temp;
        }
        this.size += 1;
    }
    insert(index, value) {
        if (0 <= index <= this.size) {
            let node = new Node(value);
            let current = this.head;
            let previous = new Node(next=current);
            let count = 0;
            while (count < index) {
                previous = current;
                current = current.next;
                count += 1;
            }
            previous.next = node
            node.next = current
            if (previous.value === null) {
                this.head = node;
            }
            if (node.next === null) {
                this.tail = node;
            }
            this.size += 1
            return true;
        } else {
            return false;
        }
    }
    pop(index) {
        if (0 <= index <= self.size && this.head === null) {
            let current = this.head;
            let previous = new Node(next=current);
            let count = 0;
            while (count < index) {
                previous = current;
                current = current.next;
                count += 1;
            }
            previous.next = current.next;
            if (previous.value === null) {
                this.head = current.next;
            }
            if (current.next === null) {
                this.tail = previous;
            }
            this.size -= 1;
            return current.value;
        } else {
            return null;
        }
    }
    remove(item) {
        let found = false;
        let current = this.head;
        let previous = new Node(next=current);
        let index = 0;
        while (! found && current !== null) {
            if (current.value === item) {
                found = true;
            } else {
                previous = current;
                current = current.next;
            }
            index += 1
        }
        if (found) {
            previous.next = current.next;
            if (previous.value === null) {
                this.head = current.next;
            }
            if (current.next === null) {
                this.tail = previous;
            }
            this.size -= 1;
            return index;
        } else {
            return -1;
        }
    }
    find(item) {
        let current = this.head;
        let count = 0;
        while (current !== null) {
            if (current.value === item) {
                return count;
            } else {
                current = current.next;
                count += 1;
            }
        }
        return -1;
    }
    iter() {
        let current = this.head;
        while (current !== null) {
            yield current.value;
            current = current.next;
        }
    }
    size() {
        return this.size;
    }
    clear() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }
    isEmpty() {
        return this.size === 0;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/98396.html

相關(guān)文章

  • 線性結(jié)構(gòu) 數(shù)組鏈表

    摘要:線性結(jié)構(gòu)數(shù)組與鏈表線性結(jié)構(gòu)線性數(shù)據(jù)結(jié)構(gòu)有兩端,有時(shí)被稱為左右,某些情況被稱為前后。將兩個線性數(shù)據(jù)結(jié)構(gòu)區(qū)分開的方法是添加和移除項(xiàng)的方式,特別是添加和移除項(xiàng)的位置。相對于數(shù)組,鏈表的好處在于,添加或移除元素的時(shí)候不需要移動其他元素。 線性結(jié)構(gòu) 數(shù)組與鏈表 線性結(jié)構(gòu) 線性數(shù)據(jù)結(jié)構(gòu)有兩端,有時(shí)被稱為左右,某些情況被稱為前后。你也可以稱為頂部和底部,名字都不重要。將兩個線性數(shù)據(jù)結(jié)構(gòu)區(qū)分開的方法...

    xi4oh4o 評論0 收藏0
  • 前言鏈表實(shí)現(xiàn)數(shù)組

    摘要:數(shù)據(jù)結(jié)構(gòu)可以分為列表線性樹形圖四種基本結(jié)構(gòu)。即承載數(shù)據(jù)的形式。數(shù)據(jù)結(jié)構(gòu)中的線性結(jié)構(gòu)有數(shù)組和鏈表,本文即對鏈表進(jìn)行簡單總結(jié),在后續(xù)文章中會實(shí)現(xiàn)幾種基本的數(shù)據(jù)結(jié)構(gòu)。 緣起 最近工作上需要依照現(xiàn)有數(shù)據(jù)生成嵌套json對象形式的組織機(jī)構(gòu)列表,一時(shí)覺得無從下手,請教同事大神才知道此乃數(shù)據(jù)結(jié)構(gòu)相關(guān)知識,遂惡補(bǔ)相關(guān)基礎(chǔ)并在此記錄。 數(shù)據(jù)結(jié)構(gòu)可以分為:1、列表;2、線性;3、樹形;4、圖 四種基本...

    ingood 評論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)與算法:二分查找

    摘要:為檢查長度為的列表,二分查找需要執(zhí)行次操作。最后需要指出的一點(diǎn)是高水平的讀者可研究一下二叉樹關(guān)于二叉樹,戳這里數(shù)據(jù)結(jié)構(gòu)與算法二叉樹算法常見練習(xí)在一個二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。 常見數(shù)據(jù)結(jié)構(gòu) 簡單數(shù)據(jù)結(jié)構(gòu)(必須理解和掌握) 有序數(shù)據(jù)結(jié)構(gòu):棧、隊(duì)列、鏈表。有序數(shù)據(jù)結(jié)構(gòu)省空間(儲存空間?。?無序數(shù)據(jù)結(jié)構(gòu):集合、字典、散列表,無序...

    zsirfs 評論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)與算法:二分查找

    摘要:為檢查長度為的列表,二分查找需要執(zhí)行次操作。最后需要指出的一點(diǎn)是高水平的讀者可研究一下二叉樹關(guān)于二叉樹,戳這里數(shù)據(jù)結(jié)構(gòu)與算法二叉樹算法常見練習(xí)在一個二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。 常見數(shù)據(jù)結(jié)構(gòu) 簡單數(shù)據(jù)結(jié)構(gòu)(必須理解和掌握) 有序數(shù)據(jù)結(jié)構(gòu):棧、隊(duì)列、鏈表。有序數(shù)據(jù)結(jié)構(gòu)省空間(儲存空間?。?無序數(shù)據(jù)結(jié)構(gòu):集合、字典、散列表,無序...

    you_De 評論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)與算法:二分查找

    摘要:為檢查長度為的列表,二分查找需要執(zhí)行次操作。最后需要指出的一點(diǎn)是高水平的讀者可研究一下二叉樹關(guān)于二叉樹,戳這里數(shù)據(jù)結(jié)構(gòu)與算法二叉樹算法常見練習(xí)在一個二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。 常見數(shù)據(jù)結(jié)構(gòu) 簡單數(shù)據(jù)結(jié)構(gòu)(必須理解和掌握) 有序數(shù)據(jù)結(jié)構(gòu):棧、隊(duì)列、鏈表。有序數(shù)據(jù)結(jié)構(gòu)省空間(儲存空間小) 無序數(shù)據(jù)結(jié)構(gòu):集合、字典、散列表,無序...

    gotham 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<