- 说明
- 基于顺序表实现的顺序队列
- 基于链表实现的链式队列
一、说明
本文基于顺序表实现的顺序队列中,数组的大小为n+1,但只存储n个元素,方便区分满队列和空队列;基于链表实现的链式队列中,front始终指向头结点(数据域为空),rear指向队列的尾结点(数据域不为空)。
二、基于顺序表实现的顺序队列
Queue.h
#include<iostream>
using namespace std;
#ifndef _Queue
#define _Queue
namespace wangzhe
{
template<typename E>
class Queue
{
private:
void operator = (const Queue&) {}
Queue(const Queue&) {}
public:
Queue() {}
virtual ~Queue() {}
virtual void clear() =0;
virtual void enqueue(const E&) =0;
virtual E dequeue() =0;
virtual const E& frontValue() const =0;
virtual int length() const =0;
};
}
#endif
AQueue.h
#include<iostream>
using namespace std;
#include"Queue.h"
#ifndef _AQueue
#define _AQueue
namespace wangzhe
{
template<typename E>
class AQueue:public Queue<E>
{
private:
int maxSize;//最大容量
int front;//头
int rear;//尾
E *listArray;//表
public:
AQueue(int size);//构造函数
~AQueue();//析构函数
void clear();//清空元素
void enqueue(const E& it);//入队列
E dequeue();//出队列
const E& frontValue() const;//先入先出,队首的值
int length() const;//元素个数
};
}
#endif
AQueue.cpp
#include<iostream>
using namespace std;
#include"AQueue.h"
namespace wangzhe
{
template<typename E>
AQueue<E>::AQueue(int size)
{
maxSize=size+1;
rear=0;
front=1;
listArray=new E[maxSize];
}
template<typename E>
AQueue<E>::~AQueue()
{
delete [] listArray;
}
template<typename E>
void AQueue<E>::clear()
{
rear=0;
front=1;
}
template<typename E>
void AQueue<E>::enqueue(const E& it)
{
if((rear+2)%maxSize==front)
{
cout<<"Queue is full"<<endl;
return;
}
rear=(rear+1)%maxSize;
listArray[rear]=it;
}
template<typename E>
E AQueue<E>::dequeue()
{
if(length()==0)
{
cout<<"Queue is empty"<<endl;
return -1;//以-1作为出错的返回值,不严谨
}
E it=listArray[front];
front=(front+1)%maxSize;
return it;
}
template<typename E>
const E& AQueue<E>::frontValue() const
{
if(length()==0)
{
cout<<"Queue is empty"<<endl;
return -1;//以-1作为出错的返回值,不严谨
}
return listArray[front];
}
template<typename E>
int AQueue<E>::length() const
{
return ((rear+maxSize)-front+1)%maxSize;
}
}
main.cpp
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include"AQueue.h"
#include"AQueue.cpp"
using namespace wangzhe;
int main(int argc, char** argv)
{
AQueue<int> a(10010);
a.enqueue(2);
a.enqueue(5);
a.enqueue(0);
cout<<a.dequeue()<<endl;
cout<<a.frontValue()<<endl;
cout<<a.length();
return 0;
}
三、基于链表实现的链式队列
Link.h
#include <iostream>
using namespace std;
#ifndef _Link
#define _Link
namespace wangzhe
{
template<typename E>
class Link
{
public:
E element;
Link *next;
Link(const E& elemval,Link* nextval =NULL)
{
element=elemval;
next=nextval;
}
Link(Link* nextval=NULL)
{
next=nextval;
}
};
}
#endif
Queue.h
同顺序队列
LQueue.h
#include<iostream>
using namespace std;
#include"Link.h"
#include"Queue.h"
#ifndef _LQueue
#define _LQueue
namespace wangzhe
{
template<typename E>
class LQueue:public Queue<E>
{
private:
Link<E>* front;
Link<E>* rear;
int size;
public:
LQueue(int sz);//构造函数
~LQueue();//析构函数
void clear();//清空元素
void enqueue(const E& it);//入队列
E dequeue();//出队列
const E& frontValue() const;//队首元素
int length() const; //元素个数
};
}
#endif
LQueue.cpp
#include<iostream>
using namespace std;
#include"LQueue.h"
namespace wangzhe
{
template<typename E>
LQueue<E>::LQueue(int sz)
{
front=rear=new Link<E>();
size=0;
}
template<typename E>
LQueue<E>::~LQueue()
{
clear();
delete front;
}
template<typename E>
void LQueue<E>::clear()
{
while(front->next!=NULL)
{
rear=front->next;
delete rear;
}
rear=front;
size=0;
}
template<typename E>
void LQueue<E>::enqueue(const E& it)
{
rear->next=new Link<E>(it,NULL);
rear=rear->next;
size++;
}
template<typename E>
E LQueue<E>::dequeue()
{
if(size==0)
{
cout<<"Queue is empty"<<endl;
return -1;//以-1作为出错的返回值,不严谨
}
Link<E>* temp=front->next;
E it=temp->element;
front->next=temp->next;
if(rear==temp) rear=front;
delete temp;
size--;
return it;
}
template<typename E>
const E& LQueue<E>::frontValue() const
{
if(size==0)
{
cout<<"Queue is empty"<<endl;
return -1;//以-1作为出错的返回值,不严谨
}
return front->next->element;
}
template<typename E>
int LQueue<E>::length() const
{
return size;
}
}
main.cpp
同顺序队列