数据结构探险(一)——队列

2023-12-28 19:58
文章标签 数据结构 队列 探险

本文主要是介绍数据结构探险(一)——队列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • FIFO
  • 普通队列
  • 循环队列

c++实现循环队列

#ifndef MYQUEUE_H
#define MYQUEUE_H
/****************************************/
/*环形队列C++实现*/
/****************************************/class MyQueue
{
public:MyQueue(int queueCapacity);  //InitQueue(&Q)virtual ~MyQueue();         //DestroyQueue(&Q)void ClearQueue();     //ClearQueue(&Q)bool QueueEmpty() const;  //QueueEmpty(Q)bool QueueFull() const;int QueueLength() const;  //QueueLength(Q)bool EnQueue(int element);  //EnQueue(&Q,element)bool DeQueue(int &element);  //DeQueue(&Q,&element)void QueueTraverse();      //QueueTraverse(Q,visit())B
private:int *m_pQueue;//队列数组指针int m_iQueueLen;//队列元素个数int m_iQueueCapacity;//队列数组容量int m_iHead;int m_iTail;};#endif
#include<stdio.h>
#include<stdlib.h>
#include"MyQueue.h"
#include<iostream>using namespace std;MyQueue::MyQueue(int queueCapacity)
{m_iQueueCapacity =queueCapacity;m_pQueue = new int[m_iQueueCapacity];ClearQueue();
}MyQueue::~MyQueue()
{delete []m_pQueue;m_pQueue=NULL;
}void MyQueue::ClearQueue()
{m_iHead=0;m_iTail=0;m_iQueueLen=0;
}bool MyQueue::QueueEmpty() const
{if(m_iQueueLen==0)return true;elsereturn false;//return m_iQueueLen==0?true:false;
}int MyQueue::QueueLength() const
{return m_iQueueLen;
}bool MyQueue::QueueFull() const
{if(m_iQueueCapacity==m_iQueueLen)return true;else return false;
}bool MyQueue::EnQueue(int element)
{if(QueueFull()){return false;}else{m_pQueue[m_iTail]=element;m_iTail++;m_iTail=m_iTail%m_iQueueCapacity;m_iQueueLen++;return true;}
}bool MyQueue::DeQueue(int &element)//element必须是引用才可以带回值
{if(QueueEmpty()){return false;}else{element=m_pQueue[m_iHead];m_iHead++;m_iHead=m_iHead%m_iQueueCapacity;m_iQueueLen--;return true;}}void MyQueue::QueueTraverse()
{for(int i=m_iHead;i<m_iQueueLen;i++)//从队列头遍历{cout<<m_pQueue[i%m_iQueueCapacity]<<endl;}
}
#include<stdio.h>
#include<stdlib.h>
#include"MyQueue.h"
#include"MyQueue.cpp"/****************************/
/* 实现环形队列 */
/****************************/
using namespace std;int main(void)
{MyQueue *p =new MyQueue(4);p->EnQueue(10);p->EnQueue(12);p->EnQueue(16);p->EnQueue(18);p->EnQueue(20);p->QueueTraverse();int e=0;p->DeQueue(e);cout<<endl;cout<<e<<endl;delete p;p=NULL;system("pause");return 0;
}

在这里插入图片描述

当存入的数据不是int,而是其他复杂数据类型时

#ifndef CUSTOMER_H
#define CUSTOMER_H#include<iostream>
using namespace std;/************************************************/
/*customer类定义*/
/**********************************************/
class Customer
{
public:Customer(string name="",int age=0);void printfInfo() const;
private:string m_strName;int m_iAge;
};#endif
Customer::Customer(string name,int age)
{m_strName=name;m_iAge=age;
}void Customer::printfInfo() const
{cout<<"name:"<<m_strName<<endl;cout<<"age:"<<m_iAge<<endl;
}#ifndef MYQUEUE_H
#define MYQUEUE_H
/****************************************/
/*环形队列C++实现*/
/****************************************/class MyQueue
{
public:MyQueue(int queueCapacity);  //InitQueue(&Q)virtual ~MyQueue();         //DestroyQueue(&Q)void ClearQueue();     //ClearQueue(&Q)bool QueueEmpty() const;  //QueueEmpty(Q)bool QueueFull() const;int QueueLength() const;  //QueueLength(Q)bool EnQueue(Customer element);  //EnQueue(&Q,element)bool DeQueue(Customer &element);  //DeQueue(&Q,&element)void QueueTraverse();      //QueueTraverse(Q,visit())B
private:Customer *m_pQueue;//队列数组指针int m_iQueueLen;//队列元素个数int m_iQueueCapacity;//队列数组容量int m_iHead;int m_iTail;};#endifusing namespace std;MyQueue::MyQueue(int queueCapacity)
{m_iQueueCapacity =queueCapacity;m_pQueue = new Customer[m_iQueueCapacity];ClearQueue();
}MyQueue::~MyQueue()
{delete []m_pQueue;m_pQueue=NULL;
}void MyQueue::ClearQueue()
{m_iHead=0;m_iTail=0;m_iQueueLen=0;
}bool MyQueue::QueueEmpty() const
{if(m_iQueueLen==0)return true;elsereturn false;//return m_iQueueLen==0?true:false;
}int MyQueue::QueueLength() const
{return m_iQueueLen;
}bool MyQueue::QueueFull() const
{if(m_iQueueCapacity==m_iQueueLen)return true;elsereturn false;
}bool MyQueue::EnQueue(Customer element)
{if(QueueFull()){return false;}else{m_pQueue[m_iTail]=element;m_iTail++;m_iTail=m_iTail%m_iQueueCapacity;m_iQueueLen++;return true;}
}bool MyQueue::DeQueue(Customer &element)//element必须是引用才可以带回值
{if(QueueEmpty()){return false;}else{element=m_pQueue[m_iHead];m_iHead++;m_iHead=m_iHead%m_iQueueCapacity;m_iQueueLen--;return true;}}void MyQueue::QueueTraverse()
{for(int i=m_iHead;i<m_iHead+m_iQueueLen;i++)//从队列头遍历{m_pQueue[i%m_iQueueCapacity].printfInfo();cout<<"前面还有"<<(i-m_iHead)<<"人"<<endl;cout<<endl;}cout<<endl;
}
int main(void)
{MyQueue *p = new MyQueue(4);//使用myQueue 指定个数Customer c1("zhangsan",20);Customer c2("liis",30);Customer c3("cdcs",50);p->EnQueue(c1);p->EnQueue(c2);p->EnQueue(c3);p->QueueTraverse();cout<<"------------------"<<endl;Customer c4("",0);p->DeQueue(c4);c4.printfInfo();cout<<"-------------------"<<endl;p->QueueTraverse();}

在这里插入图片描述

【注】:遇到的一些问题
c++默认构造器问题
在这里插入图片描述

在这里插入图片描述

修改成这样,为其制定默认值
在这里插入图片描述
即ok

这篇关于数据结构探险(一)——队列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

SpringKafka错误处理(重试机制与死信队列)

《SpringKafka错误处理(重试机制与死信队列)》SpringKafka提供了全面的错误处理机制,通过灵活的重试策略和死信队列处理,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、Spring Kafka错误处理基础二、配置重试机制三、死信队列实现四、特定异常的处理策略五

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)

《解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)》该文章介绍了使用Redis的阻塞队列和Stream流的消息队列来优化秒杀系统的方案,通过将秒杀流程拆分为两条流水线,使用Redi... 目录Redis秒杀优化方案(阻塞队列+Stream流的消息队列)什么是消息队列?消费者组的工作方式每

Redis延迟队列的实现示例

《Redis延迟队列的实现示例》Redis延迟队列是一种使用Redis实现的消息队列,本文主要介绍了Redis延迟队列的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、什么是 Redis 延迟队列二、实现原理三、Java 代码示例四、注意事项五、使用 Redi

hdu1180(广搜+优先队列)

此题要求最少到达目标点T的最短时间,所以我选择了广度优先搜索,并且要用到优先队列。 另外此题注意点较多,比如说可以在某个点停留,我wa了好多两次,就是因为忽略了这一点,然后参考了大神的思想,然后经过反复修改才AC的 这是我的代码 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)