PYTHON資料結構與演算法__JAVA

來源:互聯網
上載者:User
資料結構是電腦儲存、組織資料的方式。資料結構是指相互之間存在一種或多種特定關係的資料元素的集合。通常情況下,精心選擇的資料結構可以帶來更高的運行或者儲存效率。資料結構往往同高效的檢索演算法和索引技術有關。 一、資料的邏輯結構:指反映資料元素之間的邏輯關係的資料結構,其中的邏輯關係是指資料元素之間的前後件關係,而與他們在電腦中的儲存位置無關。邏輯結構包括:
1.集合
資料結構中的元素之間除了“同屬一個集合” 的相互關係外,別無其他關係;
2.線性結構
資料結構中的元素存在一對一的相互關係;
3.樹形結構
資料結構中的元素存在一對多的相互關係;
4.圖形結構
資料結構中的元素存在多對多的相互關係。 鏈表的定義
1.鏈表(Linked list)是一種常見的基礎資料結構,是一種線性表,但是不像順序表一樣連續儲存資料,而是在每一個節點(資料存放區單元)裡存放下一個節點的位置資訊(即地址)。2.鏈表是一種實體儲存體單元上非連續、非順序的儲存結構,資料元素的邏輯順序是通過鏈表中的指標連結次序實現的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態產生。每個結點包括兩個部分:一個是儲存資料元素的資料域,另一個是儲存下一個結點地址的指標域。 相比於線性表順序結構,操作複雜。由於不必須按順序儲存,鏈表在插入的時候可以達到O(1)的複雜度,比另一種線性表順序錶快得多,但是尋找一個節點或者訪問特定編號的節點則需要O(n)的時間,而線性表和順序表相應的時間複雜度分別是O(logn)和O(1)。3.使用鏈表結構可以克服數組鏈表需要預Crowdsourced Security Testing道資料大小的缺點,鏈表結構可以充分利用電腦記憶體空間,實現靈活的記憶體動態管理。但是鏈表失去了數組隨機讀取的優點,同時鏈表由於增加了結點的指標域,空間開銷比較大。鏈表最明顯的好處就是,常規數組排列關聯項目的方式可能不同於這些資料項目在記憶體或磁碟上順序,資料的存取往往要在不同的排列順序中轉換。鏈表允許插入和移除表上任意位置上的節點,但是不允許隨機存取。鏈表有很多種不同的類型:單向鏈表,雙向鏈表以及迴圈鏈表。鏈表可以在多種程式設計語言中實現。像Lisp和Scheme這樣的語言的內建資料類型中就包含了鏈表的存取和操作。程式語言或物件導向語言,如C,C++和Java依靠易變工具來產生鏈表。

1.單鏈表

#python3class Node(object):    """結點"""    def __init__(self, element):        self.element = element        self.next = Noneclass SingleLinkList(object):    """單向鏈表"""    def __init__(self):        self._head = None    def empty(self):        return self._head == None    def length(self):        cursor = self._head        count = 0        while cursor != None:            count += 1            cursor = cursor.next        return count    def traversal(self):        cursor = self._head        while cursor != None:            print(cursor.element, end=",")            cursor = cursor.next    def add(self, element):        node = Node(element)        node.next = self._head        self._head = node    def append(self, element):        node = Node(element)        if self.empty():            self._head = node        else:            cursor = self._head            while cursor.next != None:                cursor = cursor.next            cursor.next = node    def insert(self, position, element):        if position == 0:            self.add(element)        elif position > (self.length()-1):            self.append(element)        else:            node = Node(element)            count = 0            previous = self._head            while count < (position -1):                count += 1                previous = previous.next            node.next = previous.next            previous.next = node    def remove(self, element):        cursor = self._head        previous = None        while cursor != None:            if cursor.element == element:                if not previous:                    self._head = cur.next                else:                    previous.next = cursor.next                break            else:                previous = cursor                cursor = cursor.next    def search(self, element):        cursor = self._head        while cursor != None:            if cursor.element == element:                return True            cursor = cursor.next        return Falseif __name__ == "__main__":    linklist = SingleLinkList()    linklist.add(1)    linklist.add(2)    linklist.append(3)    linklist.insert(2, 4)    # print("length:",linklist.length())    linklist.traversal()    # print(linklist.search(3))    # print(linklist.search(5))    linklist.remove(1)    # print("length:",linklist.length())    # linklist.traversal()

2.雙向鏈表

增加了一個指向前面一個元素的指標,每個節點有兩個連結:一個指向前一個節點,當此節點為第一個節點時,前指標指向空值;而另一個指向下一個節點,當此節點為最後一個節點時,前指標指向上一個值,後指標指向空值。

class Node(object):    def __init__(self, item):        self.item = item        self.next = None        self.previous = Noneclass DoubleLinkList(object):    def __init__(self):        self._head = None    def is_empty(self):        return self._head == None    def length(self):        cursor = self._head        count = 0        while cursor != None:            count += 1            cursor = cursor.next        return count    def travel(self):        cursor = self._head        while cursor != None:            print(cursor.item, end=" ")            cursor = cursor.next        print("")    def add(self, item):        node = Node(item)        if self.is_empty():            self._head = node        else:            node.next = self._head            self._head.previous = node            self._head = node    def append(self, item):        node = Node(item)        if self.is_empty():            self._head = node        else:            cursor = self._head            while cursor.next != None:                cursor = cursor.next            cursor.next = node            node.previous = cursor    def search(self, item):        cursor = self._head        while cursor != None:            if cursor.item == item:                return True            cursor = cursor.next        return False    def insert(self, pos, item):        if pos <= 0:            self.add(item)        elif pos > (self.length()-1):            self.append(item)        else:            node = Node(item)            cursor = self._head            count = 0            while count < (pos-1):                count += 1                cursor = cursor.next            node.previous = cursor            node.next = cursor.next            cursor.next.previous = node            cursor.next = node    def remove(self, item):        if self.is_empty():            return        else:            cursor = self._head            if cursor.item == item:                if cursor.next == None:                    self._head = None                else:                    cursor.next.previous = None                    self._head = cursor.next                return            while cursor != None:                if cursor.item == item:                    cursor.previous.next = cursor.next                    cursor.next.previous = cursor.previous                    break                cursor = cursor.nextif __name__ == "__main__":    ll = DoubleLinkList()    ll.add(1)    ll.add(2)    ll.append(3)    ll.insert(2, 4)    ll.insert(4, 5)    ll.insert(0, 6)    print("length:",ll.length())    ll.travel()    print(ll.search(3))    print(ll.search(4))    ll.remove(1)    print("length:",ll.length())    ll.travel()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.