I. Concept sorting

Linked lists are one of the most widely used data structures in computer science. It is one of the simplest data structures, as well as higher-order data structures (such as stacks, ring buffers, and queues)

Simply put, a list is a collection of single data by index. In C this is called a pointer. For example, a data element can consist of address elements, geographic elements, routing information, transaction details, and so on. But the elements in a list are the same type, it’s a special kind of list.

A single list element is called a node. Unlike arrays, these nodes are not stored sequentially in memory. Instead, you can find the elements in different parts of memory using Pointers from one node to another. The last item in the list is NIL, which is the python equivalent of None

\

Here are two different types of lists — singly and doubly linked. A node in a double-linked list only points to the next element in the list, but in a double-linked list, the current node also points to the previous node. So double-linked lists take up more memory because they require extra variables to store the index. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can contact Wei: 762459510, that is really good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~

\

Figure 1. Single linked list

\

Figure 2: Double linked list

Singly linked lists can be queried from beginning to end, but the reverse is not so easy. However, a double-linked list does not matter which node you start from, and queries in any direction are the same. Adding and removing nodes takes only two steps in a singly linked list, but four steps in a doubly linked list.

But there is no data structure like a double-linked list in Python, so we can create one ourselves

If you use Python to create a linked list

(1). Define the node as a data structure

First of all, we define the node class as ListNode. When initializing the instance object, this class defines two instance variables. Data is used to store the value of the node, and next is used to store the index of the next node

Init () : initializes a node

Self. data: the value of the storage node

Self. next: Stores the index to the next node

Has_value () : compares the current node value with other values

The methods and properties above cover the basic properties and behavior a node should have

 Listing1:The ListNode class

After creating the simplest node class above, initialize the ListNode object

Listing2: Initializes the node

Three separate nodes are created above

(2) Create a single linked list class

Now we define a class called SingleLinkedList to manage our node, which contains the following methods:

Init () : Initializes an object

List_length () : Returns the number of nodes

Output_list (): Outputs node values

Add_list_item (): Adds a new node to the end of the list

Unordered_search (): Queries the list based on a special value

Remove_list_item_by_id () : Removes a node based on its ID

These methods are explained below

Init () defines head and tail, both initialized to None

 Listing3:The SingleLinkedList class(part one)

(3) Add a node

Add list elements with add_list_item(). Check to see if it is an instance of ListNode. If not, create a new node. If the list is still empty, the node is treated as the head node, and if it is not empty, the current node points to the next element (the newly added node). Add the new node to the list

Listing4:The SinglelinkedList class(part two)

The list_Length () method counts the number of nodes and returns the length of the list. Loop through the list in a loop, self.next pointing to the next node in turn

Listing5:The SingleLinkedList class(part three)

Output_list () is used to print the new node value

 Listing6:The SingleLinkedList class(part four)

Next we initialize the SingleLinkedList instance Track and create four nodes.

(4) Query list

Query the entire list using unordered_search(). It requires an extra parameter to aid the query. The head of the list is the pointcut.

(5) Remove an element from the list

When a node is removed from the list, the index pointing to that node needs to be moved to the next node of the removed node. The removed nodes are removed by Python’s garbage collection mechanism

Listing10:Removing a node by node number

(6) Create a double-linked list

To create a double linked list is to create a previous property based on the ListNode

Listing11: Extended List Node class

We can then create a new double-linked list class based on the above definition

Adding new nodes is different from a singly linked list

Remove a node from a double-linked list

Practical use of Python

The output

(7) Use queue to achieve bidirectional list

Materials video tutorial collection method:

1. Like + Comment (check “Simultaneously forward”)

2, pay attention to xiaobian. And private message reply keyword [information] (must be private message oh ~ click my avatar can see the private message button)