顺序队列

顺序队列

//
//  顺序队列
//  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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值