基于Python单向链表实现尾部、任意位置添加,删除

来源:互联网 发布:s8不祥之刃符文 知乎 编辑:程序博客网 时间:2024/04/29 14:35
# coding = utf-8
# 创建节点类
class Node(object):
    def __init__(self, data):
        # 定义指向域
        self.next = None
        # 定义数据域
        self.data = data

# 创建链表类
class LinkDict(object):
    def __init__(self):
        self._head = Node(None)
        self._length = 0

    # 尾部添加元素
    def append(self, data):
        # 构造新节点
        new_code = Node(data)
        # 查找尾部节点并定义游标指向头节点,向尾部添加新节点
        cur = self._head
        # 循环遍历
        while cur.next is not None:
            # 移动游标
            cur = cur.next
        else:
            # 向尾部添加新节点
            cur.next = new_code
            # 链表长度加1
            self._length += 1
        return self._length

    # 循环遍历链表元素
    def traval(self):
        cur = self._head
        while cur.next is not None:
            # 打印后边节点
            print(cur.next.data, "--->", end="")
            # 游标后移
            cur = cur.next
        else:
            print("None:now is terminal")

    # 任意位置添加元素
    def insert(self, pos, data):
        # 判断插入点
        if isinstance(pos, int):
            if pos < 0:
                link_dict.insert(0, data)
            elif pos > self._length:
                link_dict.append(data)
            else:
                # 构造新节点
                new_code = Node(data)
                # 构造游标
                cur = self._head
                # 构造计数器
                cnt = 0
                while cur.next is not None:
                    if cnt == pos:
                        # 让新节点有所指向
                        new_code.next = cur.next
                        # 让与新节点有关的节点有所指向
                        cur.next = new_code
                        self._length += 1
                        return
                    else:
                        # 移动游标
                        cur = cur.next
                        # 计数器加1
                        cnt += 1
        else:
            print("pos位置无效, 请确认")
            exit()

    def is_empty(self):
        if self._length == 0:
            print("链表为空")
            return True
        else:
            return False

    def remove(self, data):
        if link_dict.is_empty():
            return True
        else:
            cur = self._head
            while cur.next is not None:
                if cur.next.data == data:
                    # 删除节点
                    cur.next = cur.next.next
                    self._length -= 1
                    return
                else:
                    # 游标移动
                    cur = cur.next
            else:
                print("data is not in link_data")

if __name__ == "__main__":
    link_dict = LinkDict()
    print("尾 部 添 加:  ", end="")
    for i in range(10):
        link_dict.append(i)
    link_dict.traval()
    print("任意位置添加: ", end="")
    link_dict.insert(-1, "a")
    link_dict.insert(100, "z")
    link_dict.insert(6, "x")
    link_dict.traval()
    print("删 除 节 点: ", end="")
    link_dict.remove("a")
    link_dict.remove("x")
    link_dict.remove("z")

    link_dict.traval()



程序运行结果:

尾 部 添 加:  0 --->1 --->2 --->3 --->4 --->5 --->6 --->7 --->8 --->9 --->Node:now is terminal
任意位置添加: a --->0 --->1 --->2 --->3 --->4 --->x --->5 --->6 --->7 --->8 --->9 --->z --->Node:now is terminal
删 除 节 点: 0 --->1 --->2 --->3 --->4 --->5 --->6 --->7 --->8 --->9 --->Node:now is terminal

原创粉丝点击