Python implementation of data structure and algorithm of the linked list of detailed

Source: Internet
Author: User
In this paper, we describe the data structure and the linked list of algorithms implemented by Python. Share to everyone for your reference. The specific analysis is as follows:

I. Overview

A linked list (linked list) is a collection of data items, where each data item is part of a node, and each node also contains a link to the next node.
According to the different structure, the linked list can be divided into one-way linked list, one-way circular linked list, two-way linked list, two-way circular chain list. Where the structure of the unidirectional list and the unidirectional circular list is as shown:

Second, ADT

Only one-way circular list ADT is considered, other types of linked list ADT are similar. Unidirectional circular linked list ADT (abstract data type) generally provides the following interfaces:

①sincyclinkedlist () Creating a one-way circular link list
②add (item) Inserts a data item into the linked list
③remove (item) to delete data items in a linked list
④search (item) to find the existence of a data item in a linked list
⑤empty () Determine if the linked list is empty
⑥size () returns the number of data items in the linked list

One-way circular list operation is as follows:

Third, Python implementation

Python's built-in type list is implemented by the C array, and the list is functionally closer to the C + + vector (because the array size can be dynamically resized). As we all know, arrays are sequential lists, linked lists, which are completely different in concept and structure, so list cannot be used to implement a linked list.
The "pointer + struct" is usually used in C/C + + to implement the linked list, whereas in Python you can use "Reference + class" to implement the linked list. In the following code, the Sincyclinkedlist class represents a one-way loop linked list, and the node class represents one of the nodes in the linked list:

#!/usr/bin/env python#-*-coding:utf-8-*-class node:def __init__ (self, initdata): Self.__data = InitData self._ _next = None def getData (self): return self.__data def getNext (self): return Self.__next def setData (self, Newdat a): Self.__data = NewData def setnext (self, newnext): Self.__next = newnextclass sincyclinkedlist:def __init__ (SE LF): Self.head = node (None) Self.head.setNext (self.head) def add (self, item): TEMP = node (item) Temp.setnext ( Self.head.getNext ()) Self.head.setNext (temp) def remove (self, item): prev = Self.head while Prev.getnext ()! = SE Lf.head:cur = Prev.getnext () if cur.getdata () = = Item:prev.setNext (Cur.getnext ()) prev = Prev.getn        Ext () def search (self, item): cur = self.head.getNext () while cur! = self.head:if cur.getdata () = = Item: return True cur = cur.getnext () return False def empty (self): return self.head.getNext () = = Self.head def Size (self): count = 0    cur = self.head.getNext () while cur! = self.head:count + 1 cur = cur.getnext () return countif __name __ = = ' __main__ ': s = sincyclinkedlist () print (' s.empty () = =%s, s.size () = =%s '% (S.empty (), S.size ())) (S.add ()) s.  Add (s.empty () = =%s, s.size () = =%s '% (S.empty (), S.size ())) print (' is%s in s '% (' if ') else  ' not ',)) print (' 4 is%s in s '% ("if (4) Else ' not ',)) print (' s.empty () = =%s, s.size () = =%s '% (S.empty (), S.size ()) S.remove () print (' s.empty () = =%s, s.size () = =%s '% (S.empty (), S.size ()))

Operation Result:

$ python sincyclinkedlist.pys.empty () = = True, s.size () = = 0s.empty () = = False, s.size () = = 286 are in S4 are not in ss.empt Y () = = False, s.size () = = 2s.empty () = = False, s.size () = = 1

Hopefully this article will help you with Python programming.

  • 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: 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.