Use python to implement linked list operations and python to implement

Source: Internet
Author: User

Use python to implement linked list operations and python to implement

I. Concepts

Linked List is one of the most widely used data structures in computer science. It is one of the simplest data structures and also a relatively high-order data structure (such as SWAP, ring buffer and queue)

Simply put, a list is a collection of individual data through indexes. In C, this is called a pointer. For example, a data element can be composed of address elements, geographical elements, routing information, and other details. However, the element types in the linked list are the same, which is a special list.

A separate list element is called a node. These nodes are not stored in the memory in sequence like arrays. Instead, you can find these elements in different places of memory by pointing a node to another node. The last item in the list is represented by NIL, which is equivalent to None in python.

Here we will introduce two different lists: Single-Chain tables and double-chain tables. A node in the double-Link Table points to only the next element in the list. However, in the double-Link Table, the current node also points to the previous node. Therefore, a double-stranded table occupies more memory because it requires additional variables to store indexes.

Figure 1. Single-chain table

Figure 2: double-stranded table

A single-chain table can be queried sequentially from start to end, but it is not that easy in turn. However, no matter which node you start from, double-stranded tables are the same in any direction. Adding or deleting nodes in a single-link table takes only two steps, but in the double-link table, four steps are required.

However, python does not provide the same data structure as a double-stranded table, so we can create such a Data Structure ourselves.

Ii. Create a linked list using python

(1) define a node as a Data Structure

First, we define the node class as ListNode. When initializing the instance object, this class defines two instance variables, data is used to store the node value, and next is used to store the index of the next node, the following describes in detail the methods and attributes to be defined for the next node.

_ Init _ (): Initialize node self. data: the value of the storage node self. next: store the index has_value () pointing to the next node: Compare the current node value with other values

The preceding methods and attributes cover the basic attributes and behaviors of a node.

Listing1: The ListNode class

The simplest node class is created above, and the ListNode object is initialized below.

Listing2: initialize a node

Three independent nodes are created above.

(2) create a single-chain table class

Now we define a class named SingleLinkedList to manage our nodes. It includes the following methods:

_ Init _ (): initialization object list_length (): number of returned nodes output_list (): Output node value add_list_item (): Add a new node unordered_search () at the end of the list (): remove_list_item_by_id (): removes a node based on the node id.

The following describes these methods one by one.

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

Listing3: The SingleLinkedList class (part one)

 

(3) add nodes

Use add_list_item () to add list elements. First, check whether it is a ListNode instance. If not, create a new node. If the list is still empty, the node is treated as the header node. If it is not empty, the current node is directed to the next element (that is, the newly added node ). Add a new node to the list

Listing4: The SinglelinkedList class (part two)

The list_length () method calculates the number of nodes and returns the length of the list. In the loop list, self. next points to the next node in turn.

Listing5: The SingleLinkedList class (part three)

Output_list () is used to output new node values.

Listing6: The SingleLinkedList class (part four)

Next we initialize the instance track of SingleLinkedList and create four nodes.

(4) query the list

Use unordered_search () to query the entire list (). It requires an additional parameter to help query. The list header is the entry point.

(5) remove an element from the list

When a node is removed from the list, the index pointing to the node needs to be moved to the next node of the removed node. The removed node will be cleared by python's garbage collection mechanism.

Listing10: Removing a node by node number

 

(6) create a double-stranded table

Creating a double-linked table is actually creating a previous attribute on the basis of ListNode.

Listing11: Extended list node class

Then we can create a double-stranded table class based on the above definition.

Adding a new node with a single linked list is different.

Remove a node from a double-Link Table

Practical Use of python

Output result

 

(7) queue-based bidirectional list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.