数据结构之线性表 - Python实现

线性表

顺序表

功能参考Python的list
特点:

  1. O(1)时间的定位元素访问
  2. 添加、删除操作代价高

链表

单向链表

实现:

class LNode:
    def __init__(self, data, next_=None):
        self.data = data
        self.next = next_


class LList:
    """
    单链表
    """

    def __init__(self, node=None):  # 使用一个默认函数,接收传入的节点,若无传入,则默认头节点为空
        self.__head = node

    # 判断是否为空
    def is_empty(self):
        return self.__head == None

    # 头部添加节点
    def add_node(self, data):
        node = LNode(data)
        node.next = self.__head
        self.__head = node

    # 尾部添加节点
    def append_node(self, data):
        node = LNode(data)

        # 判断链表首付为空
        if self.is_empty():
            self.__head = node
        # 若不为空则创建一个游标 来指向节点位置
        else:
            cur = self.__head
            while cur.next:
                cur = cur.next
            cur.next = node

    # 向指定位置插入节点
    def insert_node(self, i, data):
        if i < 0:
            self.add_node(data)
        elif i > self.length - 1:
            self.append_node(data)
        else:
            node = LNode(data)
            index = 0
            cur = self.__head
            # 循环找到要插入位置的前一个节点
            while index < i - 1:
                index += 1
                cur = cur.next
            node.next = cur.next
            cur.next = node

    # 遍历节点(打印)
    def travel_llist(self):
        if self.is_empty():
            print('[]')
        else:
            cur = self.__head
            index = 0
            while index < self.length:
                print(cur.data)
                if index < self.length - 1:
                    cur = cur.next
                index += 1

    # 按位置删除某个节点 默认列表从0开始
    def remove_node(self, i):
        if 0 <= i < self.length:

            cur = self.__head
            index = 0
            if i == 0:
                self.__head = cur.next
            elif i == self.length - 1:
                while index < self.length - 2:
                    cur = cur.next
                    index += 1
                cur.next = None
            else:
                while index < i - 1:
                    cur = cur.next
                    index += 1
                cur.next = cur.next.next
        else:
            print("i无效")

    # 判断是否存在某个值,返回其坐标:
    def is_exist(self, value):
        cur = self.__head
        index = 0
        while cur:
            if cur.data == value:
                return index
            else:
                cur = cur.next
                index += 1
        return False

    # 按值删除某个节点
    def remove_node_by_value(self, value):
        cur = self.__head
        pre = None

        while cur:
            if cur.data == value:
                # 找到了指定元素
                if not pre: # 第一个就是
                    self.__head = cur.next
                else:
                    # 将删除位置前一个节点的next指向删除位置的后一个节点
                    pre.next = cur.next
                break
            else:
                # 继续按链表后移节点
                pre = cur
                cur = cur.next

    @property
    def length(self):
        len_list = 0
        # 创建游标
        cur = self.__head
        while cur is not None:
            len_list += 1
            cur = cur.next
        return len_list

# 测试
def main():
    llist = LList()
    llist.add_node(4)
    llist.add_node(5)
    llist.append_node(5)
    llist.append_node(6)
    llist.append_node(10)
    llist.append_node(6)
    llist.insert_node(2, 7)
    llist.append_node(8)
    llist.insert_node(3, 9)
    s = llist.is_exist(6)
    print(s)
    print('llist.length', llist.length)
    llist.travel_llist()

双向链表

实现

class DNode:
    def __init__(self, data, prev_=None, next_=None):
        self.data = data
        self.prev = prev_
        self.next = next_
        
class DList:
    """
    双向链表
    """

    def __init__(self, node=None):
        self.head = node

    def is_empty(self):
        return self.head

    def get_len(self):

        cur = self.head
        count = 0
        while cur:
            count += 1
            cur = cur.next
            print(cur)
        return count

    def travel(self):
        cur = self.head
        if not cur:
            print('None')
        else:
            while cur:
                print(cur.data)
                cur = cur.next

    def add_node(self, data):
        node = DNode(data)
        if self.head is None:
            self.head = node
        else:
            node.next = self.head
            self.head.prev = node
            self.head = node

    def append_node(self, data):
        node = DNode(data)

        if self.head is None:
            self.head = node
        else:
            cur = self.head
            while cur.next:
                cur = cur.next
            print('ddddddd', cur.data)

            cur.next = node
            node.prev = cur

    def insert_node(self, i, data):
        if 0 <= i < self.get_len():
            if i == 0:
                self.add_node(data)
            elif i == (self.get_len() - 1):
                self.append_node(data)
            else:
                node = DNode(data)
                cur = self.head
                index = 0

                while index < i:
                    cur = cur.next
                    index += 1
                node.next = cur
                node.prev = cur.prev
                cur.prev.next = node

        else:
            print('i无效')

    def remove_node(self, i):
        if self.get_len():
            if 0 <= i < self.get_len():
                if i == 0:
                    self.head.next.prev = None
                    self.head = self.head.next
                else:
                    cur = self.head
                    index = 0
                    while index < i - 1:
                        cur = cur.next
                        index += 1
                    if index == (self.get_len() - 2):
                        cur.next = None
                    else:
                        cur.next.next.prev = cur
                        cur.next = cur.next.next
            else:
                print('I无效')
        else:
            print('空表你删除个啥')

    def is_exist(self, value):
        cur = self.head
        has_val = False
        while cur:
            if cur.data == value:
                has_val = True
                break
            cur = cur.next
        return has_val

    def remove_node_by_value(self, value):
        cur = self.head
        has_val = False

        while cur:
            if cur.data == value:
                if cur.prev is None:
                    if cur.next is None:
                        # 只有一个节点
                        self.head = None
                    else:
                        cur.next.prev = None
                        self.head = cur.next
                        has_val = True
                        break
                else:
                    if cur.next:

                        cur.next.prev = cur.prev
                        cur.prev.next = cur.next
                    else:
                        cur.prev.next = None
                    has_val = True
                    break
            cur = cur.next

        if not has_val:
            print('无此值')

def main():
    dlist = DList()
    # dlist.remove_node(0)
    for i in range(2, 10, 2):
        dlist.add_node(i)
    dlist.append_node(5)
    dlist.append_node(12)
    dlist.insert_node(1, 3)
    dlist.insert_node(0, 100)
    dlist.insert_node(7, 100)
    # dlist.insert_node(-1, 100)
    # dlist.insert_node(6, 100)
    dlist.insert_node(7, 200)
    # dlist.append_node(234)

    # print(dlist.head)
    # print(dlist.head.data)
    # print(dlist.head.prev)
    # print(dlist.head.next.data)
    # print(dlist.head.next.prev.data)
    dlist.remove_node(0)
    dlist.remove_node(6)
    dlist.remove_node(1)
    dlist.remove_node(6)
    # dlist.remove_node_by_value(8)
    # dlist.remove_node_by_value(10)
    dlist.remove_node_by_value(5)
    dlist.remove_node_by_value(12)
    # print('a', a)
    # print('index', dlist.remove_node_by_value(8))
    print()
    s = dlist.get_len()
    print('len', s)

   链表的反转,不同链表间的操作未实现,有时间以后再完善吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值