A list of data structures and algorithms implemented by Python

Source: Internet
Author: User
This article mainly introduces the data structure implemented by Python and the linked list of algorithms. it analyzes the concept and definition of the linked list in detail and the skills related to Python implementation and use of the linked list, which is of great practical value, for more information about the data structure and algorithm linked list implemented by Python, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

I. Overview

A linked list is a collection of data items. each data item is a part of a node, and each node also contains a link pointing to the next node.
Depending on the structure, the linked list can be divided into one-way linked list, one-way cyclic linked list, two-way linked list, two-way cyclic linked list, and so on. The structure of one-way linked list and one-way cyclic linked list is shown in:

II. ADT

Here we only consider the one-way cyclic linked list ADT. The ADT of other types of linked lists is similar. The one-way cyclic linked list ADT (abstract data type) generally provides the following interfaces:

① Create a one-way cyclic linked list using sincylinkedlist ()
② Add (item) insert data items into the linked list
③ Remove (item) delete data items in the linked list
④ Search for the existence of a data item in the linked list
⑤ Empty () determines whether the linked list is empty
6. size () returns the number of data items in the linked list.

One-way cyclic linked list operation is as follows:

III. Python implementation

The underlying layer of the Python built-in list type is implemented by the C array. the list function is closer to the vector of C ++ (because the array size can be dynamically adjusted ). As we all know, arrays are continuous lists, and linked lists are linked lists. The two are completely different in concept and structure. Therefore, list cannot be used to implement a linked list.
In C/C ++, the "pointer + struct" is usually used to implement the linked list. in Python, the "reference + class" can be used to implement the linked list. In the following code, the sincylinkedlist class represents a one-way cyclic linked list, and the Node class represents a Node 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, newdata):    self.__data = newdata  def setNext(self, newnext):    self.__next = newnextclass SinCycLinkedlist:  def __init__(self):    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() != self.head:      cur = prev.getNext()      if cur.getData() == item:        prev.setNext(cur.getNext())      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 countif __name__ == '__main__':  s = SinCycLinkedlist()  print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))  s.add(19)  s.add(86)  print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))  print('86 is%s in s' % ('' if s.search(86) 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(19)  print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))

Running result:

$ python sincyclinkedlist.pys.empty() == True, s.size() == 0s.empty() == False, s.size() == 286 is in s4 is not in ss.empty() == False, s.size() == 2s.empty() == False, s.size() == 1

I hope this article will help you with 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.