python双链表

参考:www.bilibili.com/video/BV1E4…

实现

class Node:
  def __init__(self, data, nextNode=None, preNode=None):
      self.data = data
      self.next = nextNode
      self.pre = preNode


class DoubleLinkedList:
  def __init__(self, head=None):
      self.head = head if head else None
      self.tail = head if head else None
      self.length = 1 if head else 0

  def add_data(self, data):
      node = Node(data)
      # 没有head的情况将node定义成head和tail
      if not self.head:
          self.head = node
          self.tail = node
      else:
          self.tail.next = node
          node.pre = self.tail
          self.tail = node
      self.length += 1

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

  # 根据位置删除,比如第一个第二个等
  def del_data(self, position=1):
      if self.is_empty():
          print("空链表,没有数据可以删除")
          return
      if position <= 1:
          self.head = self.head.next
          self.head.pre = None
          self.length -= 1
          return
      elif position >= self.length:
          self.tail = self.tail.pre
          self.tail.next = None
          self.length -= 1
          return
      # 从head开始遍历
      if position < self.length // 2:
          tempNode = self.head
          for i in range(position - 1):
              tempNode = tempNode.next
          tempNode.pre.next = tempNode.next
          tempNode.next.pre = tempNode.pre
      else:
          # 从tail开始遍历
          tempNode = self.tail
          for i in range(self.length - position):
              tempNode = tempNode.pre
          tempNode.pre.next = tempNode.next
          tempNode.next.pre = tempNode.pre
      self.length -= 1

  def reverse(self):
      if self.is_empty():
          print("空链表")
          return
      if self.length == 1:
          return
      preNode = None
      tempNode = self.head
      self.tail = tempNode
      self.tail.pre = tempNode.next

      while tempNode:
          next_node = tempNode.next
          tempNode.next = preNode
          tempNode.pre = next_node
          preNode = tempNode
          tempNode = next_node
      self.head = preNode

  def print_list(self):
      tempNode = self.head
      while tempNode:
          print(tempNode.data, end="->")
          tempNode = tempNode.next
      print()

  def insert(self, data, position=1):
      node = Node(data)
      if self.is_empty():
          self.head = node
          self.tail = node
          self.length += 1
          return
      if position <= 1:
          self.head.pre = node
          node.next = self.head
          self.head = node
          self.length += 1
          return
      if position >= self.length:
          self.tail.next = node
          node.pre = self.tail
          self.tail = node
          self.length += 1
          return
      if position <= self.length // 2:
          tempNode = self.head
          for i in range(position - 1):
              tempNode = tempNode.next
          tempNode.pre.next = node
          node.pre = tempNode.pre
          node.next = tempNode
          tempNode.pre = node
      else:
          print("data:", data)
          tempNode = self.tail
          for i in range(self.length - position):
              tempNode = tempNode.pre
          print("tempnode data:", tempNode.data)
          tempNode.pre.next = node
          node.pre = tempNode.pre
          node.next = tempNode
          tempNode.pre = node
      self.length += 1


dl = DoubleLinkedList(Node(1))
print("tail:", dl.tail.data)
dl.add_data(2)
print("tail:", dl.tail.data)
dl.add_data(3)
print("tail:", dl.tail.data)
dl.add_data(4)
print("tail:", dl.tail.data)
dl.print_list()
dl.del_data(1)
print("tail:", dl.tail.data)
dl.print_list()
dl.del_data(4)
dl.print_list()
print("tail:", dl.tail.data)
# dl.print_list()
dl.add_data(5)
dl.print_list()
print("tail:", dl.tail.data)
dl.add_data(6)
dl.print_list()
print("tail:", dl.tail.data)
dl.add_data(7)
dl.print_list()
print("tail:", dl.tail.data)
dl.reverse()
print("反转")
print("tail:", dl.tail.data)
dl.print_list()
dl.del_data(1)
print("tail:", dl.tail.data)
dl.print_list()
dl.insert(8, 1)
print("tail:", dl.tail.data)
dl.print_list()
print("___________")
print(dl.length)
dl.insert(9, 3)
print("tail:", dl.tail.data)
dl.print_list()
print(dl.length)
dl.insert(10, 2)
dl.print_list()
print(dl.head.data)
print(dl.tail.data)
dl.reverse()
print(dl.head.data)
print(dl.tail.data)
dl.print_list()
dl.del_data(3)
dl.print_list()
dl.del_data(6)
dl.print_list()
print(dl.tail.data)
print(dl.length)