Data Structure Base Queue, Priority Queue, Double-Ended Queue

queue Definition Queues are also implemented using a chain storage structure C++ Implementation Code Priority Queue Dou...

queue

Definition

Queues are like stacks.
But there are still some differences.
Stack in, out of the stack in the queue corresponding to the queue (Enquene), out of the queue (Dequene).
The most advanced stack is called the bottom of the stack, and the most advanced queue is called the head of the queue.
Finally, it goes to the top of the stack and to the end of the queue.
The biggest difference is that the queue is not a stack of books, but a queue. It's like queuing in a supermarket to buy something. The aunt doesn't settle for the last person who enters the checkout first, but handles the whole queue first.The leader of the team left the supermarket after paying the money.This is called First In First Out (FIFO).

Thus, a queue needs the following capabilities:

  1. Entry
  2. Take the first team out and get it
  3. Check if this queue is empty
  4. Take a look at what the team leader is
  5. ClearQueue

STL Reference:https://zh.cppreference.com/w/cpp/container/queue

Queues are also implemented using a chain storage structure

The node of a queue should be like this.

That's what queuing up is all about.

The First pointer points to the first node of the queue, and the queue is where it comes out first.
Entering the queue is more cumbersome and requires pulling down from First. Which node has a Next that is a null pointer, which is the end of the queue, and then establishes a new node to assign it a Next.
To solve this problem, we set another pointer variable Last in the queue class to point to the end-of-queue node, which saves the effort of pulling down from First by assigning a value to Last when a new end-of-queue node is generated.

C++ Implementation Code

QueneNode.hpp:

#pragma once template<typename ElementType> class QueneNode { public: ElementType Data; QueneNode<ElementType>* Next = nullptr; QueneNode() {} QueneNode(ElementType data, QueneNode<ElementType>* next = nullptr) :Data(data), Next(next) {} };

Quene.hpp:

#pragma once #include <initializer_list> #include <stdexcept> #include "QueneNode.hpp" template<typename ElementType> class Quene { private: QueneNode<ElementType>* First = nullptr; QueneNode<ElementType>* Last = nullptr; public: Quene(){} Quene(const std::initializer_list<ElementType> elements); ~Quene(); void Clear(); void Enquene(ElementType element); ElementType Dequene(); ElementType& GetFirst(); }; template<typename ElementType> Quene<ElementType>::Quene(const std::initializer_list<ElementType> elements) { for (ElementType& i : elements) Enquene(i); } template<typename ElementType> Quene<ElementType>::~Quene() { Clear(); } template<typename ElementType> void Quene<ElementType>::Clear() { Last = First; while (Last) { First = First->Next; delete Last; Last = First; } First = Last = nullptr; } template<typename ElementType> void Quene<ElementType>::Enquene(ElementType element) { QueneNode<ElementType>* temp = new QueneNode<ElementType>(element); if (!First) First = Last = temp; else { Last->Next = temp; Last = temp; } } template<typename ElementType> ElementType Quene<ElementType>::Dequene() { if (!First) throw std::runtime_error("This quene has no data!"); ElementType temp = First->Data; if (First == Last) { delete First; FIrst = Last = nullptr; } else { QueneNode<ElementType>* tempPointer = FIrst; First = First->Next; delete tempPointer; } return temp; } template<typename ElementType> ElementType& Quene<ElementType>::GetFirst() { return First->Data; }
Priority Queue

A Priority Queue is a queue with a priority rule modification.

For example, if your nose gets swollen and you queue up in the emergency department,
It wasn't easy to get in there when a fellow man came in who was going to be killed.
You said it was difficult for you to line up. Why did he treat you first?
Then you have to go. It's a priority that people are dying.

STL Reference:https://zh.cppreference.com/w/cpp/container/priority_queue

The concept of a priority queue is easy to understand, but the problem is how you achieve it.
Just like the example,
How do you know when that dying buddy came to the hospital?
So you can't always guarantee that the first one is the first one.

In reality, it may take several or even dozens of indicators to determine priorities.
These all complicate the implementation of priority queues.

There are two implementations.

  1. The preferred node stays in the queue just like the other nodes, but it has a preferred flag.
    Wait until you are ready to leave the queue, then go through it to see if there is a priority.
    There are priority nodes to queue first, and there are no first nodes to queue.
    In this way, the new node will join the queue faster, but the queue operation will be slower.
  2. When a new node is added, it determines whether it is a priority node or not.
    Yes, pull directly to the top of the team.
    Not at the end of the team.
    If this is done, the queuing operation will be faster and the nodes will enter the queue slowly.
    Someone here might say that when a new node joins, it judges how slow it is.
    As you just said, it may take several or even dozens of indicators to determine priorities in reality.
    So you have to decide not only whether it is a priority node, but also what level it is.
    Do you dare to place the director behind the director?
    So when new nodes join the queue, you still have to go through the queue to see if there is a department director in the queue when you arrange the director.

There are two other ways to achieve this, but the book is very simple. I can't understand a sentence or two, that's all.

Double-ended Queue

A Double-ended Queue can either leave the first or the last queue.

STL Reference:https://zh.cppreference.com/w/cpp/container/deque

It can enter from either the first or the end of the team.

Define that. I recommend you look at this STL reference web address.
STL's deque ue can be accessed randomly.
Even though I don't know what's useful.Until I know I should add it here.

15 June 2020, 21:25 | Views: 8861

Add new comment

For adding a comment, please log in
or create account

0 comments