網站首頁 編程語言 正文
1.隊列的介紹
隊列的定義
- 隊列(Queue)是一種線性存儲結構。它有以下幾個特點:
- 按照"先進先出(FIFO, First-In-First-Out)"方式進出隊列。
- 隊列只允許在"隊首"進行取出操作(出隊列),在"隊尾"進行插入操作(入隊列?)
隊列實現(xiàn)的方式有兩種
- 基于動態(tài)數(shù)組實現(xiàn)
- 基于鏈表形式實現(xiàn)
隊列需要實現(xiàn)的函數(shù)
-
T dequeue() :?
出隊列,并返回取出的元素 -
void enqueue(const T &t) :?
入隊列 -
T &head() :?
獲取隊首數(shù)據(jù),但是不會被取出 -
const T &head() const :
?獲取const類型隊首數(shù)據(jù) -
int length() const:?
獲取數(shù)量(父類已經實現(xiàn)) -
void clear():
?清空隊列(父類已經實現(xiàn))
2.代碼實現(xiàn)
本章,我們實現(xiàn)的隊列基于鏈表形式實現(xiàn),它的父類是我們之前實現(xiàn)的LinkedList類:
C++?雙向循環(huán)鏈表類模版實例詳解
所以Queue.h代碼如下:
#ifndef QUEUE_H #define QUEUE_H #include "throw.h" // throw.h里面定義了一個ThrowException拋異常的宏,如下所示: //#include//using namespace std; //#define ThrowException(errMsg) {cout<<__FILE__<<" LINE"<<__LINE__<<": "< class Queue : public LinkedList { public: inline void enqueue(const T &t) { LinkedList ::append(t); } inline T dequeue() { if(LinkedList ::isEmpty()) { // 如果棧為空,則拋異常 ThrowException("Stack is empty ..."); } T t = LinkedList ::get(0); LinkedList ::remove(0); return t; } inline T &head() { if(LinkedList ::isEmpty()) { // 如果棧為空,則拋異常 ThrowException("Stack is empty ..."); } return LinkedList ::get(0); } inline const T &head() const { if(LinkedList ::isEmpty()) { // 如果棧為空,則拋異常 ThrowException("Stack is empty ..."); } return LinkedList ::get(0); } }; #endif // QUEUE_H
3.測試運行
int main(int argc, char *argv[]) { Queuequeue; cout<<"******* current length:"<
運行打印:
總結
原文鏈接:https://blog.csdn.net/qq_37997682/article/details/123127872
相關推薦
- 2022-04-20 C++中的函數(shù)你真的理解了嗎_C 語言
- 2023-11-22 fatal: unable to access ‘https://github.com/xxxxx/
- 2022-05-02 winform關閉窗體FormClosing事件用法介紹_C#教程
- 2022-09-05 Spark Rdd之mapToPair,flatMapToPair
- 2022-06-02 Python中函數(shù)的創(chuàng)建與調用你了解嗎_python
- 2022-06-16 golang組件swagger生成接口文檔實踐示例_Golang
- 2023-01-17 使用matplotlib繪制熱圖(heatmap)全過程_python
- 2022-09-03 C++日期類(Date)實現(xiàn)的示例代碼_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支