Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Queue queue template in C++

Shulou Source: shulou.com Published: 2022-06-01 11:53:00 10月24日 Update

This article mainly introduces the example analysis of Queue queue template in C++. What is introduced in this article is very detailed and has certain reference value. Interested friends must finish it!

1. Introduction of the queue

Definition of queues

Queue (Queue) is a linear storage structure. It has the following characteristics:

Enter and leave the queue according to the "FIFO, First-In-First-Out" mode.

The queue only allows the take-out operation (out of the queue) at the "head of the queue" and the insert operation (enter the queue) at the end of the queue.

Queues can be implemented in two ways

Implementation based on dynamic array

Implementation based on linked list

Functions that need to be implemented in the queue

T dequeue (): leaves the queue and returns the fetched element

Void enqueue (const T & t): join the queue

T & head (): get the team leader data, but will not be taken out

Const T & head () const: get team head data of const type

Int length () const: get the quantity (the parent class has been implemented)

Void clear (): clear the queue (the parent class has been implemented)

two。 Code implementation

In this chapter, the queue we implemented is based on a linked list, and its parent class is the LinkedList class that we implemented earlier:

Detailed explanation of C++ two-way circular chain list template example

So the Queue.h code is as follows:

# ifndef QUEUE_H#define QUEUE_H#include "throw.h" / / throw.h defines a macro for ThrowException to throw an exception, as follows: / / # include / / using namespace std;//#define ThrowException (errMsg) {cout

Tags: Queues templates examples examples analysis code content forms data methods articles runs values elements advanced interests functions dynamic two-way examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Docker Redmi Xiaomi