-
遍历函数有误,应该是 for(int i = m_iHead; i < m_iHead + m_iQueueLen; ++i) { cout << m_pQueue[i%m_iQueueCapacity] << endl; } }查看全部
-
此处 将int改为Customer会出现报错:没有合适的默认构造函数可用。 将Custome的构造函数 附初始值Customer(string name="",int age=0);查看全部
-
class MyQueue { // 注释:讲解一些 C 语言用法 public: MyQueue(int queueCapacity); // InitQueue(&Q) 创建队列 virtual ~MyQueue(); // DestoryQueue(&Q) 销毁队列 void ClearQueue(); // ClearQueue(&Q) 清空队列 bool QueueEmpty() const; // QueueEmpty(Q)判空队列 int QueueLength() const; // QueueLength(Q) 队列长度 bool EnQueue(int element); // EnQueue(&Q, element) 新元素入队 bool DeQueue(int &element); // DeQueue(&Q, &element)首元素出队 void QueueTraverse(); // QueueTraverse(Q,visit()) 遍历队列,visit()函数:访问的方法 private: int *m_pQueue; // 队列数组指针 int m_iQueuelen; // 队列元素个数 int m_iQueueCapacity; // 队列数组容量 };
查看全部 -
成员函数中之所以加了const修饰,是为了保护对象的数据不会被修改,在函数中只能读取对象数据并不可以修改。...查看全部
-
遍历函数有错 for(int i = m_iHead; i < m_iHead + m_iQueueLen; ++i)查看全部
-
普通队列存在的缺点: 1、若是一个元素出队列后,其他元素统一前移,补充空位,则时间效率降低。 2、若是一个元素出队列后,其他元素位置保持不变,空位保留,则空间利用率低。查看全部
-
1.环形队列入队 head++; head = head % capacity; 2.环形队列出队 tail++; tail = tail % capacity; 3.遍历环形队列时要注意循环变量应初始化为队首指针所指向的数组下标,输出时要对循环变量进行求余操作(i % capacity)以防数组下标越界问题的发生 for(int i = head; i < length + head; i++) cout<<QueueData[i % capacity]<<" ";查看全部
-
这是我整理的代码 https://xiongyiming.blog.csdn.net/article/details/100619983
查看全部 -
/********************************
******** 环形队列 *******
*********************************/
#ifndef MYQUEUE_H_
#define MYQUEUE_H_
#include <iostream>
using namespace std;
const int DefaultCapacitySize = 20;
template <typename T>
class MyQueue
{
public:
MyQueue();
~MyQueue();
bool ClearQueue() ; // 清空队列
bool IsEmpty() ; // 判断队列是否为空
bool IsFull(); // 判断队列是否为满
int QueueLen(); // 返回队列长度
bool InsertQueue(const T elem); // 插入元素
bool DeleteQueue(T & elem); // 删除元素
bool TraverseQueue() ; // 遍历队列
private:
T * m_data; // 存放数据
int iHead; // 指向队列头部
int iTail; // 指向队列尾部
int m_capacity; // 队列容量
int m_length; // 队列长度
};
// 构造函数
template <typename T>
MyQueue<T>::MyQueue()
{
m_data = new T [DefaultCapacitySize];
iHead = 0;
iTail = 0;
m_capacity = DefaultCapacitySize;
m_length = 0;
}
// 析构函数
template <typename T>
MyQueue<T>::~MyQueue()
{
delete [] m_data;
m_data = NULL;
}
// 清空队列
template <typename T>
bool MyQueue<T>::ClearQueue()
{
m_length = 0;
iHead = 0;
iTail = 0;
return true;
}
// 判断队列是否为空
template <typename T>
bool MyQueue<T>::IsEmpty()
{
return (m_length == 0) ? true : false;
}
// 判断队列是否为满
template <typename T>
bool MyQueue<T>::IsFull()
{
return (m_length == m_capacity) ? true : false;
}
// 返回队列长度
template <typename T>
int MyQueue<T>::QueueLen()
{
return m_length;
}
// 插入元素
template <typename T>
bool MyQueue<T>::InsertQueue(const T elem)
{
if (IsFull())
return false;
m_data[iTail] = elem;
iTail++;
iTail %= m_capacity;
m_length++;
return true;
}
// 删除元素
template <typename T>
bool MyQueue<T>::DeleteQueue(T & elem)
{
if (IsEmpty())
return false;
elem = m_data[iHead];
iHead++;
iHead %= m_capacity;
m_length--;
return true;
}
// 遍历队列
template <typename T>
bool MyQueue<T>::TraverseQueue()
{
if (IsEmpty())
return false;
for (int i = iHead; i < m_length + iHead; i++)
{
cout << m_data[i % m_capacity] << " ";
}
cout << endl;
return true;
}
#endif
查看全部 -
普通队列存在的缺点: 1、若是一个元素出队列后,其他元素统一前移,补充空位,则时间效率降低。 2、若是一个元素出队列后,其他元素位置保持不变,空位保留,则空间利用率低。
查看全部 -
释放内存后,记得把指针指向空m_pQueue=NULL.查看全部
-
//队列有两个重要的元素:队头和队尾; //插入元素: //当我们插入一个元素时,先判断这个队列是不是满的。 //如果不是满的,就插入元素,队尾会变化 //删除元素: //当我们删除一个元素时,先判断这个队列是不是空的。 //如果不是空的,就删除元素,队头会变化查看全部
-
出队需要element返回值 所以需引用: &element element = m_pQueue[m_iHead];//出队的值 m_iHead++;//出队查看全部
-
好,太好了查看全部
-
好查看全部
举报