def append(self, elem): if self.head is None: self.head = LNode(elem, None) return p = self.head while p.next is not None: p = p.next p.next = LNode(elem, None)
def prepend(self, elem): # add element in the front end p = LNode(elem, None) if self.rear is None: p.next = p # 表中第一个结点建立初始的循环链接 self.rear = p else: p.next = self.rear.next # 链在尾结点之后,就是新的首结点 self.rear.next = p
def prepend(self, elem): self.head = LNode(elem, self.head)
def __init__(self, prev, elem, nxt): LNode.__init__(self, elem, nxt) self.prev = prev # 扩充了一个前一结点指针
def append(self, elem): if self.rear is None: # empty list self.prepend(elem) else: self.rear.next = LNode(elem, None) self.rear = self.rear.next
def prepend(self, elem): self.head = LNode(elem, self.head) if self.rear is None: # empty list self.rear = self.head