美文网首页
python 单项链表

python 单项链表

作者: 高峥 | 来源:发表于2020-01-14 16:00 被阅读0次
class Node(object):
    def __init__(self, item):
        self.item = item
        self.next = None


class SingLink(object):
    def __init__(self, node=None):
        self.__head = node

    def length(self):
        count = 0
        cur_node = self.__head
        while cur_node != None:
            count += 1
            cur_node = cur_node.next
        return count

    def traverse(self):
        cur_node = self.__head
        while cur_node:
            print(cur_node.item, end=',')
            cur_node = cur_node.next
        print('')

    def append(self, ele):
        if ele is None:
            return None
        node = Node(ele)
        if self.__head == None:
            self.__head = node
            return node
        cur_node = self.__head
        while cur_node.next != None:
            cur_node = cur_node.next
        cur_node.next = node
        return node

    def add(self, ele):
        node = Node(ele)
        if self.__head:
            node.next = self.__head
            self.__head = node
        else:
            self.__head = node

    def insert(self, index, ele):
        count = 1
        cur = self.__head
        node = Node(ele)

        if index == 0:
            self.add(ele)
            return
        if index == 1:
            node.next = self.__head.next
            self.__head.next = node
            return

        if index < self.length():
            while True:
                cur = cur.next
                count += 1
                if count == index:
                    node.next = cur.next
                    cur.next = node
        else:
            self.append(ele)

    def delete(self, ele):

        cur = self.__head
        if cur.item == ele:
            self.__head= cur.next

        while cur:
            if cur.next:
                if cur.next.item == ele:
                    cur.next = cur.next.next
                    break
                cur = cur.next

            else:
                return

s = SingLink()
s.append('A')
s.append('B')
s.append('C')

s.traverse()

s.insert(0, 'D')
print(s.length())
s.traverse()
s.insert(10, 'F')
s.traverse()

s.delete("D")
s.delete("F")
s.delete("F")
s.traverse()

相关文章

  • Python数据结构-链表

    自己实现一遍果然感觉不一样 Python实现单链表 Python实现单项循环链表 Python实现双向链表

  • python 单项链表

  • 使用php创建一个单项链表

    创建链表 创建一个单项链表。

  • 单项环形链表介绍和约瑟夫问题

    单项环形链表介绍和约瑟夫问题 1.单项环形链表图解 2.Josephu(约瑟夫)问题 Josephu 问题为:设...

  • 单向链表

    闲来无事,弄个单项链表.在此小记.

  • 《剑指offer》逆转链表

    问题: 输入一个链表,从尾到头打印链表每个节点的值。 实现方式: 双向链表 单项链表通过交换元素进行反转链表(我的...

  • 数据结构与算法(第一季):循环链表

    一、单向循环链表 尾节点的next,指向头节点。 二、单向循环链表接口设计 相较于单项链表,单向循环链表需要重写插...

  • 单项链表题

    下列函数试图求链式存储的线性表的表长,是否正确? 上面给出的代码无法实现求取表长。p++只能是在内存连续的时候,指...

  • 02单项循环链表

    [toc] 循环链表 循环链表 就是在单链表的基础上修改而来 单链表是将尾结点的指针指向NULL,循环链表是将尾结...

  • LinkedBlockingQueue

    简介 LinkedBlockingQueue 底层结构为单项链表,拥有两把锁 takeLock 和 putLock...

网友评论

      本文标题:python 单项链表

      本文链接:https://www.haomeiwen.com/subject/dudaactx.html