隊列是一種特殊的線性表,僅能在線性表的兩端進行操作。
為灌南等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及灌南網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為做網(wǎng)站、成都網(wǎng)站制作、灌南網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!


template < typename T >
class Queue
{
public:
virtual void enqueue(const T& e) = 0;
virtual void dequeue() = 0;
virtual T front() const = 0;
virtual void clear() = 0;
virtual int length() const = 0;
};
順序隊列的實現(xiàn):
設計要點:
類模板,使用原生數(shù)組作為隊列 存儲空間,使用模板參數(shù)決定隊列的最大容量;
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()
void enqueue(const T& e)
void dequeue()
T front() const
void clear()
int length() const
int capacity() const
};注意事項:
StaticQueue實現(xiàn)要點:(循環(huán)計數(shù)法) 提高隊列操作的效率(本質上時循環(huán)隊列)
關鍵操作:
隊滿:(m_length == N) && (m_front == m_rear)
StaticQueue最終實現(xiàn):
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() //O(1)
{
m_front = 0;
m_rear = 0;
m_length = 0;
}
void enqueue(const T& e) //O(1)
{
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 staticqueue...");
}
}
void dequeue() //O(1)
{
if(m_length > 0)
{
m_front = (m_front + 1) % N;
m_length--;
}
else
{
THROW_EXCEPTION(InvalidOperationException, "no element in current staticqueue...");
}
}
T front() const //O(1)
{
if(m_length > 0)
{
return m_space[m_front];
}
else
{
THROW_EXCEPTION(InvalidOperationException, "no element in current staticqueue...");
}
}
void clear() //O(1)
{
m_front = 0;
m_rear = 0;
m_length = 0;
}
int length() const //O(1)
{
return m_length;
}
int capacity() const //O(1)
{
return N;
}
bool is_empty() //O(1)
{
return (m_length == 0) && (m_front == m_rear);
}
bool is_full() //O(1)
{
return (m_length == N) && (m_front == m_rear);
}
};
順序隊列的缺陷:當數(shù)據(jù)為類類型時,StaticQueue的對象在創(chuàng)建時,會多次調用元素類型的構造函數(shù),影響效率。所以我們采用鏈式存儲結構來實現(xiàn)隊列。
設計要點:
1.類模板,繼承自抽象父類Queue;
2.在內(nèi)部使用鏈式結構實現(xiàn)元素的存儲
3.只在鏈表的頭部和尾部進行操作。
LinkQueue聲明:
template <typename T>
class LinkQueue : public Queue<T>
{
protected:
LinkList<T> m_list;
public:
LinkQueue(){}
void enqueue(const T& e) //O(n)
void dequeue() //O(1)
T front() const //O(1)
void clear() //O(n)
int length() const //O(1)
};LinkQueue最終實現(xiàn):
template <typename T>
class LinkQueue : public Queue<T>
{
protected:
LinkList<T> m_list;
public:
LinkQueue(){}
void enqueue(const T& e) //O(n)
{
m_list.insert(e);
}
void dequeue() //O(1)
{
if(m_list.length() > 0)
{
m_list.remove(0);
}
else
{
THROW_EXCEPTION(InvalidOperationException, "no elemet in current LinkQueue...");
}
}
T front() const //O(1)
{
if(m_list.length() > 0)
{
return m_list.get(0);
}
else
{
THROW_EXCEPTION(InvalidOperationException, "no elemet in current LinkQueue...");
}
}
void clear() //O(n)
{
while (m_list.length() > 0)
{
m_list.remove(0);
}
}
int length() const //O(1)
{
return m_list.length();
}
};LinkQueue中,入隊操作由于只能操作隊列的尾部(鏈表的最后位置),要進行遍歷操作,所以時間復雜度為O(n),可以使用雙向循環(huán)鏈表代替單鏈表來解決這個問題。
網(wǎng)站題目:數(shù)據(jù)結構(07)_隊列
鏈接地址:http://chinadenli.net/article14/ppcsde.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、小程序開發(fā)、定制網(wǎng)站、定制開發(fā)、自適應網(wǎng)站、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)