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

Source: Internet
Author: User
Tags prev

This paper illustrates the data structure of Python and the linked list of algorithms. Share to everyone for your reference. The specific analysis is as follows:

I. Overview

A 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 chain list, two-way linked list, two-way circular chain list and so on. The structure of one-way linked lists and one-way cyclic lists is shown in the following illustration:

Second, ADT

Only one-way cyclic list ADT are considered here, and other types of linked lists ADT very similar. The one-way cyclic list ADT (abstract data type) generally provides the following interfaces:

①sincyclinkedlist () Create a one-way circular chain list
②add (item) Inserts a data item into the linked list
③remove (item) Deleting data items in a linked list
④search (item) to find whether a data item exists in a linked list
⑤empty () to determine whether the linked list is empty
⑥size () returns the number of data items in a linked list

The schematic diagram of one-way cyclic list operation is as follows:

Three, Python implementation

Python's built-in type list is implemented by the C array, and the list is functionally closer to the vector of C + + (because the array size can be dynamically resized). As we all know, arrays are contiguous lists, lists are linked, and they are completely different in concept and structure, so the list cannot be used to implement a linked list.
In C + +, the "pointer + structure" is usually used to implement the linked list, while in Python, the "Reference + Class" can be used to implement the linked list. In the following code, the Sincyclinkedlist class represents a one-way loop list, and the node class represents one of the nodes in the 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 (s
  Elf, newdata): Self.__data = NewData def setnext (self, newnext): Self.__next = Newnext class Sincyclinkedlist: def __init__ (self): self.head = node (None) Self.head.setNext (self.head) def add (self, item): TEMP = node (it EM) Temp.setnext (Self.head.getNext ()) Self.head.setNext (temp) def remove (self, item): prev = Self.head W Hile prev.getnext ()!= self.head:cur = Prev.getnext () if cur.getdata () = = Item:prev.setNext (cur.getn
      Ext ()) prev = Prev.getnext () 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 count if __name__ = ' __main__ ': s = sincyclinkedlist () print (' s.empty () = =%s, s.size ( = =%s '% (S.empty (), S.size ()) S.add () S.add () print (' s.empty () =%s, s.size () = =%s '% (S.empty (), S.size ()
  ) Print (' is%s in s '% (' if s.search () Else ' not ',) "Print (' 4 is%s in s '% (' if S.search (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 ())

Run Result:

$ python sincyclinkedlist.py
s.empty () = = True, s.size () = = 0
s.empty () = = False, s.size () = = 2 is in
s
   
    4 isn't
in S s.empty () = = False, s.size () = = 2
s.empty () = = False, s.size () = = 1
   

I hope this article will help you with your Python programming.

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.