def append(self, elem): if self._head is None: self._head = LNode(elem) self._rear = self._head else: self._rear.next_ = LNode(elem) self._rear = self._rear.next_
def prepend(self, elem): p = LNode(elem) 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): # 把要插入的数据放入一个结点 p = LNode(elem) # 如果是空表,建立循环 if self._rear is None: p.next = p self._rear = p else: p.next = self._rear.next self._rear.next = p
def append(self, elem): # 如果是空表 if self._head is None: self._head = LNode(elem, self._head) self._rear = self._head else: # 先让尾结点的引用域指向新增加的结点 # 再让尾结点指针指向新的结点 self._rear.next = LNode(elem) self._rear = self._rear.next
def prepend(self, elem): if self._head is None: # 如果是空表 # LNode(elem, self._head) - 先让新结点的引用域指向原来头指针指向的地方 # 再让头指针指向新的结点 self._head = LNode(elem, self._head) # 头指针和尾结点的指针都指向新的结点 self._rear = self._head # 不是空表 else: self._head = LNode(elem, self._head)
def insert(self, elem, i=0): p = self._head if i == 0: if self._head is None: self._head = LNode(elem, self._head) self._rear = self._head else: self._head = LNode(elem, self._head) return elif i != 0 and self._head is None: raise LinkedListUnderflow("insert") elif self.length() < i or i < 0: raise LinkedListUnderflow("insert length error") else: pass while p is not None and (i - 1) > 0: i -= 1 p = p.next p.next = LNode(elem, p.next) self._rear = p.next
def prepend(self, elem): if self._head is None: self._head = LNode(elem) self._rear = self._head else: self._head = LNode(elem, self._head)
def __init__(self, elem, prev=None, next_=None): LNode.__init__(self, elem, next_) self.prev = prev