数据结构之队列

/*
 * Copyright (c) 2012 Fang Ying (Y. Fang), NEU Electric Engineering 20092725 ,fangying712@gmail.com
 * Copyright (c) GPLv3
 * All Rights Reserved.
 * This program is free software; 
 *you can redistribute it and/or modify it under the terms of the GNU General Public License V3 as published by the
 * Free Software Foundation, either version 2 or any later version.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details. A copy of the GNU General Public License is available at:
 * http://www.fsf.org/licensing/licenses
 */
 
 
#ifndef _SQ_QUEUE_H_
#define _SQ_QUEUE_H_
#include <iostream>

using namespace std;
//定义类模板
template <class T>

class sq_queue
{
private:
    int max;        //最大存储空间
    int front;        //队列头
    int rear;        //队列尾
    int sign;        //队列状态,0表示空。
    T *q;            //队列首地址
public:
    sq_queue(int max);            //构造函数
    ~sq_queue();                //析构函数
    void print();                //输出队列信息
    int  flag();                //返回队列标识
    void insert(T );            //新增元素
    T      remove();                    //删除元素
};

//建立容量为max的循环队列
template <class T>
sq_queue<T>::sq_queue(int max)
{
    this->max = max;    //队列初始化
    q = new T[this->max];
    front = this->max;
    rear  = max;
    sign = 0;        //表空
    return;            //表示函数的退出
}

//输出队列的头和队列中的元素
template <class T>
void sq_queue<T>::print()
{    
    int i;
    cout<<"front = "<<front<<endl;
    cout<<"rear = "<<rear<<endl;
    if (0 == sign)
        {
            cout<<" queue is empty!"<<endl;
            return;    //这一句必要的!
        }
        
    i = front;
    do
    {
        i++;
        if(max+1 == i) i = 1;
        cout<<q[i-1]<<endl;
    }while(i!=rear);
    return;
}

//检测循环队列的状态
template <class T>
int sq_queue<T>::flag()
{
    if ( (1==sign) && (rear == front))    return -1;
    if ( sign == 0)    return 0;
    return 1;
}

//入队
template <class T>
void sq_queue<T>::insert(T x)
{
    if ((sign == 1)&&(rear==front))
        {cout<<"queue overflow!"<<endl; return ;}
    rear += 1;
    if (rear == max +1) rear = 1;
    q[rear -1] = x;
    sign = 1;
    return ;
}
//退队
template <class T>
T sq_queue<T>::remove()
{
    T y;
    if( sign==0 )
        {cout<<"Queue underflow!"<<endl; return 1;}
    front += 1;
    if (front == max + 1)    front = 1;
    y = q[front-1];
    if (front == rear) sign=0;
    return(y);
}
//书序表空间回收
template <class T>
sq_queue<T>::~sq_queue()
{
    delete q;
}
#endif
#include "sq_queue.h"

int main()
{
    sq_queue<int>    q(10);
    cout<<"输出排头指针以及队尾指针:"<<endl;
    q.print();
    q.insert(50);
    q.insert(550);
    q.insert(5450);
    q.insert(45460);
    q.insert(456);
    cout<<"再次输出排头指针以及队尾指针:"<<endl;
    q.print();
    cout<<"输出退栈元素:"<<endl;
    cout<<q.remove()<<endl;
    cout<<q.remove()<<endl;
    cout<<q.remove()<<endl;
    cout<<q.remove()<<endl;
    cout<<"再次输出排头指针以及队尾指针:"<<endl;
    q.print();
    
    return 0;
}

 

posted @ 2012-06-15 17:53  fangying  阅读(432)  评论(0编辑  收藏  举报