A detailed description of the use of the Python data structure and the list of algorithms

Source: Internet
Author: User
This article mainly introduces the Python data structure and algorithm of the chain list definition and usage, combined with specific examples of the form of a more detailed analysis of the single-linked list, circular chain list, such as the definition, use of methods and related considerations, the need for friends can refer to the next

This paper describes the definition and usage of the linked list of Python data structure and algorithm. Share to everyone for your reference, as follows:

This article will explain to you:

(1) Starting from the definition of the linked list node, the design of the linked list is carried out in the way of class and object-oriented thought.

(2) The boundary conditions to be considered when implementing member functions such as inserting and deleting linked list classes.
Prepend (head insertion), pop (head delete), append (trailing insert), pop_last (trailing delete)

2.1 Insert:

Empty linked list
Linked list length is 1
Insert to end

2.2 Delete

Empty linked list
Linked list length is 1
Delete End Element

(3) A variety of variants from a single linked list to a single linked list:

Single-linked list with tail node
Circular single-linked list
Double linked list

1. Definition of linked list nodes


Class Lnode:def __init__ (self, Elem, next_=none):  Self.elem = elem  Self.next = Next_

2. Implementation of single-link list

Focus on understanding the implementation of insertions, deletions, and the boundary conditions that need to be considered:


Class Linkedlistunderflow (ValueError): Passclass llist:def __init__ (self):  self._head = None def is_empty (self): C1/>return Self._head is None def prepend (self, elem):  self._head = Lnode (Elem, Self._head) def pop (self):  if self . _head is None:   raise Linkedlistunderflow (' in pop ')  e = Self._head.elem  self._head = Self._head.next  Return e def append (self, elem):  if Self._head is None:   self._head = Lnode (elem)   return  p = self._head< C12/>while P.next is isn't None:   p = p.next  p.next = Lnode (elem) def pop_last (self):  if Self._head is none:
  raise Linkedlistunderflow (' in Pop_last ')  p = self._head  if P.next is None:   e = P.elem   self._ Head = None   return e while p.next.next are not  None:   p = p.next  e = p.next.elem  p.next = none
  
   return E
  

Simple summary:

(0) The premise of being able to access P.next.next is that p.next is not empty;
(1) Tail insertion, if the list is not empty, need and only need to change the tail node pointer;
(2) Tail Delete, if the list length is not empty, need and only need to change is the second-to-last node pointer.

Simple deformation of a single-linked list: single-linked list with tail nodes


Class LList1 (llist): def __init__ (self):  llist.__init__ (self)  self._rear = None ...

All we need to rewrite is: the insertion of the head, the insertion of the tail, the deletion of the tail


Def prepend (self, elem): If Self._head is None:  self._head = Lnode (elem)  self._rear = Self._head else:  self._  Head = Lnode (Elem, Self._head) def append (self, elem): If Self._head is None:  self._head = Lnode (elem)  self._rear = Self._head else:  self._rear.next = Lnode (elem)  self._rear = Self._rear.nextdef pop_last (self): if Self._head  Is none:  raise Linkedlistunderflow (' in Pop_last ') p = Self._head If P.next is none:  e = P.elem  self._head = None  return e while p.next.next are not None:  p = p.next E = P.next.elem self._rear = P P.next = None return E

Single-linked list variants: Circular single-linked list


Class Lclist:def __init__ (self):  self._rear = None def prepend (self, elem):  if Self._rear is None:   Self._rea R = Lnode (elem)   self._rear.next = self._rear  else:   self._rear.next = Lnode (Elem, Self._rear.next) def Append (self, elem):  self.prepend (elem)  self_rear = Self._rear.next def pops (self):  if Self._rear is None:   raise Linkedlistunderflow (' in POPs ')  p = Self._rear.next  if P is none:   self._rear = None  else:< C14/>self._rear.next = P.next  return P.elem def printall (self):  if Self._rear is None:   raise ...  p = self._rear.next while  True:   print (P.elem)   if P was self._rear:    break   p = P.next

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.