This paper illustrates the two-terminal queue of data structure and algorithm implemented by Python. Share to everyone for your reference. The specific analysis is as follows:
I. Overview
A two-terminal queue (deque, full name double-ended queue) is a linear data structure with a queue and stack nature. The two-terminal queue also has both ends: the head of the team (front), the tail (rear), but unlike the queue, the insert operation at both ends (team head and Team tail) can be done, the deletion is the same.
Second, ADT
The two-terminal queue ADT (abstract data type) generally provides the following interfaces:
①deque () Create a two-terminal queue
②addfront (item) insert item to Team Head
③addrear (item) Inserts an item to the end of the team
④removefront () returns the entry for the first team and deletes the item from the two-terminal queue
⑤removerear () returns the item at the end of the team and deletes the item from the two-terminal queue
⑥empty () to determine if the two-terminal queue is empty
⑦size () returns the number of items in a two-terminal queue
The schematic diagram of the two-terminal queue operation is as follows:
Three, 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 a two-terminal queue in Python).
The difference between the two modes is mainly embodied in the performance (Specific reference Collections.deque | timecomplexity):
Operation | realization way list collections.deque
-----------------------------------------
addfront O (n) O (1)
-----------------------------------------
addrear O (1) O (1)
------------------ -----------------------
removefront o (n) O (1)
-----------------------------------------
Removerear O (1) O (1)
1, the use of 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, the use of standard library Collections.deque
#!/usr/bin/env python
#-*-coding:utf-8-*-from
collections import Deque
class 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)
Four, the application
Palindrome (palindrome) is the same word or sentence that is read and reversed, and it is a rhetorical method and a word game.
English Example:
Madam
Able was I ere I saw Elba
Chinese Example:
Flowers not flowers
Everyone is for me, I am for all
If you want to implement a palindrome verification algorithm (verify that a given string is a palindrome), using the Deque class will be very easy: store the string to a two-terminal queue, take out both the end-and-tail characters and compare equality, as long as there are a pair of characters, the string is not a palindrome, if all equal, then 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 aStr ing:
chardeque.addrear (CH) while
chardeque.size () > 1: a
= Chardeque.removefront () Last
= Chardeque.removerear ()
If I!= last: return
False to
True
if __name__ = = ' __main__ ':
STR1 = ' Able I ere I saw Elba '
print ('%s ' is%s palindrome '% (str1, ' if Palchecker (str1) Else ' not '))
St r2 = U ' Everyone for me, I for everyone '
print (U ' '%s '%s is a palindrome '% (str2, U ' if Palchecker (str2) Else U ' no ')
STR3 = u "What ' wrong" What's up
print (U '%s '%s is palindrome '% (str3, U ' if Palchecker (STR3) Else U ' no '))
Run Result:
$ python palchecker.py
"Able I ere I saw Elba"
is palindrome "Everyone for me, I for everyone" is a palindrome
"What's wrong What's wrong" is not a palindrome
I hope this article will help you with your Python programming.