紧接上一篇博客,这篇博客是关于数组实现队列的
🟦️环形数组实现队列(有容量限制)
好处:1)对比普通数组,起点和终点更为自由,不用考虑数据的移动
2)环意味着不会存在越界问题
3)与链表实现队列相比,数组的性能更佳
4)适合实现有界队列
方法一(m_Head,m_Tail代表头和尾,存储的是数组元素)
1️⃣创建环形数组

⚠️下标的计算:(cur+step)%len (当前的指针位置+前进步数)% 数组的长度
⚠️成员函数类外实现
template <class T>
class ArrayQueue
{
public:
ArrayQueue(int capacity)
{
m_V.resize(capacity + 1);//数组大小要在期望的容量上+1,因为要留一个位子专门存尾指针
}
private:
vector<T> m_V;
int m_Head=0;//相当于头指针
int m_Tail=0;//相当于尾指针
};
2️⃣判断队列是否为空myIsempty();和判断队列是否满了myIsfull();

template <class T>
bool ArrayQueue<T>::myIsempty()
{
return m_Head == m_Tail;
}
template <class T>
bool ArrayQueue<T>::myIsfull()
{
return (m_Tail + 1) % m_V.size() == m_Head;
}
3️⃣从队列尾部添加节点myPush();

template <class T>
bool ArrayQueue<T>::myPush(T value)
{
if (this->myIsfull())
{
return false;
}
m_V[m_Tail] = value;
m_Tail = (m_Tail + 1) % m_V.size();
return true;
}
4️⃣从队列头部删除节点myPop();

template<class T>
bool ArrayQueue<T>::myPop()
{
if (this->myIsempty())
{
return false;
}
m_Head = (m_Head + 1) % m_V.size();
return true;
}
5️⃣返回队列第一个的值myFront();
template<class T>
T ArrayQueue<T>::myFront()
{
if (this->myIsempty())
{
return NULL;
}
return m_V[m_Head];
}
6️⃣返回队列最后一个的值myBack();
template<class T>
T ArrayQueue<T>::myBack()
{
if (this->myIsempty())
{
return NULL;
}
return m_V[(m_Tail-1)%m_V.size()];
}
⚠️在STL标准模板库中,容器queue不能遍历,但为了能看到测试结果,这里提供遍历代码
template<class T>
void ArrayQueue<T>::Loop()
{
for (int i = m_Head; i != m_Tail; i=(i+1)%m_V.size())
{
cout << m_V[i] << ' ';
}
cout << endl;
}
方法二(m_Head,m_Tail不再存储数组元素,而是不断递增的指针值)
队列功能实现思路和方法一几乎一模一样,这里提供整体实现代码
/*#include<iostream>
#include<vector>
using namespace std;
template <class T>
class ArrayQueue2
{
public:
ArrayQueue2(int capacity)
{
m_V.resize(capacity);
}
bool myIsfull();//判断环形数组是否满了
bool myIsEmpty();//判断环形数组是否为空
bool myPush(T value);//向队尾添加元素
T myPop();//从队头移除元素
T myFront();//获取第一个元素的值
T myBack();//获取最后一个元素的值
void Loop();//遍历队列
private:
vector<T> m_V;
//当类型为int时,可能值会超过最大正整数,改为unsigned int
unsigned int m_Head = 0;
unsigned int m_Tail = 0;
};
template<class T>
bool ArrayQueue2<T>::myIsfull()
{
return m_Tail-m_Head==m_V.size();
}
template<class T>
bool ArrayQueue2<T>::myIsEmpty()
{
return m_Head==m_Tail;
}
template<class T>
bool ArrayQueue2<T>::myPush(T value)
{
if (this->myIsfull())
{
return false;
}
m_V[m_Tail % m_V.size()] = value;
m_Tail++;
return true;
}
template<class T>
bool ArrayQueue2<T>::myPop()
{
if (this->myIsEmpty())
{
return false;
}
m_Head++;
return true;
}
template<class T>
T ArrayQueue2<T>::myFront()
{
if (this->myIsEmpty())
{
return NULL;
}
T ret = m_V[m_Head % m_V.size()];
return ret;
}
template<class T>
T ArrayQueue3<T>::myBack()
{
if (this->myIsEmpty())
{
return NULL;
}
T ret = m_V[m_Tail % m_V.size()];
return ret;
}
template<class T>
void ArrayQueue2<T>::Loop()
{
for (int i = m_Head; i != m_Tail; i++)
{
cout << m_V[i % m_V.size()] << ' ';
}
cout << endl;
}
void test01()//测试案例
{
ArrayQueue2<int> q(4);
q.myPush(10);
q.myPush(20);
q.myPush(30);
q.myPush(40);
q.Loop();
q.myPop();
q.Loop();
q.myPush(50);
q.Loop();
cout << q.myFront() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
这篇blog到这里就结束了(完结撒花🎉🎉🎉)
创作不易,还望各位多多支持🌹🌹🌹
(一次性写了两篇blog,佩服自己😏😏😏)

514

被折叠的 条评论
为什么被折叠?



