Queue of commonly used data structures

1. Definition

        Following on from the previous article on the stack of commonly used data structures , let's strike while the iron is hot, and today we will talk about the queue. We all know that the stack is a restricted linear table, which can only enter from the top to the top, that is, last in first out. A queue is a data structure very similar to a stack, and it is also a restricted linear list. The restriction is FIFO (first in first out), just like we usually line up, newcomers need to be at the end of the line, and the first person in the line is the first to get on the bus.

               

2. Sequential queue code implementation

        We use a python list to implement a simple queue, including enqueue and dequeue; here, every time we leave the team, we directly take the first position, and then the other elements move up at once.

#-*- coding:utf-8 -*-
#此处Queue类是否需要继承object,属于历史遗留问题
#python2的时候如果继承object表示新式类,在子类方法调用时采用广度优先;否则为旧式类,采用深度优先搜索
#但是python3已经进行了统一,都用广度优先;所以此处不需要继承object
class Queue:
    def __init__(self):
        self.array = []
        self.front = 0
        self.rear = 0
    #插入一个元素到队列尾部
    def in_queue(self,v):
        self.array.append(v)
        self.rear += 1
    #出队列
    def out_queue(self):
        return self.array.pop(self.front)
    def get_size(self):
        return len(self.array)
    def is_empty(self):
        return self.array == []
    def list_queue(self):
        return self.array
queue = Queue()
queue.in_queue(1)
print(queue.list_queue())

3. Deque

       Above we introduced the sequential queue, which can only be added from the tail and pops up from the top, which is similar to our usual scene of queuing up and getting on the train; now imagine such a scene, in the morning, go to the company to queue up and wait for the elevator. Suppose there are two elevators A and B, and only one elevator runs at a time (a bit pit). In this way, there will be people waiting on both sides, and both can go. It's just that those who come first are not bound to come first.

       The double-ended queue is such a structure. It runs from the top and the tail to and from the queue, which provides greater flexibility. If you only use the function at one end, it is a sequential queue. The following is a simple python implementation.

#-*- coding:utf-8 -*-
class Deque:
    def __init__(self):
        self.array = []
    def add_front(self,v):
        self.array.insert(0,v)
    def add_rear(self,v):
        self.array.append(v)
    def pop_front(self):
        return self.array.pop(0)
    def pop_rear(self):
        return self.array.pop(-1)
    def get_size(self):
        return len(self.array)
    def is_empty(self):
        return self.array == []
    def list_item(self):
        return self.array

4. Double-ended queue inspection palindrome

     For the classic palindrome problem, given a string'abcdcba', if the beginning and the end are the same, and there is a word in the middle at the end, it is judged to be a palindrome. This problem can be easily solved by using a double-ended queue. Each character is enqueued, and the first and the last of the queue are compared each time. If there is a difference, it is not a palindrome. Otherwise it is a palindrome.

    We have found that to solve some classic algorithm problems, it is very important to use appropriate data structures. It can often make complex problems simple!

def palindrome(str):
    deque = Deque()
    for char in str:
        deque.add_rear(char)
    print(deque.list_item())
    while deque.get_size()>1:
        first_char = deque.pop_front()
        last_char = deque.pop_rear()
        if first_char != last_char:
            return False
    return True
str = 'abcdcba'
print(palindrome(str))

5. Priority queue

    When queuing, there are really a lot of things, a lot of rules; the next thing is the priority queue. The order of the queue has nothing to do with the order of entering the queue. You can imagine that these people are all related households. Everyone handles the business together. The priority of the relationship with the chairman is high, and the next one is related to the general manager, and then the director, generally middle-level, team leader; each element goes out according to the priority;

    In general, we adopt the structure of the heap, and the element at the top of the heap is always the largest. For details, please refer to our previous article The Heap of Data Structures , so I won’t go into details here!

6. Summary

        In this chapter, we discussed the data structure of the queue, which is a linear table that conforms to the first-in first-out principle, and can only add elements from the tail and remove from the top. Then we introduced some of its variants, such as a double-ended queue, which can add and delete elements from the beginning and the end; the priority queue, which determines the order of dequeuing according to the priority of the elements. Generally speaking, it is relatively simple, but the use of queues is still very wide. For example, after I post this article, the platform will remind me'your article has entered the review queue'. For the study of data structure, different content should be linked and compared, and the efficiency will be slightly higher. Come on!

7. Gossip

         The world is a mystery, and the key to solving this mystery lies in how to explain the concept of "randomness". We know that if a coin is randomly thrown, the probability that the front and back are up is 1/2; but why I throw it up and it's the same for everyone? Is this result fair to the negative? Like many people who lose money in gambling, I cannot understand this problem at all. People call this phenomenon random. Does the so-called randomness mean that the future is turning the brand? Is there such a thing as random? The cognitive system of the world now recognizes its existence, that is, the so-called future has not yet come. It's as if we keep walking forward, but in fact, there is no road ahead. The road is created the moment your foot falls. People are so frightened, how can people feel so confident that tomorrow the world will look like tomorrow? Before something happens, do you need to prepare its house so that it can live in?

          Otherwise, just think about it, in fact, all the roads have been created, so we won't be afraid. In fact, there is no such thing as random, and time is not a linear concept. It is a big chessboard, it is a confrontation, a game. Random is not a blind selection, but an algorithm that is too powerful to understand.

[Global hit debut song] Avril Lavigne-Complicated (Live at All That) 2002.10.26

 

    

Guess you like

Origin blog.csdn.net/gaobing1993/article/details/108795314