📄 queue.h
字号:
#ifndef QUEUE_CLASS
#define QUEUE_CLASS
// maximum size of a queue list
const int MaxQueueSize = 1000;
template<class T>
class Queue
{
private:
int front, rear, count;
T qlist[MaxQueueSize]; // array implementation
public:
Queue (void); // initialize integer data members
void Insert(const T& item);
T Delete(void);
void Clear(void);
T GetFront(void) const;
int GetLength(void) const;
bool IsEmpty(void) const;
bool IsFull(void) const;
};
// initialize queue front, rear, count
template<class T>
Queue<T>::Queue (void) : front(0), rear(0), count(0)
{}
// insert item into the queue
template<class T>
void Queue<T>::Insert (const T& item)
{
// terminate if queue is full
if (count == MaxQueueSize) {
cerr << "Queue overflow!" << endl;
exit(1);
}
// increment count, assign item to qlist and update rear
count++;
qlist[rear] = item;
rear = (rear+1) % MaxQueueSize;
}
// delete element from front of queue and return its value
template<class T>
T Queue<T>::Delete(void)
{
// if qlist is empty, terminate the program
if (count == 0)
{
cerr << "Deleting from an empty queue!" << endl;
exit(1);
}
// record value at the front of the queue
T temp = qlist[front];
// decrement count, advance front and return former front
count--;
front = (front+1) % MaxQueueSize;
return temp;
}
// return value of the first entry
template<class T>
T Queue<T>::GetFront(void) const
{
return qlist[front];
}
// return number of queue elements
template<class T>
inline int Queue<T>::GetLength(void) const
{
return count;
}
// test for an empty queue
template<class T>
inline bool Queue<T>::IsEmpty(void) const
{
// return the logical value count == 0
return count == 0;
}
// test for a full queue
template<class T>
inline bool Queue<T>::IsFull(void) const
{
// return the logical value count == MaxQSize
return count == MaxQSize;
}
// clear the queue by resetting count, front and rear to 0
template<class T>
void Queue<T>::Clear(void)
{
count = 0;
front = 0;
rear = 0;
}
#endif // QUEUE_CLASS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -