[DS+Algo] 003 一维表结构 Python 代码实现

Posted yorkyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[DS+Algo] 003 一维表结构 Python 代码实现相关的知识,希望对你有一定的参考价值。

  • 接上一篇

前言

  • 本篇共 3 个代码实现
  • 严格来说
    • code1 相当于模仿了 Python 的 list 的部分简单功能
    • code2code3 简单实现了“循环单链表”与“双链表”
  • 代码比较简单,但它们的行数相比于普通博文又显得有些长,所以单独写一篇
  • 其实放到 GitHub 上更合适,我打算积几个再去 push

1. 模仿 Python 的 list 的一些功能

class SingleNode(object):
    def __init__(self, item):
        self.item = item
        self.next = None


class SingleLinkList(object):
    def __init__(self):
        self._head = None

    def is_empty(self):
        return self._head is None

    def length(self):
        cur = self._head

        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def traverse(self):
        cur = self._head

        while cur:
            print(cur.item)
            cur = cur.next
        return None

    def add_first(self, item):
        node = SingleNode(item)
        node.next = self._head
        self._head = node

    def append(self, item):
        node = SingleNode(item)

        if self.is_empty():
            self._head = node
        else:
            cur = self._head
            while cur.next:
                cur = cur.next
            cur.next = node

    def insert(self, pos, item):
        if pos <= 0:
            self.add_first(item)
        elif (self.length() - 1) < pos:
            self.append(item)
        else:
            node = SingleNode(item)

            count = 0
            pre = self._head
            while count < (pos - 1):
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    def remove(self, item):
        cur = self._head
        pre = None
        while cur:
            if cur.item == item:
                if not pre:
                    self._head = cur.next
                else:
                    pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next

    def search(self, item):
        cur = self._head
        while cur:
            if cur.item == item:
                return True
            cur = cur.next
        return False


if __name__ == "__main__":
    lst = SingleLinkList()
    
    lst.add_first(10)
    lst.add_first(20)
    lst.append(30)
    lst.insert(2, 40)
    print(f"Length of lst is {lst.length()}")
    lst.traverse()
    
    print(lst.search(30))
    print(lst.search(31))

    lst.remove(20)
    print(f"Length of lst is {lst.length()}")
    lst.traverse()

2. 循环单链表

class SingleNode(object):
    def __init__(self, item):
        self.item = item
        self.next = None


class SingleCycLinkedList(object):
    def __init__(self):
        self._head = None

    def is_empty(self):
        return self._head is None

    def length(self):
        if self.is_empty():
            return 0

        cnt = 1
        cur = self._head
        while cur.next != self._head:
            cur = cur.next
            cnt += 1
        return cnt

    def traverse(self):
        if self.is_empty():
            return

        cur = self._head
        print(cur.item)
        while cur.next != self._head:
            cur = cur.next
            print(cur.item)

    def add_first(self, item):
        node = SingleNode(item)
        if self.is_empty():
            self._head = node
            node.next = self._head
        else:
            node.next = self._head
            cur = self._head
            while cur.next != self._head:
                cur = cur.next
            cur.next = node
            self._head = node

    def append(self, item):
        node = SingleNode(item)
        if self.is_empty():
            self._head = node
            node.next = self._head
        else:
            cur = self._head
            while cur.next != self._head:
                cur = cur.next
            cur.next = node
            node.next = self._head

    def insert(self, pos, item):
        if pos <= 0:
            self.add_first(item)
        elif self.length() - 1 < pos:
            self.append(item)
        else:
            node = SingleNode(item)
            cur = self._head
            cnt = 0
            while cnt < pos - 1:
                cur = cur.next
                cnt += 1
            node.next = cur.next
            cur.next = node

    def remove(self, item):
        if self.is_empty():
            return

        cur = self._head
        pre = None
        if cur.item == item:
            if cur.next != self._head:
                while cur.next != self._head:
                    cur = cur.next
                cur.next = self._head.next
                self._head = self._head.next
            else:
                self._head = None
        else:
            pre = self._head
            while cur.next != self._head:
                if cur.item == item:
                    pre.next = cur.next
                    return
                else:
                    pre = cur
                    cur = cur.next
            if cur.item == item:
                pre.next = cur.next

    def search(self, item):
        if self.is_empty():
            return False

        cur = self._head
        if cur.item == item:
            return True

        while cur.next != self._head:
            cur = cur.next
            if cur.item == item:
                return True
        return False


if __name__ == "__main__":
    lst = SingleCycLinkedList()

    lst.add_first(2)
    lst.add_first(1)
    lst.append(4)
    lst.append(5)
    lst.insert(0, 0)
    lst.insert(3, 33)
    print(f"length: {lst.length()}")
    lst.traverse()

    print(lst.search(3))
    print(lst.search(5))

    lst.remove(33)
    print(f"length: {lst.length()}")
    lst.traverse()

3. 双链表

class Node(object):
    def __init__(self, item):
        self.item = item
        self.next = None
        self.prev = None


class DLinkList(object):
    def __init__(self):
        self._head = None

    def is_empty(self):
        return self._head is None

    def length(self):
        cur = self._head
        cnt = 0
        while cur:
            cnt += 1
            cur = cur.next
        return cnt

    def traverse(self):
        cur = self._head
        while cur:
            print(cur.item)
            cur = cur.next

    def add_first(self, item):
        node = Node(item)
        if self.is_empty():
            self._head = node
        else:
            node.next = self._head
            self._head.prev = node
            self._head = node

    def append(self, item):
        node = Node(item)
        if self.is_empty():
            self._head = node
        else:
            cur = self._head
            while cur.next:
                cur = cur.next
            cur.next = node
            node.prev = cur

    def search(self, item):
        cur = self._head
        while cur:
            if cur.item == item:
                return True
            cur = cur.next
        return False

    def insert(self, pos, item):
        if pos <= 0:
            self.add_first(item)
        elif self.length() - 1 < pos:
            self.append(item)
        else:
            node = Node(item)
            cur = self._head
            cnt = 0
            while cnt < pos - 1:
                cur = cur.next
                cnt += 1
            node.prev = cur
            node.next = cur.next
            cur.next.prev = node
            cur.next = node

    def remove(self, item):
        if self.is_empty():
            return
        else:
            cur = self._head
            if cur.item == item:
                if cur.next is None:
                    self._head = None
                else:
                    cur.next.prev = None
                    self._head = cur.next
                return
            cur = cur.next
            while cur:
                if cur.item == item:
                    cur.prev.next = cur.next
                    cur.next.prev = cur.prev
                    break
                cur = cur.next


if __name__ == "__main__":
    lst = DLinkList()

    lst.add_first(2)
    lst.add_first(1)
    lst.append(4)
    lst.append(5)
    lst.insert(0, 0)
    lst.insert(3, 33)
    print(f"length: {lst.length()}")
    lst.traverse()

    print(lst.search(3))
    print(lst.search(5))

    lst.remove(33)
    print(f"length: {lst.length()}")
    lst.traverse()

以上是关于[DS+Algo] 003 一维表结构 Python 代码实现的主要内容,如果未能解决你的问题,请参考以下文章

algo&ds2.线性表

algo&ds5.图及其存储结构遍历

[DS+Algo] 008 查找

c_cpp dsahapersonal_algo_ds

algo&ds8.最小生成树

algo&ds7.最短路径问题