-
數(shù)據(jù)結(jié)構(gòu)是指查看全部
-
FIFO先進(jìn)先出查看全部
-
普通隊列費時間費空間
環(huán)形隊列優(yōu)先級比普通隊列高,可以充分利用空間
查看全部 -
兩種隊列處理方式,各有優(yōu)劣,更具具體的情況處理
內(nèi)存占用少,但是效率低下
內(nèi)存占用多,效率較高。使用環(huán)形隊列處理,head/tail;采用順時針或逆時針皆可,當(dāng)隊頭隊尾在使用是具有高效低內(nèi)存的優(yōu)點,不適用順序隊列。使用環(huán)形隊列作為編碼典范。(應(yīng)用:自動排號機)
查看全部 -
隊列的用途
查看全部 -
好查看全部
-
/********************************
********? ? ?環(huán)形隊列? ? ?*******
*********************************/
#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;? // 存放數(shù)據(jù)
int iHead;? ?// 指向隊列頭部
int iTail;? ?// 指向隊列尾部
int m_capacity; // 隊列容量
int m_length;? ?// 隊列長度
};
// 構(gòu)造函數(shù)
template <typename T>
MyQueue<T>::MyQueue()
{
m_data = new T [DefaultCapacitySize];
iHead = 0;
iTail = 0;
m_capacity = DefaultCapacitySize;
m_length = 0;
}
// 析構(gòu)函數(shù)
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.環(huán)形隊列入隊 head++; head = head % capacity; 2.環(huán)形隊列出隊 tail++; tail = tail % capacity; 3.遍歷環(huán)形隊列時要注意循環(huán)變量應(yīng)初始化為隊首指針?biāo)赶虻臄?shù)組下標(biāo),輸出時要對循環(huán)變量進(jìn)行求余操作(i % capacity)以防數(shù)組下標(biāo)越界問題的發(fā)生 for(int i = head; i < length + head; i++) ?cout<<QueueData[i % capacity]<<" ?";
查看全部 -
class MyQueue { // 注釋:講解一些 C 語言用法 public: MyQueue(int queueCapacity); // InitQueue(&Q) 創(chuàng)建隊列 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()函數(shù):訪問的方法 private: int *m_pQueue; // 隊列數(shù)組指針 int m_iQueuelen; // 隊列元素個數(shù) int m_iQueueCapacity; // 隊列數(shù)組容量 };
查看全部 -
普通隊列存在的缺點: 1、若是一個元素出隊列后,其他元素統(tǒng)一前移,補充空位,則時間效率降低。 2、若是一個元素出隊列后,其他元素位置保持不變,空位保留,則空間利用率低。
查看全部 -
隊列:先入先出。
查看全部 -
#include?<iostream> #include?<string> using?namespace?std; class?Customer{ public: Customer(){ //需要默認(rèn)構(gòu)造函數(shù) } Customer(string?name,?int?age){ m_strName?=?name; m_iAge?=?age; } void?printInfo()?const{ cout?<<?"姓名:?"?<<?m_strName?<<?endl; cout?<<?"年齡:?"?<<?m_iAge?<<?endl; cout?<<?endl; } private: string?m_strName; int?m_iAge; }; template?<class?T> class?MyQueue{ public: MyQueue(int?queueCapacity){ m_iQueueCapacity?=?queueCapacity; m_iQueueLen?=?0; m_iHead?=?0; m_iTail?=?0; m_pQueue?=?new?T[queueCapacity]; } ~MyQueue(){ delete[]?m_pQueue; } void?QueueClear(){ m_iQueueLen?=?0; m_iHead?=?0; m_iTail?=?0; } bool?QueueEmpty()?const{ if(m_iQueueLen==0){ return?true; } else{ return?false; } } bool?QueueFull()?const{ if(m_iQueueLen==m_iQueueCapacity){ return?true; } else{ return?false; } } bool?EnQueue(T?element){ if(QueueFull()){ return?false; } else{ m_pQueue[m_iTail]?=?element; m_iTail?++; m_iTail?=?m_iTail?%?m_iQueueCapacity; m_iQueueLen?++; return?true; } } bool?DeQueue(T?&element){ if(QueueEmpty()){ return?false; } else{ element?=?m_pQueue[m_iHead]; m_iHead?++; m_iHead?=?m_iHead?%?m_iQueueCapacity; m_iQueueLen?--; return?true; } } void?QueueTraverse(){ for(int?i?=?m_iHead;?i?<?m_iHead?+?m_iQueueLen;?i++){ m_pQueue[i%m_iQueueCapacity].printInfo(); } } private: T*?m_pQueue; int?m_iHead; int?m_iTail; int?m_iQueueLen; int?m_iQueueCapacity; }; int?main(int?argc,?char?*argv[])?{ MyQueue<Customer>*?p?=?new?MyQueue<Customer>(4); p->EnQueue(Customer("imooc",?20)); p->QueueTraverse(); }
查看全部 -
1
查看全部 -
請輸入筆記內(nèi)容...
查看全部
舉報