顺序队列
//
// 顺序队列
// TestList
//
// Created by chenshang on 14-2-7.
// Copyright (c) 2014年 chenshang. All rights reserved.
//
#ifndef TestList_SeqQueue_h
#define TestList_SeqQueue_h
#include <iostream>
using namespace std;
typedef int T;
class SeqQueue{
public:
SeqQueue(int sz):MaxSize(sz),rear(0),front(0),count(0){
arr = new T[sz];
if (arr==NULL) {
cout<<"Application Error!"<<endl;
exit(1);
}
}
~SeqQueue(){
delete[] arr;
}
public:
void Empty();
bool isEmpty();
bool isFull();
bool Append(const T item);//插入数据
T Delete(); //删除数据
T Get(); //得到队列头的数据
void print();
private:
int rear;
int front;
int count;
int MaxSize;
T *arr;
};
void SeqQueue::Empty(){
this->rear=0;
this->front=0;
this->count=0;
}
bool SeqQueue::isEmpty(){
return count==0;
}
bool SeqQueue::isFull(){
return count==MaxSize;
}
bool SeqQueue::Append(const T item){
if (isFull()) {
cout<<"The quque is full!"<<endl;
return false;
}
//队列的顺序储存结构要使用循环队列,为了提高效率,防止越界。
arr[rear]= item;
rear =(rear+1)%MaxSize;
count++;
return true;
}
T SeqQueue::Delete(){
if (isEmpty()) {
cout<<"There is no element!"<<endl;
return false;
}
T temp = arr[front];
front=(front+1)%MaxSize;
count--;
return temp;
}
T SeqQueue::Get(){
if (isEmpty()) {
cout<<"There is no element!"<<endl;
return false;
}
return arr[front];
}
void SeqQueue::print(){
cout<<"front";
for (int i=0;i<count; i++) {
cout<<"--->"<<arr[(front+i)%MaxSize];
}
cout<<"-->rear"<<endl<<endl<<endl;
}
#endif