单向链表python实现,增删改查+链表反转

单向链表,实现了增删改查以及链表反转,

# 实现 is_empty / clear / append / prepend / search / insert /show_all/reverse/locate/del_posi

class Node:
    def __init__(self, val):
        self.val = val
        self.nextp = None


class LinkedList:
    def __init__(self):
        self.length = 0
        self.head = None
        self.tail = None

    def is_empty(self):
        return self.length == 0

    def clear(self):
        self.head = self.tail = None
        self.length = 0

    def locate(self, posi):
        now = self.head
        for i in range(posi - 1):
            now = now.nextp
        return now

    def append(self, val):
        if not self.is_empty():  # 如果已经有元素
            now = self.locate(self.length)
            val = Node(val)
            now.nextp = val
            self.length += 1
            self.tail = val
        else:  # 如果链表为空
            self.tail = self.head = Node(val)
            self.length = 1

    def prepend(self, val):
        val = Node(val)
        head = self.head
        self.length += 1
        self.head, val.nextp = val, self.head

    def search(self, value):  # 第一个位置为0
        now = self.head
        posi = 0
        while now.nextp != None:
            if now.val != value:
                posi += 1
                now = now.nextp
            else:
                return posi
        if now.val != value:
            print('no such element')
            return None

    def insert(self, posi, value):
        if posi > self.length:  # 插入超界
            print('index beyond maxium')
            return None

        if posi == 0 and not self.is_empty():  # 如果要插入的位置为零且非空
            self.prepend(value)
            return None

        if posi <= self.length:
            ele = Node(value)
            now = self.locate(posi)
            now.nextp, ele.nextp = ele, now.nextp
            if posi == self.length:
                self.tail = ele
            return None

    def show_all(self):
        if not self.is_empty():
            now = self.head
            while now.nextp != None:
                print(now.val)
                now = now.nextp
            print(now.val)
        else:
            print('empty list')

    def reverse(self):
        if self.is_empty():
            print('empty')
            return None
        if self.length == 1:
            print('Done')
            return None
        else:
            a, b = self.head, self.head.nextp
            for i in range(self.length - 2):
                c = b.nextp
                b.nextp, a, b = a, b, c
            b.nextp = a
            self.head, self.tail = self.tail, self.head
            self.tail.nextp = None

    def del_posi(self, posi):
        if not self.is_empty():
            if posi > self.length:
                print('beyond index')
                return None
            if posi == 0:
                if self.length == 1:
                    self.clear()
                else:
                    self.head = self.head.nextp
                    self.length -= 1
                return None
            if posi <= self.length:
                now = self.locate(posi - 1)
                print(now.val)
                now.nextp = now.nextp.nextp
                self.length -= 1

    def __str__(self):
        return "<LinkedList>  quantity:{}  head:{}  tail:{}".format(self.length, self.head.val, self.tail.val)

    def __iter__(self):
        now = self.head
        yield now.val
        for i in range(self.length - 1):
            now = now.nextp
            yield now.val


l = LinkedList()
for i in range(10):
    l.append(i)
print(l)
l2 = [i for i in l]
print(l2)
l.insert(3, 'a')
l.del_posi(6)
print('=====================')
l.show_all()
l.search('a')
l.reverse()
l.is_empty()
l.prepend('abc')
l.clear()
# 实现 is_empty / clear / append / prepend / search / insert /show_all/reverse/locate/del_posi

猜你喜欢

转载自blog.csdn.net/qq_32835305/article/details/85041230