📄 queue.cpp
字号:
#include <iostream.h>
template<class T>
class queue
{public:
queue(int = 50);
~queue( ) { delete [] sqPtr; }
int enqueue(const T&); //入队
int dlqueue(T&); //出队
int gethead(T&);
int isempty() const { return front == rear; } //注意
int isfull() const {return rear == size - 1; }
private:
int front,rear,size;
T *sqPtr;
};
template<class T>
queue<T>::queue(int s)
{ size = s > 0 && s < 1000 ? s : 50;
front=rear=-1; //注意
sqPtr = new T[size];}
//input an element into the queue
template<class T>
int queue<T>::enqueue(const T &item)
{
if( !isfull( ) )
{ sqPtr[++rear]=item;
return 1;
}
return 0;
}
//output an element off the queue
template<class T>
int queue<T>::dlqueue(T &Value)
{
if ( !isempty( ) )
{
Value = sqPtr[++front];
return Value;
}
return 0;
}
//get the head of the queue
template<class T>
int queue<T>::gethead(T &head)
{
if ( !isempty( ) )
{
head=sqPtr[++front];
front--;
return head;
}
return 0;
}
void main( )
{
queue<int> intqueue(5);
int a=1;
cout << "enqueue!" << endl;
while ( intqueue.enqueue(a) ) {cout << a << " "; a += 1;}
cout << endl << "queue is full. Cannot input " << a << endl;
intqueue.gethead(a);
cout<<"the head of the queue is : "<< a <<endl;
cout << "dlqueue!" << endl;
while ( intqueue.dlqueue(a) ) cout << a << " ";
cout << endl << "queue is empty. Cannot output " << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -