예제 #1
0
 def insert(self, pos=0, el=None):
     """
     在指定位置插入新节点
     pos 位置,从0开始
     :return:
     """
     if isinstance(pos, int):
         if 0 > pos or pos > self.__size:
             raise LinkListError('i is false %d' % (pos, ))
         elif pos == 0:
             self.push_front(el)
         elif pos == self.__size:
             self.push_back(el)
         else:
             count = 0
             e = self.__head
             while count != pos - 1:
                 e = e.next
                 count += 1
             h = DLNode(el, n=e.next, p=e)
             h.prev.next = h
             h.next.prev = h
             self.__size += 1
     else:
         raise LinkListError('param pos is not int type!!')
예제 #2
0
 def isinList(self, el):
     """
     el是否在链表中
     :return:
     """
     if isinstance(el, int):
         if not self.__size:
             #空链表,报错
             raise LinkListError('linklist is empty')
         p = self.__head
         while p.elem != el and p:
             p = p.next
         return True if p.elem == el else False
     else:
         raise LinkListError('param el is not int type!!')
예제 #3
0
    def delete(self, el):
        """
        删除指的一个节点
        :return:
        """
        if not self.__size:
            #空链表,报错
            raise LinkListError('linklist is empty')
        if self.__size == 1 and self.__head.elem == el:
            self.__head = None
            self.__tail = None
            self.__size -= 1
        elif self.__head.elem == el:
            self.__head = self.__head.next
            self.__head.prev = None
            self.__size -= 1
        else:
            e = self.__head
            p = None
            while e:
                if e.elem != el:
                    p = e
                    e = e.next
                else:

                    if e == self.__tail:
                        self.__tail = p
                        self.__tail.next = None
                    else:
                        p.next = e.next
                        p.next.prev = p
                    self.__size -= 1
                    break
예제 #4
0
 def back(self):
     """
     返回最后一个节点
     :return:
     """
     if not self.__size:
         #空链表,报错
         raise LinkListError('linklist is empty')
     return self.__tail
예제 #5
0
 def front(self):
     """
     返回第一个节点
     :return:
     """
     if not self.__size:
         #空链表,报错
         raise LinkListError('linklist is empty')
     return self.__head
예제 #6
0
 def get(self, i):
     """
     访问链表i位置的节点
     :return:
     """
     if isinstance(i, int):
         if not self.__size:
             #空链表,报错
             raise LinkListError('linklist is empty')
         if 0 >= i or i > self.__size:
             raise LinkListError('i is false %d' % (i, ))
         n = 0
         e = self.__head
         while e.next and e.next is not self.__tail and n != i:
             e = e.next
             n += 1
         return e.elem
     else:
         raise LinkListError('param i is not int type!!')
예제 #7
0
 def elements(self):
     """
     遍历链表所有节点
     :return:
     """
     if not self.__size:
         #空链表,报错
         raise LinkListError('linklist is empty')
     p = self.__head
     while p is not None:
         yield p.elem
         # print('el=%d, next=%d' % (p.elem, p.next),end='|')
         p = p.next
예제 #8
0
 def pop_front(self):
     """
     删除链表头节点
     :return:
     """
     if not self.__size:
         #空链表,报错
         raise LinkListError('linklist is empty')
     if self.__size == 1:
         self.__head = None
         self.__tail = None
     else:
         self.__head = self.__head.next
         self.__head.prev = None
     self.__size -= 1
예제 #9
0
 def pop_back(self):
     """
     删除链表尾节点
     :return:
     """
     if not self.__size:
         #空链表,报错
         raise LinkListError('linklist is empty')
     if self.__size == 1:
         self.__head = None
         self.__tail = None
     else:
         self.__tail = self.__tail.prev
         self.__tail.next = None
     self.__size -= 1