Python one-way circular linked list operation

Table of contents

1. One-way circular linked list

One-way circular linked list graph

 Second, the operation of one-way circular linked list

1. Determine whether the linked list is empty

2, the length of the linked list

3. Traverse the entire linked list

4. Add elements at the head of the linked list

5. Add elements at the end of the linked list

6. Insert an element at the specified position

7. Modify the element at the specified position

8. Delete elements

9. Find whether the element exists

3. Complete code


 related articles

Python one-way linked list operation

Operation of Python one-way circular linked list

Operation of Python doubly linked list

Operation of Python two-way circular linked list

 

1. One-way circular linked list

One-way circular linked list graph

There are two parts in the node node: elem (element) and next (pointer to the next node), the head pointer points to the head node, and the tail node next points to the head node

 Second, the operation of one-way circular linked list

1. Determine whether the linked list is empty

    def is_empty(self):
        # 链表是否为空
        if self.__head is None:
            return True
        else:
            return False


2, the length of the linked list

    def length(self):
        # 链表长度
        if self.is_empty():
            return 0
        count = 0
        cur = self.__head
        while cur.next != self.__head:
            count += 1
            cur = cur.next
        return count


3. Traverse the entire linked list

    def travel(self):
     # 遍历整个链表
        if self.is_empty():
            return
        cur = self.__head
        while cur.next != self.__head:
            print(cur.elem, end=' ')
            cur = cur.next
        print(cur.elem)


4. Add elements at the head of the linked list

    def add(self, item):
        # 链表头部添加元素
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            tail = self.__head
            # 循环找到尾结点
            while tail.next != self.__head:
                tail = tail.next
            # 新来的节点的next指向第一个节点
            node.next = self.__head
            # 再改变第一个节点的指针指向新节点
            self.__head = node
            # 最后将尾指向指向添加node节点
            tail.next = node


5. Add elements at the end of the linked list

    def append(self, item):
        # 链表尾部添加元素
        # 创建新结点
        node = Node(item)
        # 是空链表就把头节点指向这个节点
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            tail = self.__head
            # 找到尾节点
            while tail.next != self.__head:
                tail = tail.next
            tail.next = node
            node.next = self.__head


6. Insert an element at the specified position


Traversal let the pointer point to the position to be inserted, and modify the pointing of the pointer before and after

    def insert(self, pos, item):
        # 位置pos在第一个元素之前,则在头部插入
        if pos <= 0:
            self.add(item)
            # 位置pos大于总长度,则在尾部插入
        elif pos > self.length():
            self.append(item)
        else:
            # 指定位置添加元素
            node = Node(item)
            count = 0
            pre = self.__head
            # 循环定位指针位置
            while count < (pos-1):
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

7. Modify the element at the specified position
 

    def modify(self, pos, item):
        """修改指定位置的元素"""
        # 当指定的位置pos小于等于0时,则修改头部元素
        if pos <= 0:
            self.__head.elem = item
        # 当pos大于等于链表总长度时,则修改尾部元素
        elif pos >= self.length():
            tail = self.__head
            # 循环让指针指向尾部元素
            while tail.next != self.__head:
                tail = tail.next
            tail.elem = item  # 修改尾部元素
        else:
            count = 0
            tail = self.__head
            # 循环指针找到指定的位置
            while count < pos:  # 1.当不满足条件退出循环时,说明指针已经指向了给定的pos位置
                tail = tail.next
                count += 1
            tail.elem = item  # 2.将pos位置的元素修改

8. Delete elements
 

    def remove(self, item):
        # 删除节点
        cur = self.__head  # cur当前指针
        pre = None  # 前一个指针
        while cur.next != self.__head:
            # 找到了要删除的元素
            if cur.elem == item:
                # 要删除的元素就是第一个元素
                if cur == self.__head:
                    tail = self.__head
                    # 让tail指向最后一个元素
                    while tail.next != self.__head:
                        tail = tail.next
                    # 删除第一个节点,改变指针的指向
                    tail.next = cur.next
                    self.__head = cur.next
                else:
                    pre.next = cur.next
                return
            # 未找到要删除的元素,指针向后走,继续遍历
            else:
                pre = cur
                cur = cur.next
        # 当上面的while循环不满足条件时(cur.next == self.__head),说明只有一个结点元素
        if cur.elem == item:
            if cur.next == self.__head:
                pre.next = self.__head

9. Find whether the element exists
 

    def search(self, item):
        # 查找节点是否存在
        cur = self.__head
        while cur.next != self.__head:
            # 找到了返回True,未回到指向下一个继续遍历
            if cur.elem == item:
                return True
            cur = cur.next
        # 查找的元素在最后一个,遍历后指向最后一个,但是没有进入循环,所以需要在循环体外判断一次
        if cur.elem == item:
            return True
        return False

3. Complete code

class Node():
    def __init__(self, elem):
        # 单链表结点
        self.elem = elem
        self.next = None


class SingleCircleLinkList():
    def __init__(self, node=None):
        self.__head = node
        if node:
            node.next = node  # 只有一个结点时,next指针指向自己,构成循环

    def is_empty(self):
        # 链表是否为空
        if self.__head is None:
            return True
        else:
            return False

    def length(self):
        # 链表长度
        if self.is_empty():
            return 0
        count = 0
        cur = self.__head
        while cur.next != self.__head:
            count += 1
            cur = cur.next
        return count

    def travel(self):
     # 遍历整个链表
        if self.is_empty():
            return
        cur = self.__head
        while cur.next != self.__head:
            print(cur.elem, end=' ')
            cur = cur.next
        print(cur.elem)

    def add(self, item):
        # 链表头部添加元素
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            tail = self.__head
            # 循环找到尾结点
            while tail.next != self.__head:
                tail = tail.next
            # 新来的节点的next指向第一个节点
            node.next = self.__head
            # 再改变第一个节点的指针指向新节点
            self.__head = node
            # 最后将尾指向指向添加node节点
            tail.next = node

    def append(self, item):
        # 链表尾部添加元素
        # 创建新结点
        node = Node(item)
        # 是空链表就把头节点指向这个节点
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            tail = self.__head
            # 找到尾节点
            while tail.next != self.__head:
                tail = tail.next
            tail.next = node
            node.next = self.__head

    def modify(self, pos, item):
        """修改指定位置的元素"""
        # 当指定的位置pos小于等于0时,则修改头部元素
        if pos <= 0:
            self.__head.elem = item
        # 当pos大于等于链表总长度时,则修改尾部元素
        elif pos >= self.length():
            tail = self.__head
            # 循环让指针指向尾部元素
            while tail.next != self.__head:
                tail = tail.next
            tail.elem = item  # 修改尾部元素
        else:
            count = 0
            tail = self.__head
            # 循环指针找到指定的位置
            while count < pos:  # 1.当不满足条件退出循环时,说明指针已经指向了给定的pos位置
                tail = tail.next
                count += 1
            tail.elem = item  # 2.将pos位置的元素修改

    def insert(self, pos, item):
        # 位置pos在第一个元素之前,则在头部插入
        if pos <= 0:
            self.add(item)
            # 位置pos大于总长度,则在尾部插入
        elif pos > self.length():
            self.append(item)
        else:
            # 指定位置添加元素
            node = Node(item)
            count = 0
            pre = self.__head
            # 循环定位指针位置
            while count < (pos-1):
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    def remove(self, item):
        # 删除节点
        cur = self.__head  # cur当前指针
        pre = None  # 前一个指针
        while cur.next != self.__head:
            # 找到了要删除的元素
            if cur.elem == item:
                # 要删除的元素就是第一个元素
                if cur == self.__head:
                    tail = self.__head
                    # 让tail指向最后一个元素
                    while tail.next != self.__head:
                        tail = tail.next
                    # 删除第一个节点,改变指针的指向
                    tail.next = cur.next
                    self.__head = cur.next
                else:
                    pre.next = cur.next
                return
            # 未找到要删除的元素,指针向后走,继续遍历
            else:
                pre = cur
                cur = cur.next
        # 当上面的while循环不满足条件时(cur.next == self.__head),说明只有一个结点元素
        if cur.elem == item:
            if cur.next == self.__head:
                pre.next = self.__head

    def search(self, item):
        # 查找节点是否存在
        cur = self.__head
        while cur.next != self.__head:
            # 找到了返回True,未回到指向下一个继续遍历
            if cur.elem == item:
                return True
            cur = cur.next
        # 查找的元素在最后一个,遍历后指向最后一个,但是没有进入循环,所以需要在循环体外判断一次
        if cur.elem == item:
            return True
        return False


if __name__ == '__main__':
    ll = SingleCircleLinkList()
    print(ll.is_empty())
    print(ll.length())
    ll.travel()

    print('add')
    ll.add(9)
    ll.travel()
    ll.add(10)
    ll.travel()

    print('append')
    ll.append(13)
    ll.travel()

    print('insert')
    ll.insert(5, 33)
    ll.travel()

    print('remove')
    ll.travel()
    ll.remove(10)
    ll.travel()

    print('modify')
    ll.modify(2, 100)
    ll.travel()

    print(ll.search(33))

Guess you like

Origin blog.csdn.net/qq_37140721/article/details/130286404