Python implementation of data structure and algorithm of the two-end queue detailed

Source: Internet
Author: User
In this paper, the two-terminal queue of data structure and algorithm implemented by Python is described. Share to everyone for your reference. The specific analysis is as follows:

I. Overview

The double-ended queue (deque, full name double-ended queue) is a linear data structure with queue and stack properties. The double-ended queue also has two ends: the team head (front), the tail (rear), but unlike the queue, the insert operation can be done at both ends (team head and tail), and the delete operation is the same.

Second, ADT

The two-terminal queue ADT (abstract data type) generally provides the following interfaces:

①deque () Create a double-ended queue
②addfront (item) insert item to first Team
③addrear (item) Inserts an item to the end of the queue
④removefront () returns the entry for the first team and removes the item from the double-ended queue
⑤removerear () returns the item at the end of the queue and removes the item from the double-ended fleet
⑥empty () Determine if the double-ended queue is empty
⑦size () returns the number of items in a double-ended queue

The double-ended queue operation is as follows:

Third, Python implementation

In Python, there are two ways to achieve the above two-terminal queue ADT: Using the built-in type list, using the standard library collections.deque (in fact, Collections.deque is the standard implementation of the double-ended queue in Python).

The difference between the two methods is mainly reflected in the performance (Specific reference Collections.deque | timecomplexity):

Operation | Implementation mode  list  collections.deque-----------------------------------------addfront    o (n)  O (1)----- ------------------------------------addrear     O (1)  O (1)----------------------------------------- Removefront   o (n)  O (1)-----------------------------------------removerear   O (1)  O (1)

1. Use the built-in type list

#!/usr/bin/env python#-*-coding:utf-8-*-class Deque:  def __init__ (self):    self.items = []  def Addfront (Self, item):    self.items.insert (0, item)  def addrear (self, item):    self.items.append (item)  def Removefront (self):    return self.items.pop (0)  def removerear (self):    return Self.items.pop ()  def Empty (self):    return self.size () = = 0  def size (self):    return len (self.items)

2. Using standard library Collections.deque

#!/usr/bin/env python#-*-coding:utf-8-*-from Collections Import Dequeclass Deque:  def __init__ (self):    Self.items = Deque ()  def addfront (self, item):    self.items.appendleft (item)  def addrear (self, item):    Self.items.append (item)  def removefront (self):    return Self.items.popleft ()  def removerear (self):    Return Self.items.pop ()  def empty (self):    return self.size () = = 0  def size (self):    return Len ( Self.items)

Iv. Application

A palindrome (palindrome) is the same word or sentence that is read in reverse, is a rhetorical way and a word game.

English Example:

Madam
Able was I ere I saw Elba

Chinese Example:

Flowers not flowers
Everyone for me, I for everyone
If you want to implement a palindrome validation algorithm (to verify that a given string is a palindrome), using the Deque class will be very easy: store the string in a double-ended queue, remove the last and last characters and compare equality, as long as there is a pair of characters, the string is not a palindrome, if all equal, the string is a palindrome. The specific code is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-def palchecker (astring):  chardeque = Deque () for  ch in astring:    C Hardeque.addrear (CH) while  chardeque.size () > 1: First    = Chardeque.removefront () Last    = Chardeque.removerear ()    if first! = Last:      return False  return trueif __name__ = = ' __main__ ':  str1 = ' able Was I ere I saw Elba '  print (' "%s" is%s palindrome '% (str1, ' if Palchecker (str1) Else ' not '))  str2 = U ' Everybody for me, ME For everyone '  print (U '%s '%s ' is palindrome '% (str2, U ' if Palchecker (str2) Else U ' not '))  STR3 = u "What's Wrong"  Print (U ' "% S "%s is palindrome '% (str3, U ' if Palchecker (STR3) Else U ' not '))

Operation Result:

$ python palchecker.py "Able was I ere I saw Elba" is palindrome "everyone for me, I for everyone" is a palindrome "What's the wrong" is not a palindrome

Hopefully 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.