队列的定义:队列是一种运算受限的线性表,它限定在表的一端进行插入操作,在表的另一端进行删除操作。
其中:
允许插入的一端称作队尾;
允许删除的一端称作队首。
队列的顺序实现(C++)
初始化操作: InitQueue()
取队首元素操作: GetHead()
入列操作: EnQueue()
出列操作: DeQueue ()
template
class Queue
{
public:
virtual bool IsEmpty() const = 0;
virtual bool IsFull() const = 0;
virtual bool Front(T &x) const = 0;
virtual bool EnQueue(T x) = 0;
virtual bool DeQueue() = 0;
virtual void Clear() = 0;
virtual int Length() = 0;
};
template
class SeqQueue :public Queue
{
public:
SeqQueue(int mSize)
{
maxSize = mSize;
q = new T[maxSize];
front = rear = 0;
}
~SeqQueue() { delete[]q; };
bool IsEmpty() const { return front == rear; }
bool IsFull() const { return (rear + 1) % maxSize == front; }
bool Front(T &x) const //在x中返回队头元素
{
if (IsEmpty()) //空队列处理
{
cout
队列的链式实现
typedef int ElemType;
typedef struct qnode
{
ElemType data;
struct qnode * next;
}DataNode;
typedef struct
{
DataNode *front;
DataNode *rear;
}LinkQuNode;
void InitQueue(LinkQuNode *&q)
{
q = new LinkQuNode;
q->front = q->rear = NULL;
}
bool IsEmpty(LinkQuNode *q)
{
return (q->rear == NULL);
}
void EnQueue(LinkQuNode *&q,ElemType Data)
{
DataNode *temp = new DataNode;
temp->data = Data;
temp->next = NULL;
if (IsEmpty(q))
{
q->rear = temp;
}
else
{
q->rear->next = temp;
q->rear = temp;
}
}
bool DelQueue(LinkQuNode *& q)
{
if (IsEmpty(q))
{
cout