链表(未完成)

class SBLinkedList(ValueError):
    pass

class LNode():

    def __init__(self, elem, next_ = None):
        self.elem = elem
        self.next = next_


class LList():

    def __init__(self):
        self.__head = None

    def is_empty(self):
        return self.__head is None

    def prepend(self, elem):
        self.__head = LNode(elem, self.__head)

    def pop(self):
        if self.__head is None:
            raise SBLinkedList
        e = self.__head.elem
        self.__head = self.__head.next
        return e

    def append(self, elem):
        if self.__head is None:
            self.__head = LNode(elem)
            return
        p = self.__head
        while p.next is not None:
            p = p.next
        p.next = LNode(elem)

    def pop_last(self):
        if self.__head is None:
            raise SBLinkedList
        p = self.__head

        if p.next is None:
            e = p.elem
            self.__head = None
            return e

        while p.next.next is None:   # !!!!
            p = p.next

        e = p.next.elem
        p.next = None
        return e

    def find(self, target):
        p  = self.__head
        while p is not None:
            if p.elem == target:
                return p.elem
            p = p.next

    def printall(self):
        p = self.__head
        while p is not None:
            print(p.elem, end='')
            if p.next is not None:
                print(', ', end='')
            p = p.next
        print('')


list1= LList()
for i in range(10):
    list1.prepend(i)
for i in range(11, 20):
    list1.append(i)

list1.printall()

你可能感兴趣的:(链表(未完成))