Python Data Structure linked list: one-way linked list (instance description)

Source: Internet
Author: User

Python Data Structure linked list: one-way linked list (instance description)

Unidirectional linked list

A one-way linked list is also called a single-chain table. It is the simplest form of a linked list. Each node contains two fields, one Information Field (element field) and one link field. This link points to the next node in the linked list, and the link field of the last node points to a null value.

The table element Field elem is used to store specific data.

The next link field is used to store the location of the next node (the identifier in python)

The Variable p points to the position of the head node (first node) of the linked list. Any node in the table can be found from p.

Node implementation

Class Node (object): "Node of a single-chain table" def _ init _ (self, item): # item stores the data element self. item = item # next is the ID of the next node self. next = None

Single-chain table operations

Whether the is_empty () linked list is empty

Length () linked list length

Traverse the entire linked list

Add (item) add element to the head of the linked list

Append (item) add elements at the end of the linked list

Insert (pos, item) specifies the position to add elements

Remove (item) delete a node

Search (item) to find whether a node exists

Single-chain table implementation

Class Singlepnkpst (object): "single-chain table" def _ init _ (self): self. _ head = None def is_empty (self): "determines whether the linked list is empty" "return self. _ head = None def length (self): "linked list length" # Starting from cur, it points to the header node cur = self. _ head count = 0 # The End Node points to None. When the end node does not reach the end, while cur! = None: count + = 1 # Move cur back to a node cur = cur. next return count def travel (self): "traversal chain table" cur = self. _ head while cur! = None: print (cur. item, end = '') cur = cur. next print ("")

Add element to the header

Def add (self, item): "add element in the header" "# create a node that saves the item value Node = node (item) # point the next link field of the new node to the header node, that is, the node where the _ head points. next = self. _ head # point the head _ head of the linked list to the new node self. _ head = nod

Add element at the end

Def append (self, item): "adding elements at the end" node = Node (item) # first, judge whether the linked list is empty. If it is an empty linked list, point _ head to the new node if self. is_empty (): self. _ head = node # if it is not empty, locate the end and point the next of the End node to the new node else: cur = self. _ head while cur. next! = None: cur = cur. next cur. next = node

Add element at specified position

Def insert (self, pos, item): "add element at specified position" "# If pos is before the first element, insert if pos in the execution header <= 0: self. add (item) # If the specified position exceeds the end of the linked list, insert epf pos> (self. length ()-1): self. append (item) # locate the specified location else: node = Node (item) count = 0 # pre is used to point to the previous location pos-1 at the specified location pos, start from the Start Node and move to the specified position pre = self. _ head while count <(pos-1): count + = 1 pre = pre. next # first point the next of the new node to the node at the insertion position. next = pre. next # point the next of the previous node to the new node pre. next = node

Delete a node

Def remove (self, item): "delete node" cur = self. _ head pre = None while cur! = None: # The specified element if cur is found. item = item: # if the first node is deleted, if not pre: # point the header pointer to the last node self of the header node. _ head = cur. next else: # point the next of the previous node to the previous node pre. next = cur. next break else: # Move the node pre = cur. next

Check whether a node exists

Def search (self, item): "check whether a node exists in the linked list, and return True or False" cur = self. _ head while cur! = None: if cur. item = item: return True cur = cur. next return False

The above python Data Structure linked list's one-way linked list (instance description) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.

Related Article

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.