《数据结构、算法与应用C++语言描述》使用C++语言实现链表队列

news2024/9/24 17:17:21

《数据结构、算法与应用C++语言描述》使用C++语言实现链表队列

定义

队列的定义

队列(queue)是一个线性表,其插入和删除操作分别在表的不同端进行。插入元素的那一端称为队尾(back或rear),删除元素的那一端称为队首(front)。

队列的抽象数据类型

在这里插入图片描述

链表队列代码

_17queue.h

抽象类栈。

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			队列的抽象类
*/
#pragma once
#ifndef _QUEUE_H_
#define _QUEUE_H_
template<class T>
class queue
{
public:
	virtual ~queue() {}
	virtual bool empty() const = 0;//返回true,当且仅当队列为空
	virtual int size() const = 0;//返回队列中元素个数
	virtual T& front() = 0;//返回头元素的引用
	virtual T& back() = 0;//返回尾元素的引用
	virtual void pop() = 0;//删除首元素
	virtual void push(const T& theElement) = 0;//把元素theELment加入队尾
};
#endif

_6chainNode.h

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日22点06分
Last Version:			V1.0
Descriptions:			链表的结点
*/
#pragma once
#ifndef _CHAINNODE_H_
#define _CHAINNODE_H_
template <class T>
struct chainNode
{
	//数据成员
	T element;
	chainNode<T>* next;
	//方法
	chainNode() {}
	chainNode(const T& element)
	{
		this->element = element;
		this->next = nullptr;
	}

	chainNode(const T& element, chainNode<T>* next)
	{
		this->element = element;
		this->next = next;
	}
};
#endif

_21linkedQueue.h

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			链表存储的队列的头文件
*/
#pragma once
#ifndef _LINKEDQUEUE_H_
#define _LINKEDQUEUE_H_
#include<sstream>
#include<iostream>
#include "_1myExceptions.h"
#include "_6chainNode.h"
#include "_17queue.h"
/*测试函数*/
void linkedQueueTest();

using namespace std;
template<class T>
class linkedQueue : public queue<T>
{
public:
    /*成员函数*/
    linkedQueue(int initialCapacity = 10)
    {
        theFront = theBack = nullptr;
        queueSize = 0;
    }
    ~linkedQueue();
    bool empty() const { return queueSize == 0; }
    //返回队列的元素个数
    int size() const { return queueSize; }
    /*清空队列中的元素*/
    void clear();
    /*返回第一个元素*/
    T& front() { return theFront->element; }
    /*返回最后一个元素*/
    T& back() { return theBack->element; }
    /*删除队首元素*/
    void pop()
    {
        chainNode<T>* next = theFront->next;
        delete theFront;
        theFront = next;
        queueSize--;
    }
    /*向队尾插入元素theElement*/
    void push(const T& theElement)
    {
        if (queueSize == 0)
            theFront = theBack = new chainNode<T>(theElement, nullptr);
        else
        {
            theBack->next = new chainNode<T>(theElement, nullptr);
            theBack = theBack->next;
        }       
        queueSize++;
    }
    //void meld(arrayQueue<T>& a, arrayQueue<T>& b);//合并队列a,b到当前队列
    //void split(arrayQueue<T>& a, arrayQueue<T>& b);//将当前队列分成两个队列a,b

    /*重载操作符*/
    /*重载[]操作符*/
    T operator[](int i)
    {
        chainNode<T>* currentNode = theFront;
        for (int j = 0; j < i; j++)
            currentNode = currentNode->next;
        return currentNode->element;
    }

    /*友元函数*/
    friend istream& operator>> <T>(istream& in, linkedQueue<T>& m);
    //输出但是不pop()元素
    friend ostream& operator<< <T>(ostream& out, linkedQueue<T>& m);
private:
    chainNode<T>* theFront;       // 指向第一个元素的指针
    chainNode<T>* theBack;        // 指向最后一个元素的指针
    int queueSize;    // 队列的容量,实质上比队列容量(不包含queueFront指向的那一个位置)大1
};
/*友元函数*/
/*>>操作符*/
template<class T>
istream& operator>>(istream& in, linkedQueue<T>& m)
{
    int numberOfElement = 0;
    cout << "Please enter the number of element:";
    while (!(in >> numberOfElement))
    {
        in.clear();//清空标志位
        while (in.get() != '\n')//删除无效的输入
            continue;
        cout << "Please enter the number of element:";
    }
    T cinElement;
    for (int i = 0; i < numberOfElement; i++)
    {
        cout << "Please enter the element " << i + 1 << ":";
        while (!(in >> cinElement))
        {
            in.clear();//清空标志位
            while (in.get() != '\n')//删除无效的输入
                continue;
            cout << "Please enter the element " << i + 1 << ":";
        }
        m.push(cinElement);
    }
    return in;
}
//输出但是不pop()元素
/*<<操作符*/
template<class T>
ostream& operator<<(ostream& out, linkedQueue<T>& m)
{
    chainNode<T>* currentNode = m.theFront;
    while(currentNode != nullptr)
    {
        cout << currentNode->element << " ";
        currentNode = currentNode->next;
    }
    out << endl;
    return out;
}
/*成员函数*/
/*析构函数*/
template<class T>
linkedQueue<T>::~linkedQueue()
{
    chainNode<T>* nextNode = theFront;
    while (nextNode != nullptr)
    {
        nextNode = nextNode->next;
        delete theFront;
        theFront = nextNode;
    }
}
/*清空队列中的元素*/
template<class T>
void linkedQueue<T>::clear()
{
    chainNode<T>* nextNode = theFront;
    while (nextNode != nullptr)
    {
        nextNode = nextNode->next;
        delete theFront;
        theFront = nextNode;
    }
    queueSize = 0;
    theFront = theBack = nullptr;
}

#endif

_21linkedQueue.cpp

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			测试_21linkedQueue.h头文件中的所有函数
*/
#include <iostream>
#include <time.h>
#include "_21linkedQueue.h"
using namespace std;
/*测试函数*/
void linkedQueueTest()
{
	cout << endl << "*********************************linkedQueueTest()函数开始*************************************" << endl;
	linkedQueue<int> a;

	//测试输入和输出
	cout << endl << "测试友元函数*******************************************" << endl;
	cout << "输入输出************************" << endl;
	cin >> a;
	cout << "linkedQueue a is:" << a;
	cout << endl << "测试成员函数*******************************************" << endl;
	cout << "empty()*************************" << endl;
	cout << "a.empty() = " << a.empty() << endl;
	cout << "size()**************************" << endl;
	cout << "a.size() = " << a.size() << endl;
	cout << "push()**************************" << endl;
	cout << "before push linkedQueue a is:" << a;
	a.push(99);
	a.push(22);
	cout << "after push linkedQueue a is:" << a;
	cout << "front()*************************" << endl;
	cout << "a.front() = " << a.front() << endl;
	cout << "back()**************************" << endl;
	cout << "a.back() = " << a.back() << endl;
	cout << "pop()***************************" << endl;
	cout << "before pop linkedQueue a is:" << a;
	a.pop();
	a.pop();
	cout << "after pop linkedQueue a is:" << a;
	cout << "clear()*************************" << endl;
	a.clear();
	cout << "after clear linkedQueue a is:" << a;

	cout << endl << "测试成员函数性能***************************************" << endl;
	cout << "push()**************************" << endl;
	linkedQueue<int> f;
	double clocksPerMillis = double(CLOCKS_PER_SEC) / 1000;
	clock_t startTime = clock();
	for (int i = 0; i < 100000000; i++)
		f.push(i);
	double pushTime = (clock() - startTime) / clocksPerMillis;
	cout << 10000 << " push took " << pushTime << " ms" << endl;

	cout << "*********************************linkedQueueTest()函数结束*************************************" << endl;
}

main.cpp

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			main()函数,控制运行所有的测试函数
*/
#include <iostream>
#include "_18arrayQueue.h"


int main()
{
	arrayQueueTest();
	return 0;
}

_1myExceptions.h

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			综合各种异常
*/
#pragma once
#ifndef _MYEXCEPTIONS_H_
#define _MYEXCEPTIONS_H_
#include <string>
#include<iostream>

using namespace std;

// illegal parameter value
class illegalParameterValue 
{
   public:
      illegalParameterValue(string theMessage = "Illegal parameter value")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal input data
class illegalInputData 
{
   public:
      illegalInputData(string theMessage = "Illegal data input")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal index
class illegalIndex 
{
   public:
      illegalIndex(string theMessage = "Illegal index")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix index out of bounds
class matrixIndexOutOfBounds 
{
   public:
      matrixIndexOutOfBounds
            (string theMessage = "Matrix index out of bounds")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix size mismatch
class matrixSizeMismatch 
{
   public:
      matrixSizeMismatch(string theMessage = 
                   "The size of the two matrics doesn't match")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// stack is empty
class stackEmpty
{
   public:
      stackEmpty(string theMessage = 
                   "Invalid operation on empty stack")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// queue is empty
class queueEmpty
{
   public:
      queueEmpty(string theMessage = 
                   "Invalid operation on empty queue")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// hash table is full
class hashTableFull
{
   public:
      hashTableFull(string theMessage = 
                   "The hash table is full")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// edge weight undefined
class undefinedEdgeWeight
{
   public:
      undefinedEdgeWeight(string theMessage = 
                   "No edge weights defined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// method undefined
class undefinedMethod
{
   public:
      undefinedMethod(string theMessage = 
                   "This method is undefined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};
#endif

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1111155.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

告别传统纸质期刊,电子期刊更环保更快捷

​【新发现】随着科技的发展&#xff0c;电子期刊逐渐取代了传统的纸质期刊&#xff0c;成为人们获取信息的新选择。电子期刊不仅环保&#xff0c;而且快捷方便&#xff0c;但是你知道怎么制作电子期刊吗&#xff1f; 不会制作的可以试试我推荐的这个网站----FLBOOK电子杂志制作…

Simulink 最基础教程(一)

1.1基本概念 一个典型的Simulink模型大致如上图这样&#xff1a; 1&#xff09;模块 block&#xff1a;图中画圈的那些&#xff0c;每个模块可以完成一些特定的任务&#xff0c;类似MATLAB中函数的概念。软件提供了很多模块&#xff0c;当然也可以自定义新的模块 2&#xff0…

进阶JAVA篇- Collection 类的常用的API与 Collection 集合的遍历方式

目录 1.0 Collection 类的说明 1.1 Collection 类中的实例方法 2.0 Collection 集合的遍历方式&#xff08;重点&#xff09; 2.1 使用迭代器&#xff08; Iterator &#xff09;进行遍历 2.2 使用增强型 for 循环进行遍历 2.3 使用 Java 8的 Stream API 进行遍历&#xff08;使…

线程安全之锁的原理

&#x1f525;&#x1f525; 欢迎来到小林的博客&#xff01;&#xff01;       &#x1f6f0;️博客主页&#xff1a;✈️林 子       &#x1f6f0;️博客专栏&#xff1a;✈️ Linux       &#x1f6f0;️社区 :✈️ 进步学堂       &#x1f6f0…

思维模型 鸟笼效应

本系列文章 主要是 分享 思维模型&#xff0c;涉及各个领域&#xff0c;重在提升认知。 1 鸟笼效应的应用 1.1 购物中的鸟笼效应 1 漂亮鞋子的故事 假设一个人在商场看到一双漂亮的鞋子&#xff0c;并冲动地购买了它们。当他回到家后&#xff0c;他发现这双鞋子并不适合他的…

【QT】QListWidget

新建项目 list widget&#xff0c;做了布局 #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);// listWidget的使用&#xff0c;基于itemQListWidgetItem* item …

【电子通识】USB发展历史

USB接口自1994年推出以来&#xff0c;经过29年的发展&#xff0c;经过USB1.0/1.1、USB2.0、USB3.x&#xff0c;发展到了现在的USB4&#xff1b;传输速率也从最开始的1.5Mbps&#xff0c;大幅提高到了最新的40Gbps。 USB1.0 1996年1月15日推出USB1.0接口规范规定低速传输速率为…

数字孪生软件架构选BS还是CS?不,我们选择CSaaS!

BS&#xff08;Browser/Server&#xff09;和CS&#xff08;Client/Server&#xff09;是两种不同的软件架构模式&#xff0c;具有不同的特点和优缺点。 BS&#xff08;Browser/Server&#xff09;架构 BS架构指的是基于浏览器和服务器的软件架构&#xff0c;客户端通常是一个…

1814_ChibiOS中的时间以及时间间隔处理

全部学习汇总&#xff1a; GreyZhang/g_ChibiOS: I found a new RTOS called ChibiOS and it seems interesting! (github.com) 1. 时间的相关配置&#xff0c;有tick的计数精度、时钟频率、间隔时间精度、时间类型大小等不同的配置。这些参数&#xff0c;涉及到系统的时间计数…

JVMGC复习

TLAB:默认给每一个线程开辟一块内存空间存放线程自己的对象 Class对象是存放在堆区的&#xff0c;不是方法区&#xff0c;类的元数据元数据并不是类的Class对象&#xff0c;Class对象是加载的最终产品&#xff0c;类的方法代码&#xff0c;变量名&#xff0c;方法名&#xff0c…

解决windows中被占端口问题(实测有效)

1、用管理员身份打开cmd 2、输入命令查找所被占的端口号 例&#xff1a;8902 netstat -ano | findstr 8902终结被占端口号的进程 例&#xff1a;端口号为8080&#xff0c;则查找到的pid为18524 taskkill /t /f /pid 18524强制&#xff08;/F参数&#xff09; 子进程&#x…

小公司如何成功申请企业邮箱

对于小公司来说拥有专业的企业邮箱不仅有助于提升公司形象&#xff0c;还能有效提高工作效率。小公司怎么申请企业邮箱&#xff1f;以下是一些步骤和建议供您参考。 需要明确公司的需求。 这包括确定所需用户账户的数量&#xff08;一般是目前使用人数再加上几个备用的邮箱&…

Docker入门到精通教程

Docker是什么 Docker是一个开源的应用容器引擎&#xff0c;它基于Go语言并遵从Apache2.0协议开源。容器技术是和我们的宿主机共享硬件资源及操作系统&#xff0c;实现资源的动态分配&#xff0c;在资源受到隔离的进程中运行应用程序及其依赖关系。 Docker可帮助更快地打包、测…

Redis实现附近商户

GEO数据结构的基本用法 GEO就是Geolocation的简写形式&#xff0c;代表地理坐标。Redis在3.2版本中加入了对GEO的支持&#xff0c;允许存储地理坐标信息&#xff0c;帮助我们根据经纬度来检索数据。常见的命令有&#xff1a; GEOADD&#xff1a;添加一个地理空间信息&#xf…

【广州华锐互动】VR石油钻井井控实训系统

在过去的几十年中&#xff0c;石油工业的发展速度一直在加快。为了适应这个快速发展的行业&#xff0c;需要新的技术和工具&#xff0c;而VR&#xff08;虚拟现实&#xff09;技术正是其中之一。本文将探讨VR石油钻井井控实训系统在石油工业教育中的应用。 在真实的钻井环境中&…

接口管理神器Apipost

自诞生以来&#xff0c;Apipost凭借其简洁直观的用户界面、强大的功能以及简单、易上手的操作&#xff0c;让Apipost成为了开发人员不可或缺的工具。本文将详细介绍Apipost的主要功能和使用方法&#xff0c;帮助大家更好地了解这款优秀的API开发工具。 下载安装 直接进入Apip…

Stable Diffusion WebUI扩展a1111-sd-webui-tagcomplete之Booru风格Tag自动补全功能详细介绍

安装地址 直接附上地址先: Ranting8323 / A1111 Sd Webui Tagcomplete GitCodeGitCode——开源代码托管平台,独立第三方开源社区,Git/Github/Gitlabhttps://gitcode.net/ranting8323/a1111-sd-webui-tagcomplete.git上面是GitCode的地址,下面是GitHub的地址,根据自身情…

个人微信CRM客户管理系统怎么选?功能介绍

现在市面上有许多种类的个人微信CRM客户管理系统可供选择&#xff0c;因此&#xff0c;我们需要选择最适合自己需求的微信管理系统CRM&#xff0c;最重要的是根据您的需求和期望的功能来进行筛选。 如何选择适合自己的微信CRM客户管理系统&#xff1f; 现在市面上的系统五花八…

Cloud Studio连接MySQL,Access denied for一系列问题

官方文档有写如何安装Mysql $ apt update $ apt install mysql-server mysql-client -y$ service mysql start mysql -uroot -p123456进入MySQL命令行 问题出在连接数据库这一步&#xff0c;命令行能进去&#xff0c;但是数据库插件和代码都连不上 Access denied for 大概率…

基于SSM的外卖点餐系统设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue、HTML 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是…