佇列的物理實現(順序佇列+鏈式佇列)
阿新 • • 發佈:2018-12-27
- 說明
- 基於順序表實現的順序佇列
- 基於連結串列實現的鏈式佇列
一、說明
本文基於順序表實現的順序佇列中,陣列的大小為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
同順序佇列