Deque Dque

Concept data structure deque

  the deque (also referred deque) is similar to the ordered set of queue entries. It has two end portions, the first portion and the tail, and are unchanged in the collection.

  deque special is that add and delete items are non-limiting. You can add a new item in the front or back. Similarly, existing items can be removed from either end. In a sense, the hybrid linear structure provides a single stack data structure and the ability of all queues.  

Deque abstract data type definitions: Deque abstract data types should be defined by the following structure and operation. Which can add and remove elements from either end of the head or tail. Deque follows :
  • Dque () to create a new, empty deque. It takes no arguments and returns an empty deque.
  • lpush (item) will add a new item to the header of the deque. It takes the item parameter does not return anything.
  • rpush (item) will add a new item to the end of the deque. It takes the item parameter does not return anything.
  • lpop () to delete the first item in the deque. It takes no arguments and returns the item. deque is modified.
  • rpop () delete the item from the tail of the deque. It takes no arguments and returns the item. deque is modified.
  • Whether isEmpty () Test deque is empty. It takes no arguments and returns a Boolean value.
  • size () Returns the number of items in the deque. It takes no parameters and returns an integer.  

Defined deque  

 1 class Deque(object):
 2     def __init__(self):
 3         self.deque = []
 4 
 5     def __str__(self):
 6         return str(self.deque)
 7 
 8     def rpush(self, item):
 9         self.deque.append(item)
10 
11     def lpush(self, item):
12         self.deque.insert(0, item)
13 
14     def rpop(self):
15         return self.deque.pop() if self.deque else "Dequeue is empty!"
16 
17     def lpop(self):
18         return self.deque.pop(0) if self.deque else "Dequeue is empty!"
19 
20     def isEmpty(self):
21         return self.deque == []
22 
23     def size(self):
24 return len (self.deque)

Deque application case - Palindrome inspection

Palindrome detection: design procedures to detect whether a string is a palindrome.

- Palindrome: palindrome is a string, reading the same first and last characters, e.g., radar toot madam.

- Analysis: The solution to this problem would be to use deque to store character strings. We deal with string from left to right, and add each character to the end of the deque. At this point, deque like a regular queue. However, we can now take advantage of the dual function deque. The first part of the first character string stored in the deque, deque tail saves the last character. We can delete and compare last characters, only when they match to continue. If a match can last and last characters, we end up either run out of characters, or leave out the size of the deque 1, depending on the length of the original string is even or odd. In either case, the strings are palindromes.

 1 from basic.deque import Deque
 2 
 3 def palchecker(aString):
 4     chardeque = Deque()
 5 
 6     for ch in aString:
 7         chardeque.addRear(ch)
 8 
 9     stillEqual = True
10 
11     while chardeque.size() > 1 and stillEqual:
12         first = chardeque.removeFront()
13         last = chardeque.removeRear()
14         if first != last:
15             stillEqual = False
16 
17     return stillEqual
18 
19 print(palchecker("lsdkjfskf"))
20 print(palchecker("radar"))
双端队列解决回文字符串

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11367019.html