python 链表发方细致实现配例子

看了数据结构关于链表的内容,于是在leetcode上搜索了对用题目,同时动手实现了链表的数据结构。最后一种使用list 方式实在是太有python风格了。代码都有对应注释。

# Node for Linked List https://leetcode.com/problems/design-linked-list/
class Node:

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


class linked_list:

    def __init__(self):
        self.head = None
        self.size = 0   # size of linklist

    def get(self, index:int)->int:
        """Get the value of the index-node in the linked list. if the index is valid return -1"""
        if self.head is None or index >= self.size or index < 0:
            return -1
        count, pre = 0, self.head
        # while count < index-1:
        #      pre = pre.next
        #      count += 1
        for i in range(index):
            pre = pre.next
        return pre.data

    # add data in the front
    def addAtHead(self, val: int) -> None:
        """Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list."""
        node = Node(val)
        node.next = self.head
        self.head = node
        self.size += 1

    def addAtTail(self, val: int) -> None:
        cur, node = self.head, Node(val)
        if self.head is None:
            self.head = node
        else:
            while cur.next:
                cur = cur.next
            cur.next = node
        print("add ", node.data)
        self.size += 1

    def addAtIndex(self, index, val):
        if index < 0 or index > self.size:
            return

        if index == 0:
            self.addAtHead(val)

        else:
            cur = self.head
            for i in range(index-1):
                cur = cur.next
            node = Node(val)
            node.next = cur.next
            cur.next = node
            self.size += 1

    def deleteAtIndex(self, index):
        if index < 0 or index >= self.size:
            return

        if index == 0:
            self.head = self.head.next
            self.size -= 1
            return

        cur = self.head
        for i in range(1, index):
            cur = cur.next
        cur.next = cur.next.next
        self.size -= 1

    # link list Traverlsal
    def showList(self):
        # use array to store
        res = []
        pre = self.head
        while pre:
            res.append(pre.data)
            self.size += 1
            pre= pre.next
        print(res)
        return res


class linked_list_2:

    def __init__(self):
        self.head = None
        self.size = 0  # size of linklist

    # empty size?
    def isEmpty(self):
        if self.size == 0:
            print("LinkList is Empty")
            return True
        else:
            print("LinkList is not Empty")
            return False

    # link list Traverlsal
    def showList(self):
        # use array to store
        res = []
        pre = self.head
        while pre:
            res.append(pre.data)
            pre = pre.next
        print(res)
        return res


# Using list to design linked list is also a good way.
if __name__ == '__main__':

    # first method to create linked list
    l2 = linked_list_2()

    # use the attribute to add node
    l2.head = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    l2.head.next = node2
    node2.next = node3
    l2.showList()   # [1, 2, 3]


    link = linked_list()
    # addhead
    link.addAtHead(3)
    link.addAtHead(2)
    link.addAtHead(1)
    link.addAtTail(4)
    link.showList()
    # [1, 2, 3, 4]

    link.addAtIndex(2,6)     # [1, 2, 6, 3, 4]
    link.showList()
    link.deleteAtIndex(2)   # [1, 2, 3, 4]
    print(link.size)
    link.showList()
    # link.isEmpty()
    print(link.get(3))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值