平时编程里经常需要用到数据结构,比如  栈和队列 等,  为了避免每次用到都需要重新编写的麻烦现将  C++ 编写的 数据结构 队列  记录下来,以备后用。

将 数据结构  队列  用头文件的形式写成,方便调用。


#ifndef QUEUE_CLASS
#define QUEUE_CLASS

#include<iostream>
#include<cstdlib>
using namespace std;
const int MaxQSize=50;

template <class T>
class Queue
{
private:
int front, rear, count;
T qlist[MaxQSize];
public:
Queue(void);

void QInsert(const T &item);
T QDelete(void);
void ClearQueue(void);
T QFront(void) const;

int QLength(void) const;
int QEmpty(void) const;
int QFull(void) const;
};

//默认构造函数
template <class T>
Queue<T>::Queue(void):front(0), rear(0), count(0)
{}

template <class T>
void Queue<T>::QInsert(const T &item)
{
if(count==MaxQSize)
{
cerr<<"Queue overflow!"<<endl;
exit(1);
}
count++;
qlist[rear]=item;
rear=(rear+1)%MaxQSize;
}

template <class T>
T Queue<T>::QDelete(void)
{
T temp;
if(count==0)
{
cerr<<"Deleting from an empty queue!"<<endl;
exit(1);
}
count--;
temp=qlist[front];
front=(front+1)%MaxQSize;

return temp;
}

template <class T>
T Queue<T>::QFront(void) const
{
return qlist[front];
}

template <class T>
int Queue<T>::QLength(void) const
{
return count;
}

template <class T>
int Queue<T>::QEmpty(void) const
{
return count==0;
}

template <class T>
int Queue<T>::QFull(void) const
{
return count==MaxQSize;
}

template <class T>
void Queue<T>::ClearQueue(void)
{
front=0;
rear=0;
count=0;
}
#endif


具体的调用形式:

C++  泛型  编写的  数据结构    队列_ios

运行结果:

C++  泛型  编写的  数据结构    队列_数据结构_02