Data structure must be queue and double ended queue (Python)

data structure queue double ended


queue

1. What is the queue

​ The idea of queue is closer to our life , When we line up to check out at the supermarket , In fact, it is the implementation of a queue , That is, the people who line up first check out first , The idea of people in line after checking out .

​ Like the stack , A queue is also an ordered collection , The add operation occurs at the end , The removal operation occurs in the head , The new element will enter the queue from the tail , Then move straight forward to the head , Until it becomes the next element to be removed .

​ The nature of the queue stipulates that the newly added element must wait at the end of the queue , The longest element in the queue is at the head , This principle is called FIFO(first-in first-out)

 Insert picture description here

2. Implementation of queues

​ The implementation of queue is very simple , We just need to ensure that the elements “ From where , I don't know where it comes from ” That's all right. , Using a list to implement the queue, we can use... From the end insert() Function insert element , Reuse pop() Push out the elements of the head . The specific implementation method is as follows :

class Queue:
def __init__(self):
self.items = []
# Determine whether the queue is empty 
def isEmpty(self):
return self.items == []
# Queue entry 
def enqueue(self, item):
self.items.insert(0, item)
# Outgoing queue 
def dequeue(self):
return self.items.pop()
# Length of queue 
def size(self):
return len(self.items)

​ The test results are as follows :

# Test queue 
q = Queue()
q.isEmpty()
# Incoming elements 
q.enqueue('I')
q.enqueue('like')
q.enqueue('python')
q.size()
q.dequeue()
# Output 
'I'

deque

1. The concept of double ended queue

​ The double ended queue is more free than the previous data structure , There is no limit on which end of the queue to add and remove elements , This means that you can add and remove elements from either end .

​ The nature of double ended queue also determines that it has two different principles :LIFO and FIFO.

 Insert picture description here

2. Implementation of double ended queue

​ When implementing double ended queue, we can combine the previous methods used on stack and queue LIFO and FIFO Two thoughts . The specific implementation method is as follows :

class Deque:
def __init__(self):
self.items = []
# Determine whether it is null 
def isEmpty(self):
return self.items == []
# Insert data from the front end 
def addFront(self, item):
self.items.append(item)
# Insert data from the tail 
def addRear(self, item):
self.items.insert(0, item)
# Delete data from the front end 
def removeFront(self):
return self.item.pop()
# Delete data from the tail 
def removeRear(self):
return self.item.pop(0)
# Length of queue 
def size(self):
return len(self.items)

​ The test results are as follows :

# test 
d = Deque()
d.isEmpty()
# Insert elements 
d.addFront('I')
d.addFront('like')
d.addRear('Python')
# Test length 
d.size()
# Output 
3
版权声明
本文为[The second brother is not like a programmer]所创,转载请带上原文链接,感谢
https://javamana.com/2021/10/20211002150018487K.html

  1. Hadoop Foundation - 03 - hdfs (Hadoop Distributed File System) Basic Concepts
  2. Hadoop Foundation - 05 - hdfs Project (word Frequency Statistics)
  3. Hadoop Foundation - 06 - hdfs Data Read and write
  4. The "monthly test" report card of the new car built under the lack of core: Xiaopeng and Weilai took the lead in "breaking 10000", and the ideal plummeted by 25%
  5. Introduction to making arch linux software package
  6. Hard core observation 407 HTTPS everywhere browser extension is about to retire
  7. How to use busybox on Linux
  8. In 2021, the talent incentive plan of Linux foundation open source software School Park was officially launched
  9. It's not windows or Linux. Shrink is the "God operating system"
  10. Install anydesk on Ubuntu Linux
  11. 2021, can we recommend using Linux to play games?
  12. not exist:org.springframework.kafka.listener.AbstractMessageListenerContaingetContainerProperties()
  13. Are you sure HTTPS is asymmetric encryption for content encryption? See the answers and reasons
  14. MySQL online slow log query
  15. Java JDK server installation
  16. 手把手教你使用Java开发在线生成pdf文档
  17. Questions d'entrevue dans la base de données MySQL (dernière version 2021)
  18. Java零基础小白入门必做题汇总(建议收藏)第一篇
  19. Day15 Java Foundation
  20. Day16 Java Foundation
  21. Day17 Java Foundation
  22. Day18 Java Foundation
  23. Linux installe JDK 1.8 et configure les variables d'environnement
  24. Tutoriel d'utilisation Maven super détaillé
  25. Spring boot reads project parameter configuration
  26. Docker installing rocketmq
  27. Java Zero Basic small white Beginner must make a summary of issues (recommended Collection) Chapitre 1
  28. Manuel pour vous apprendre à utiliser le développement Java pour générer des documents PDF en ligne
  29. 40 + comment les femmes s'habillent - elles pour montrer leur jeunesse?Un manteau et une jupe vous donnent un look haut de gamme tout au long de l'automne et de l'hiver
  30. Tutoriel d'installation Ubuntu 16.04 / Hadoop 3.1.3Configuration autonome / pseudo - distribuée
  31. L'apprentissage le plus détaillé de springboot à l'échelle du réseau - day01
  32. L'apprentissage le plus détaillé de springboot sur le Web - day02
  33. L'apprentissage le plus détaillé de springboot sur le Web - day03
  34. L'apprentissage le plus détaillé de springboot sur le Web - day04
  35. Tutoriel d'utilisation Maven super détaillé
  36. L'apprentissage le plus détaillé de springboot sur le Web - day05
  37. L'apprentissage le plus détaillé de springboot sur le Web - day06
  38. L'apprentissage le plus détaillé de springboot sur le Web - day07
  39. Introduction to JavaScript - write a photo album for your girlfriend
  40. [Hadoop 3. X] HDFS storage type and storage strategy (V) overview
  41. L'apprentissage le plus détaillé de springboot sur le Web - day08
  42. Introduction à la page Web de rabbitmq (3)
  43. No Converter found for return value of type: class java.util.arraylist Error Problem
  44. (16) , spring cloud stream message driven
  45. Que faut - il apprendre de l'architecture des microservices Spring Cloud?
  46. Résolution: erreur: Java: distribution cible invalide: 11problème d'erreur
  47. Springboot démarre en une minute et sort de l'enfer de la configuration SSM!
  48. Maven - un outil de gestion essentiel pour les grands projets d'usine, de l'introduction à la maîtrise![️ Collection recommandée]
  49. ️ Push to interview in Large Factory ᥧ - - Spring Boot Automatic Assembly Principle
  50. [️ springboot Template Engine] - thymeleaf
  51. Springboot - MVC Automatic configuration Principle
  52. Mybatis reverse engineering and the use of new version mybatisplus 3.4 reverse engineering
  53. Base de données MySQL - transactions et index
  54. Sécurité du printemps - [authentification, autorisation, déconnexion et contrôle des droits]
  55. Moteur de base de données InnoDB diffère de myisam
  56. Swagger - [springboot Integrated Swagger, configure Swagger, configure scan Interface, configure API Group]
  57. Cadre de sécurité Shiro - [QUICKstart, login Block, User Authentication, request Authorization]
  58. [Introduction à Java] installation de l'environnement de développement - Introduction à Java et construction de l'environnement
  59. 【 linux】 notes d'utilisation tmux
  60. MySQL + mybatis paging query - database series learning notes