链表

一、单向链表

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

  • 元素域elem用来存放具体的数据
  • 链接域next用来存放下一个节点的位置(Python中的标识)
  • 变量head指向链表头节点(首节点)的位置,从head出发能找到表中的任意节点

代码实现:

class Node(object):
    """单向链表的节点"""
    def __init__(self, item):
        self.data = item  # data存放数据元素
        self.next = None  # next是下一个节点的标识


class SingleLinkList(object):
    """单向链表"""
    def __init__(self, node=None):
        self.__head = node

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

    def length(self):
        """测量链表长度"""
        # cursor游标,用来移动遍历节点
        cursor = self.__head
        # count记录数量
        count = 0
        while cursor:
            count += 1
            cursor = cursor.next
        return count

    def travel(self):
        """遍历整个链表"""
        cursor = self.__head
        while cursor:
            print(cursor.data)
            cursor = cursor.next

    def add(self, item):
        """链表头部添加元素"""
        node = Node(item)
        node.next = self.__head
        self.__head = node

    def append(self, item):
        """链表尾部添加元素"""
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            cursor = self.__head
            while cursor.next:
                cursor = cursor.next
            cursor.next = node

    def insert(self, position, item):
        """指定位置插入元素"""
        if position <= 0:
            self.add(item)
        elif position > self.length()-1:
            self.append(item)
        else:
            prior = self.__head
            count = 0
            while count < (position-1):
                count += 1
                prior = prior.next
            # 当循环退出后,prior指向position-1位置
            node = Node(item)
            node.next = prior.next
            prior.next = node

    def remove(self, item):
        """删除节点"""
        prior = None
        cursor = self.__head
        while cursor is not None:
            if cursor.data == item:
                # 判读此节点是否是头节点
                # 头节点
                if cursor == self.__head:
                    self.__head = cursor.next
                # 不是头节点
                else:
                    prior.next = cursor.next
                break
            else:
                prior = cursor
                cursor = cursor.next

    def search(self, item):
        """查找节点是否存在"""
        cursor = self.__head
        while cursor:
            if cursor.data == item:
                return True
            else:
                cursor = cursor.next
        return False


if __name__ == "__main__":
    ll = SingleLinkList()
    ll.append(1)
    ll.append(2)
    ll.append(3)
    ll.travel()
single_link_list.py

二、双向链表

代码实现:

class Node(object):
    """双向链表的节点"""
    def __init__(self, item):
        self.data = item  # data存放数据元素
        self.prev = None  # prev是前驱节点
        self.next = None  # next是后继节点


class DoubleLinkList(object):
    """双向链表"""
    def __init__(self, node=None):
        self.__head = node

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

    def length(self):
        """测量链表长度"""
        # cursor游标,用来移动遍历节点
        cursor = self.__head
        # count记录数量
        count = 0
        while cursor:
            count += 1
            cursor = cursor.next
        return count

    def travel(self):
        """遍历整个链表"""
        cursor = self.__head
        while cursor:
            print(cursor.data)
            cursor = cursor.next

    def add(self, item):
        """链表头部添加元素"""
        node = Node(item)
        node.next = self.__head
        self.__head = node
        node.next.prev = node

    def append(self, item):
        """链表尾部添加元素"""
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            cursor = self.__head
            while cursor.next:
                cursor = cursor.next
            cursor.next = node
            node.prev = cursor

    def insert(self, position, item):
        """指定位置插入元素"""
        if position <= 0:
            self.add(item)
        elif position > self.length()-1:
            self.append(item)
        else:
            cursor = self.__head
            count = 0
            while count < position:
                count += 1
                cursor = cursor.next
            # 当循环退出后,cursor指向position位置
            node = Node(item)
            node.next = cursor
            node.prev = cursor.prev
            cursor.prev.next = node
            cursor.prev = node

    def remove(self, item):
        """删除节点"""
        cursor = self.__head
        while cursor is not None:
            if cursor.data == item:
                # 判读此节点是否是头节点
                # 头节点
                if cursor == self.__head:
                    self.__head = cursor.next
                    if cursor.next is not None:
                        # 判断链表是否只有一个节点
                        cursor.next.prev = None
                # 不是头节点
                else:
                    cursor.prev.next = cursor.next
                    if cursor.next is not None:
                        cursor.next.prev = cursor.prev
                break
            else:
                cursor = cursor.next

    def search(self, item):
        """查找节点是否存在"""
        cursor = self.__head
        while cursor:
            if cursor.data == item:
                return True
            else:
                cursor = cursor.next
        return False


if __name__ == "__main__":
    dl = DoubleLinkList()
    dl.append(1)
    dl.append(2)
    dl.append(3)
    dl.travel()
double_link_list.py

 

posted @ 2019-04-06 02:08  就俗人一个  阅读(177)  评论(0编辑  收藏  举报