36、队列的概念及实现(上)

本文详细介绍了队列这种特殊线性表的数据结构,包括其基本概念、操作方式及两种主要实现方式:静态队列(StaticQueue)和链式队列(LinkQueue)。静态队列使用固定大小的数组实现,而链式队列则利用链表实现,适用于不同应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列是一种特殊的线性表。

队列仅能在线性表的两端进行操作

队头(Front):取出数据元素的一端。

队尾(Rear):插入数据元素的一端。

队列的特性:先进先出(First in first out)

操作:创建队列(queue()),销毁,清空,进,出,获取队头,长度

StaticQueue设计要点:类模板,使用原生数组作为队列的存储空间,使用模板参数决定队列的最大容量。

StaticQueue实现要点(循环计数法(高效))

关键操作:

进队列:m_space[m_rear]=e;m_rear=(m_rear+1)%N //取余

出队列:m_front=(m_front+1)%N;

队列的状态:

队空:(m_length==0)&&(m_front==m_rear)

队满:(m_length==N)&&(m_front==m_rear)

#ifndef STATICQUEUE_H_
#define STATICQUEUE_H_
#include "Queue.h"
#include "Exception.h"
namespace WSlib
{
template<typename T,int N>
class StaticQueue:public Queue<T>
{
protected:
T m_space[N];
int m_front;
int m_rear;
int m_length;
public:
StaticQueue()
{
m_front=0;
m_rear=0;
m_length=0;
}
int capacity()const
{
return N;
}

void add(const T& e)
{
if(m_length<N)
{
m_space[m_rear]=e;
m_rear=(m_rear+1)%N;
m_length++;
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No space in current queue");
}
}
void remove()
{
if(m_length>0)
{
m_front=(m_front+1)%N;
m_length--;
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
T front() const
{
if(m_length>0)
{
return m_space[m_front];
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
void clear()
{
m_front=0;
m_rear=0;
m_length=0;
}
int length()const
{
return m_length;
}
};
}
#endif
/**************************************************8
#include <iostream>
#include "StaticQueue.h"
using namespace std;
using namespace WSlib;


int main()
{
StaticQueue<int,5> q ;
for(int i=0;i<5;i++)
{
q.add(i);
}
while(q.length()>0)
{
cout<<q.front()<<endl;
q.remove();
}
return 0;
}
******************************************************/

小结:队列是一种特殊的线性表,具有先进先出的特性。队列只允许在线性表的两端进行操作,一端进,一端出。StaticQueue使用原生数组作为内部存储空间。StaticQueue的最大容量由模板参数决定。StaticQueue采用循环计数法提高队列操作的效率。

37、链式队列的设计要点

类模板,抽象父类Queue的直接子类,在内部使用链式结构实现元素的存储,只在链表的头部和尾部进行操作。

使用LinkList类实现链式队列是否合适,是否有更好的方案?

#ifndef LINKQUEUE_H_
#define LINKQUEUE_H_
#include "Queue.h"
#include "LinkList.h"


namespace WSlib
{
template <typename T>
class LinkQueue:public Queue<T>
{
protected:
LinkList<T> m_list;
public:
LinkQueue()
{

}


void add(const T& e)  //o(n)
{
m_list.insert(e); //尾部
}
void remove()    //o(1)
{
if(m_list.length()>0)
{
m_list.remove(0); //头部
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
T front() const   //o(1)
{
if(m_list.length()>0)
{
return m_list.get(0);
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
void clear() //o(n)
{
m_list.clear();
}
int length()const //o(1)
{
return m_list.length();
}
};
}
#endif
/**************************
#include <iostream>
#include "LinkQueue.h"
using namespace std;
using namespace WSlib;
int main()
{
LinkQueue<int>lq;
for(int i=0;i<10;i++)
{
lq.add(i);
}
while(lq.length()>0)
{
cout<<lq.front()<<endl;
lq.remove();
}
return 0;
}

*************************/

基于linux:

#ifndef LINKQUEUE_LINUX_H_
#define LINKQUEUE_LINUX_H_
#include "Queue.h"
#include "LinuxList.h"
#include "Exception.h"
namespace WSlib
{
template <typename T>
class LinkQueue_linux:public Queue<T>
{
protected:
struct Node:public Wobject
{
list_head head;//链表头成员
T value;
};
list_head m_header;
int m_length;


public:
LinkQueue_linux()  //o(1)
{
m_length=0;
INIT_LIST_HEAD(&m_header);
}


void add(const T& e)  //o(1)
{
Node* node=new Node();
if(node != NULL)
{
node->value=e;
list_add_tail(&node->head,&m_header);
m_length++;
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
void remove()    //o(1)
{
if(m_length>0)
{
list_head* toDel=m_header.next;
list_del(toDel);
m_length--;
delete list_entry(toDel,Node,head);
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
T front() const   //o(1)
{
if(m_length>0)
{
return list_entry(m_header.next,Node,head)->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException,"No element in current queue");
}
}
void clear() //o(n)
{
while(m_length>0)
{
remove();
}
}
int length()const //o(1)
{
return m_length;
}
~LinkQueue_linux() //o(n)
{
clear();
}
};
}
#endif
/******************************************************
#include <iostream>
#include "LinkQueue_linux.h"
#include "StaticQueue.h"
using namespace std;
using namespace WSlib;
class Test:public Wobject
{
public:
Test()
{
cout<<"Test()"<<endl;
}
~Test()
{
cout<<"~Test()"<<endl;
}
};
int main()
{
LinkQueue_linux <Test> lq;
StaticQueue<Test,5> l;
for(int i=0;i<10;i++)
{
//lq.add(i);
}
while(lq.length()>0)
{
//cout<<lq.front()<<endl;
lq.remove();
}
return 0;
}

********************************************************/

StaticQueue在初始化时可能多次调用元素类型的构造函数,LinkList的组合使用能够实现队列的功能(插入尾部效率低),但是不够高效,LinkQueue的最终实现组合使用了Linux内核链表,LinkQueue_linux中入队和出队操作可以在常量时间内完成。


内容概要:本文档详细介绍了基于MATLAB实现的无人机三维路径规划项目,核心算法采用蒙特卡罗树搜索(MCTS)。项目旨在解决无人机在复杂三维环境中自主路径规划的问题,通过MCTS的随机模拟与渐进式搜索机制,实现高效、智能化的路径规划。项目不仅考虑静态环境建模,还集成了障碍物检测与避障机制,确保无人机飞行的安全性和效率。文档涵盖了从环境准备、数据处理、算法设计与实现、模型训练与预测、性能评估到GUI界面设计的完整流程,并提供了详细的代码示例。此外,项目采用模块化设计,支持多无人机协同路径规划、动态环境实时路径重规划等未来改进方向。 适合人群:具备一定编程基础,特别是熟悉MATLAB和无人机技术的研发人员;从事无人机路径规划、智能导航系统开发的工程师;对MCTS算法感兴趣的算法研究人员。 使用场景及目标:①理解MCTS算法在三维路径规划中的应用;②掌握基于MATLAB的无人机路径规划项目开发全流程;③学习如何通过MCTS算法优化无人机在复杂环境中的飞行路径,提高飞行安全性和效率;④为后续多无人机协同规划、动态环境实时调整等高级应用打下基础。 其他说明:项目不仅提供了详细的理论解释和技术实现,还特别关注了实际应用中的挑战和解决方案。例如,通过多阶段优化与迭代增强机制提升路径质量,结合环境建模与障碍物感知保障路径安全,利用GPU加速推理提升计算效率等。此外,项目还强调了代码模块化与调试便利性,便于后续功能扩展和性能优化。项目未来改进方向包括引入深度强化学习辅助路径规划、扩展至多无人机协同路径规划、增强动态环境实时路径重规划能力等,展示了广阔的应用前景和发展潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值