python 数据结构之链表

单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。


class Node(object):

    """节点类"""

    def __init__(self, item):
        # 记录元素内容
        self.item = item
        # 记录下一个节点
        self.next = None


class SingleLinkList(object):

    """链表类"""

    def __init__(self):
        # 记录首节点
        self.__head = None

    def is_empty(self):
        """链表是否为空"""
        # 判断首节点是否为空
        return self.__head is None

    def length(self):
        """链表长度"""
        # 定义count统计长度
        count = 0
        cur = self.__head
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        # 从首节点遍历
        # 创建游标,不停的往后移动
        cur = self.__head
        while cur is not None:
            # 输出当前节点的内容
            print(cur.item, end=" ")
            cur = cur.next
        print()

    def add(self, item):
        """链表头部添加元素"""
        # 创建新节点
        node = Node(item)
        # 让新节点的next记录老的头节点
        node.next = self.__head
        # 让head记录新的头
        self.__head = node

    def append(self, item):
        """链表尾部添加元素"""
        if self.is_empty():
            self.add(item)
            return
        # 先遍历,找到尾节点
        cur = self.__head
        while cur.next is not None:
            cur = cur.next
        # 当while循环结束,cur指向的是尾节点
        node = Node(item)
        cur.next = node

    def insert(self, pos, item):
        """指定位置添加元素"""
        # 健壮性
        if pos <= 0:
            self.add(item)
        elif pos >= self.length():
            self.append(item)
        else:
            # 先遍历,找到pos位置的前一个节点
            cur = self.__head
            index = 0
            while index < (pos - 1):
                index += 1
                cur = cur.next
            # 当while循环结束,cur指向的是pos前一个节点
            node = Node(item)
            # 让新节点指向pos位置的节点
            node.next = cur.next
            # 在让pos前一个节点指向新节点
            cur.next = node

    def remove(self, item):
        """删除节点"""
        # 先遍历,找到要删除的节点
        cur = self.__head
        # 定义pre,记录当前节点的前一个节点
        pre = None
        while cur is not None:
            # 判断当前节点是否是要删除的节点
            if cur.item == item:
                # 如果pre为空,证明要删除的是首节点
                if pre is None:
                    self.__head = cur.next
                else:
                    pre.next = cur.next
            pre = cur
            cur = cur.next

    def search(self, item):
        """查找节点是否存在"""
        # 遍历,查找每一个节点
        cur = self.__head
        while cur is not None:
            if cur.item == item:
                return True
            cur = cur.next
        return False


双向链表:每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。


class Node(object):

    """节点类"""

    def __init__(self, item):
        # 记录元素内容
        self.item = item
        # 记录下一个节点
        self.next = None
        # 记录前一个节点
        self.pre = None


class DoubleLinkList(object):

    """链表类"""

    def __init__(self):
        # 记录首节点
        self.__head = None

    def is_empty(self):
        """链表是否为空"""
        # 判断首节点是否为空
        return self.__head is None

    def length(self):
        """链表长度"""
        # 定义count统计长度
        count = 0
        cur = self.__head
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        # 从首节点遍历
        # 创建游标,不停的往后移动
        cur = self.__head
        while cur is not None:
            # 输出当前节点的内容
            print(cur.item, end=" ")
            cur = cur.next
        print()

    def add(self, item):
        """链表头部添加元素"""
        # 创建新节点
        node = Node(item)
        if self.is_empty():
            self.__head = node
            return

        # 让新节点的next记录老的头节点
        node.next = self.__head
        # 让老的头节点的pre指向新节点
        self.__head.pre = node
        # 让head记录新的头
        self.__head = node

    def append(self, item):
        """链表尾部添加元素"""
        if self.is_empty():
            self.add(item)
            return
        # 先遍历,找到尾节点
        cur = self.__head
        while cur.next is not None:
            cur = cur.next
        # 当while循环结束,cur指向的是尾节点
        node = Node(item)
        cur.next = node
        # 让新节点的pre指向老的尾节点
        node.pre = cur

    def insert(self, pos, item):
        """指定位置添加元素"""
        # 健壮性
        if pos <= 0:
            self.add(item)
        elif pos >= self.length():
            self.append(item)
        else:
            # 先遍历,找到pos位置的前一个节点
            cur = self.__head
            index = 0
            while index < (pos - 1):
                index += 1
                cur = cur.next
            # 当while循环结束,cur指向的是pos前一个节点
            node = Node(item)
            # 让新节点指向pos位置的节点
            node.next = cur.next
            # 让pos位置的节点的pre指向新节点
            cur.next.pre  = node
            # 在让pos前一个节点指向新节点
            cur.next = node
            # 让新节点的pre指向pos前一个节点
            node.pre = cur

    def remove(self, item):
        """删除节点"""
        # 先遍历,找到要删除的节点
        cur = self.__head
        # 定义pre,记录当前节点的前一个节点

        while cur is not None:
            # 判断当前节点是否是要删除的节点
            if cur.item == item:
                # 让当前节点的前一个节点的next指向 当前节点的下一个节点
                if cur.pre is None:  # 证明删除的是首节点
                    self.__head = cur.next
                else:
                    cur.pre.next = cur.next
                    if cur.next is not None:
                        cur.next.pre = cur.pre
            cur = cur.next

    def search(self, item):
        """查找节点是否存在"""
        # 遍历,查找每一个节点
        cur = self.__head
        while cur is not None:
            if cur.item == item:
                return True
            cur = cur.next
        return False


单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点。

class Node(object):

    """节点类"""

    def __init__(self, item):
        # 记录元素内容
        self.item = item
        # 记录下一个节点
        self.next = None


class SingleCycLinkList(object):

    """链表类"""

    def __init__(self):
        # 记录首节点
        self.__head = None

    def is_empty(self):
        """链表是否为空"""
        # 判断首节点是否为空
        return self.__head is None

    def length(self):
        """链表长度"""
        if self.is_empty():
            return 0
        # 定义count统计长度
        count = 1
        cur = self.__head
        while cur.next is not self.__head:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        if self.is_empty():
            return
        # 从首节点遍历
        # 创建游标,不停的往后移动
        cur = self.__head
        print(cur.item, end=" ")
        while cur.next is not self.__head:
            # 输出当前节点的内容
            cur = cur.next
            print(cur.item, end=" ")
        print()

    def add(self, item):
        """链表头部添加元素"""
        # 创建新节点
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
            return

        # 遍历,找到尾节点
        cur = self.__head
        while cur.next is not self.__head:
            cur = cur.next
        # 循环结束,cur指向的是尾节点
        node.next = self.__head
        self.__head = node
        cur.next = self.__head

    def append(self, item):
        """链表尾部添加元素"""
        if self.is_empty():
            self.add(item)
            return
        # 先遍历,找到尾节点
        cur = self.__head
        while cur.next is not self.__head:
            cur = cur.next
        # 当while循环结束,cur指向的是尾节点
        node = Node(item)
        cur.next = node
        # 让新节点指向头节点
        node.next = self.__head

    def insert(self, pos, item):
        """指定位置添加元素"""
        # 健壮性
        if pos <= 0:
            self.add(item)
        elif pos >= self.length():
            self.append(item)
        else:
            # 先遍历,找到pos位置的前一个节点
            cur = self.__head
            index = 0
            while index < (pos - 1):
                index += 1
                cur = cur.next
            # 当while循环结束,cur指向的是pos前一个节点
            node = Node(item)
            # 让新节点指向pos位置的节点
            node.next = cur.next
            # 在让pos前一个节点指向新节点
            cur.next = node

    def remove(self, item):
        """删除节点"""
        if self.is_empty():
            return
        # 先遍历,找到要删除的节点
        cur = self.__head
        # 定义pre,记录当前节点的前一个节点
        pre = None
        while cur.next is not self.__head:
            if cur.item == item:
                if pre is None:  # 删除的是头节点,需要让尾节点指向新的头
                    temp = self.__head
                    while temp.next is not self.__head:
                        temp = temp.next
                    self.__head = cur.next
                    temp.next = self.__head
                else:
                    pre.next = cur.next
            pre = cur
            cur = cur.next
        # while循环处理不了尾节点,结束后单独处理尾节点
        if cur.item == item:
            if pre is None:  # 链表只有一个节点,且删除的就是这个节点
                self.__head = None
            else:
                pre.next = self.__head

    def search(self, item):
        """查找节点是否存在"""
        if self.is_empty():
            return False
        # 遍历,查找每一个节点
        cur = self.__head
        # 处理头节点
        if cur.item == item:
            return True
        # while 遍历第二个一直到最后一个
        while cur.next is not self.__head:
            cur = cur.next
            if cur.item == item:
                return True
        return False

猜你喜欢

转载自blog.csdn.net/liming066/article/details/85244314