Python data structure 6: queue concept and implementation, double ended queue concept and implementation, hot potato algorithm, method of judging palindromes

Posted by Foregone96 on Thu, 10 Feb 2022 03:55:52 +0100

1. Concept and nature of queue

1.1 definition:

Queue is an ordered data set, which is characterized by

  • The addition of new data items always occurs at one end (often referred to as the "tail end")
  • The removal of existing data items always occurs at the other end (usually referred to as the "first front" end)
    When the data item is added to the queue, it first appears at the end of the queue. With the removal of the data item at the head of the queue, it gradually approaches the head of the queue.

1.2 nature:

  • FIFO first in first out: the newly added data item must wait at the end of the data set, and the data item with the longest waiting time is the head of the team
  • The queue has only one entry and one exit: data items are not allowed to be directly inserted into the queue or removed from the middle

1.3 example:

  • Queuing: when you go to the bank to do business, the first to arrive will be in front and receive the service first. The people behind receive service.

2. Queue in data structure

In the data structure, we use the abstract data type Queue to represent the Queue.

2.1 definition:

Abstract data type Queue is defined by the following operations:

  • Queue(): create an empty queue object with the return value of queue object;
  • enqueue(item): add the data item item to the end of the queue without return value; Join the team.
  • dequeue(): removes the data item from the head of the queue, the return value is the head of the queue, and the queue is modified; Out of the team
  • isEmpty(): test whether the queue is empty. The return value is Boolean
  • size(): returns the number of data items in the queue.

2.2 examples

for example

2.3 code implementation Queue

class Queue:
    def __init__(self):
        self.items = []
    def enqueue(self, item): # Complexity is O(n)
        self.items.insert(0, item) # That is, the leftmost end of the list is the tail of the team, which is the key difference from the stack
    def dequeue(self): # Complexity is O(1)
        return self.items.pop() # That is, the rightmost end of the list is the end of the queue, which is the same as the stack
    def isEmpty(self):
        return self.items == []
    def size(self):
        return len(self.items)

# test
q = Queue()
q.enqueue("xxx")
print(q.items)
print(q.isEmpty())
print(q.size())
print(q.dequeue())
print(q.items)
print(q.isEmpty())
print(q.size())

3. Double ended queue Deque

3.1 definitions

Double ended queue is a special kind of queue. This kind of queue data items can be added from the head of the queue or from the
Join at the end of the team; Data items can also be removed from both ends.
In a sense, dual ended queues integrate stack and queue capabilities

3.2 nature

Dual ended queues do not have inherent L IFO or FIFO characteristics
If the dual ended queue is used to simulate the stack or queue, the user needs to maintain the consistency of operation

4. Deque in data structure

4.1 definition:

deque defines the following operations:

  • Deque(): create an empty double ended queue
  • Add front (item): add item to the team leader
  • addRear(item): add item to the end of the queue
  • removeFront(): removes the data item from the head of the queue, and the return value is the removed data item
  • removeRear(): removes the data item from the end of the queue, and the return value is the removed data item
  • isEmpty(): Returns whether deque is empty
  • size(): returns the number of data items contained in deque

4.2 examples

4.3 code implementation Deque

class Deque:
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return self.items == []
    def addFront(self, item):
        self.items.append(item)
    def addRear(self, item):
       self.items.insert(0, item)
    def removeFront(self):
       return self.items.pop()
    def removeRear(self):
        return self.items.pop(0)
    def size(self):
        return len(self.items)

4.4 algorithm question: judge palindrome words:

4.4.1 problems:

"Palindrome word" refers to a word that is read the same as the reverse one,
Such as radar, madam, toot
Shanghai's tap water comes from the sea and Shandong's luohuashenghualuodongshan

4.4.2 solution:

Through observation, the letter composition of each word is symmetrical. We use a double ended queue to store all the letters of each word. Letters leave the queue from both ends. When the queue is empty or there is only one letter left, the same letters from both ends are palindromes.

class Deque:
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return self.items == []
    def addFront(self, item):
        self.items.append(item)
    def addRear(self, item):
       self.items.insert(0, item)
    def removeFront(self):
       return self.items.pop()
    def removeRear(self):
        return self.items.pop(0)
    def size(self):
        return len(self.items)

def huiWenci(wordList):
    dq = Deque()
    for ch in wordList:
        dq.addRear(ch)
    stillEqual = True
    while dq.size() > 1 and stillEqual:
        first = dq.removeFront()
        last = dq.removeRear()
        if first != last:
            stillEqual = False
    return stillEqual

print(huiWenci("radar")) # True
print(huiWenci("toot")) # True
print(huiWenci("Shanghai's tap water comes from the sea")) # True
print(huiWenci("Shandong Luohua Shenghua Luodong mountain")) # True
print(huiWenci("toor")) # False
print(huiWenci("Shandong luohuashenghualuoxishan")) # False

reference

The knowledge of this article comes from the data structure and algorithm of station B video [mu class + Classroom record], Python version - Peking University - Chen Bin - subtitle proofreading - [end!], ***

Topics: Python Algorithm data structure