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.