def pop(self):
     if self._head is None:
         raise LinkedListUnderflow("in pop of DLList")
     e = self._head.elem
     self._head = self._head.next
     if self._head:
         self._head = None
     return e
Exemple #2
0
 def pop(self):
     if self._rear is None:
         raise LinkedListUnderflow("in pop of LCList")
     p = self._rear.next
     if self._rear is p:
         self._rear = None
     else:
         self._rear.next = p.next
     return p.elem
 def pop_last(self):
     if self._head is None:
         raise LinkedListUnderflow("in pop_last of DLList")
     e = self._rear.elem
     self._rear = self._rear.prev
     if self._rear is None:
         self._head = None
     else:
         self._rear.next = None
     return e
Exemple #4
0
 def pop(self):
     # 如果是空表
     if self._head is None:
         raise LinkedListUnderflow('空表')
     # 保存被删除的元素
     e = self._head.elem
     # 让链表的头指针指向第二个结点
     self._head = self._head.next
     if self._head is not None:
         # 把第二个结点的prev指针设为空
         self._head.prev = None
     return e
    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
Exemple #6
0
 def pop(self):
     # 如果是空表
     if self._head is None:
         raise LinkedListUnderflow('该表中没有元素')
     # 不是空表
     # 如果只有一个元素,则头指针和尾指针都指向None
     elif self._head.next == None:
         self._head = self._head.next
         self._rear = self._head
     # 不只一个元素,丢弃第一个结点
     else:
         self._head = self._head.next
 def pop(self):
     # 如果是空表
     if self._rear is None:
         raise LinkedListUnderflow('空表')
     # p保存要弹出的结点
     p = self._rear.next
     # 如果只有一个结点
     if self._rear is p:
         self._rear = None
     else:
         self._rear.next = p.next
     # 返回要删除的元素
     return p.elem
Exemple #8
0
 def deltail(self):
     if self._head is None:
         raise LinkedListUnderflow('空表')
     e = self._rear.elem
     # 让表尾指针指向倒数第二个结点
     self._rear = self._rear.prev
     # 如果删除了一个结点后,没有结点了
     if self._rear is None:
         self._head = None
     else:
         # 把倒数第二个结点的next设为None
         self._rear.next = None
     return e
Exemple #9
0
 def pop_last(self):
     if self._head is Node:
         raise LinkedListUnderflow("in pop_last")
     p = self._head
     if p.next is None:
         e = p.elem
         self._head = None
         return e
     while p.next.next:
         p = p.next
     e = p.next.elem
     p.next = None
     self._rear = p
 def deltail(self):
     # 如果是空表
     if self._rear is None:
         raise LinkedListUnderflow('空表')
     # 扫面指针,从头开始
     p = self._rear.next
     if self._rear is p:
         self._rear = None
     else:
         while p.next.next is not self._rear.next:
             p = p.next
         # return p.next.elem
         p.next = self._rear.next
         self._rear = p
Exemple #11
0
 def pop_last(self):
     if self.is_empty():
         raise LinkedListUnderflow("in pop_last")
     p = self._rear.next
     if p is self._rear:
         self._rear = None
         return p.elem
     else:
         e = self._rear.elem
         while True:
             if p.next is self._rear:
                 break
             p = p.next
         p.next = self._rear.next
         self._rear = p
         return e
Exemple #12
0
 def deltail(self):
     # 如果是空表
     if self._head is None:
         raise LinkedListUnderflow('该表中没有元素')
     # 扫描指针
     p = self._head
     # 表中只有一个元素
     if self._head.next is None:
         e = p.elem
         self._head = None
         return e
     # 找到最后一个结点
     while p.next.next is not None:
         p = p.next
     e = p.next.elem
     p.next = None
     self._rear = p
     return e