Home  >  Article  >  Backend Development  >  How to implement a singly linked list using Python

How to implement a singly linked list using Python

WBOY
WBOYOriginal
2023-06-11 16:40:331333browse

Singly linked list is a common data structure that consists of a series of nodes, each node contains an element and a pointer to the next node. You can use classes to implement singly linked lists in Python.

First, define a node class that contains an element and a pointer to the next node:

class Node:
    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next_node = next_node

Among them, data represents the element of the node, and next_node represents the pointer to the next node. .

Next, define a singly linked list class, which contains a head node and some basic operation methods, such as inserting, deleting, searching and printing singly linked list operations:

class LinkedList:
    def __init__(self):
        self.head = Node()

    def insert(self, data):
        new_node = Node(data)
        current_node = self.head
        while current_node.next_node is not None:
            current_node = current_node.next_node
        current_node.next_node = new_node

    def delete(self, data):
        current_node = self.head
        previous_node = None
        while current_node is not None:
            if current_node.data == data:
                if previous_node is not None:
                    previous_node.next_node = current_node.next_node
                else:
                    self.head = current_node.next_node
                return
            previous_node = current_node
            current_node = current_node.next_node

    def search(self, data):
        current_node = self.head
        while current_node is not None:
            if current_node.data == data:
                return True
            current_node = current_node.next_node
        return False

    def print_list(self):
        current_node = self.head.next_node
        while current_node is not None:
            print(current_node.data)
            current_node = current_node.next_node

above In the code, the insert method inserts a new node into the end of the singly linked list. The delete method will delete the node where the specified element is located. The search method is used to find whether a node exists in a singly linked list. The print_list method is used to print the entire singly linked list.

Finally, we can test our singly linked list class:

linked_list = LinkedList()
linked_list.insert(1)
linked_list.insert(2)
linked_list.insert(3)
linked_list.insert(4)

print(linked_list.search(3)) # True
print(linked_list.search(5)) # False

linked_list.delete(3)

linked_list.print_list() # 1 2 4

The above are the basic steps to implement a singly linked list using Python. It can be seen that Python is characterized by being simple and easy to understand, with a small amount of code and easy to read and understand, which makes Python a programming language very suitable for implementing data structures.

The above is the detailed content of How to implement a singly linked list using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn