Python写数据结构:单向循环链表

注:带头结点

#!/usr/bin/python3.5
# _*_coding:utf-8_*_


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


class CycleList:
    def __init__(self):
        self._head = Node(None)
        self._head.next = self._head
        self.rear = self._head

    def insert(self, index, value):
        n = Node(value)
        cur = self._head
        for i in range(index - 1):
            cur = cur.next
        n.next = cur.next
        cur.next = n
        if n.next == self._head:
            self.rear = n

    def remove(self, index):
        if index <= 0 or index > self.length():
            print('删除位置有误')
            return
        cur = self._head
        for i in range(index - 1):
            cur = cur.next
            if cur == None:
                print('删除位置有误')
                return
        n = cur.next
        cur.next = cur.next.next
        del n
        if cur.next == self._head:
            self.rear = cur

    def empty(self):
        return self._head.next == self._head

    def search(self, value):
        cur = self._head.next
        index = 1
        while cur != self._head and cur.data != value:
            cur = cur.next
            index += 1
        if cur == self._head:
            print('没有该元素')
            return
        print("index:%d,value:%d" % (index, value))

    def clear(self):
        cur = self._head.next
        while cur != self._head:
            n = cur
            cur = cur.next
            del n
        self._head.next = self._head
        self.rear = self._head

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

    def travel(self):
        if self.empty():
            print('空')
            return
        cur = self._head
        while cur.next != self._head:
            cur = cur.next
            print(cur.data)

    def appendhead(self, value):
        n = Node(value)
        if self.empty():
            self.rear = n
        n.next = self._head.next
        self._head.next = n

    def appendrear(self, value):
        n = Node(value)
        n.next = self.rear.next
        self.rear.next = n
        self.rear = n


if __name__ == '__main__':
    link = CycleList()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向循环链表是一种常见的数据结构,它与单向链表的区别在于每个节点都有两个指针,一个指向前一个节点,一个指向后一个节点。而循环链表则是在单向或双向链表的基础上,将最后一个节点的指针指向头节点,形成一个环。 以下是Python实现双向循环链表的示例代码: ```python class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def add_node(self, data): new_node = Node(data) if self.head is None: self.head = new_node self.tail = new_node self.head.prev = self.tail self.tail.next = self.head else: new_node.prev = self.tail self.tail.next = new_node new_node.next = self.head self.head.prev = new_node self.tail = new_node def display(self): current = self.head if self.head is None: print("List is empty") return else: print("Nodes of the doubly linked list:") print(current.data) while current.next != self.head: current = current.next print(current.data) dlist = DoublyLinkedList() dlist.add_node(1) dlist.add_node(2) dlist.add_node(3) dlist.display() ``` 上述代码中,我们定义了一个`Node`类来表示链表中的节点,每个节点包含一个数据项和两个指针,分别指向前一个节点和后一个节点。然后我们定义了一个`DoublyLinkedList`类来表示双向循环链表,其中包含一个头指针和一个尾指针。在`add_node`方法中,我们创建一个新节点,并将其添加到链表的末尾。在`display`方法中,我们遍历整个链表,并输出每个节点的数据项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值