One-way cyclic linked list function example implemented by Python, python implementation example

Source: Internet
Author: User

One-way cyclic linked list function example implemented by Python, python implementation example

This example describes the one-way circular linked list function implemented by Python. We will share this with you for your reference. The details are as follows:

Overview:

A one-way cyclic linked list refers to a single-chain table. The last element of the table points to the head node of the linked list and is no longer empty.

The figure shows that the judgment condition of the one-way cyclic linked list is no longer that the table is empty, but whether the table is to the header.

Operation

Is_empty () determines whether the linked list is empty
Length () returns the length of the linked list.
Travel () Traversal
Add (item) add a node in the header
Append (item) adds a node at the end
Insert (pos, item) add nodes to pos at the specified position
Remove (item) delete a node
Search (item) to find whether a node exists

Code:

Class Node (object): "Node" def _ init _ (self, item): self. item = item self. next = Noneclass sincyclic kedlist (object): "unidirectional cyclic linked list" def _ init _ (self): self. _ head = None def is_empty (self): "checks whether the linked list is empty" "return self. _ head = None def length (self): "returns the length of the linked list" "# if the linked list is empty, return the length 0 if self. is_empty (): return 0 count = 1 cur = self. _ head while cur. next! = Self. _ head: count + = 1 cur = cur. next return count def travel (self): "traversal chain table" if self. is_empty (): return cur = self. _ head print cur. item, while cur. next! = Self. _ head: cur = cur. next print cur. item, print "" def add (self, item): "add node in the header" Node = node (item) if self. is_empty (): self. _ head = node. next = self. _ head else: # The added node points to _ head node. next = self. _ head # Move to the end of the linked list and point next of the tail node to node cur = self. _ head while cur. next! = Self. _ head: cur = cur. next cur. next = node # _ head points to the self. _ head = node def append (self, item): "add nodes at the end" node = Node (item) if self. is_empty (): self. _ head = node. next = self. _ head else: # Move to the end of the linked list. cur = self. _ head while cur. next! = Self. _ head: cur = cur. next # point the end node to node cur. next = node # point the node to the header node _ head node. next = self. _ head def insert (self, pos, item): "add a node at a specified position" if pos <= 0: self. add (item) elif pos> (self. length ()-1): self. append (item) else: node = Node (item) cur = self. _ head count = 0 # Move to the previous position of the specified position while count <(pos-1): count + = 1 cur = cur. next node. next = cur. next cur. next = node def remove (self, item): "" Delete Except for a node "" # if the linked list is empty, if self is directly returned. is_empty (): return # point cur to the header node cur = self. _ head pre = None # if the element of the header node is the item if cur. item = item: # if the linked list has more than one node if cur. next! = Self. _ head: # first locate the end node and point next of the End Node to the second node while cur. next! = Self. _ head: cur = cur. next # cur points to the End Node cur. next = self. _ head. next self. _ head = self. _ head. next else: # The linked list has only one node self. _ head = None else: pre = self. _ head # The first node is not the while cur. next! = Self. _ head: # locate the element to be deleted if cur. item = item: # delete pre. next = cur. next return else: pre = cur. next # cur points to the end node if cur. item = item: # delete pre at the end. next = cur. next def search (self, item): "Find whether a node exists" if self. is_empty (): return False cur = self. _ head if cur. item = item: return True while cur. next! = Self. _ head: cur = cur. next if cur. item = item: return True return Falseif _ name _ = "_ main _": ll = sincyclic kedlist () ll. add (1) ll. add (2) ll. append (3) ll. insert (2, 4) ll. insert (4, 5) ll. insert (0, 6) print "length:", ll. length () ll. travel () print ll. search (3) print ll. search (7) ll. remove (1) print "length:", ll. length () ll. travel ()

Running result:

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.