码迷,mamicode.com
首页 > 其他好文 > 详细

队列之数组实现

时间:2014-09-25 00:53:27      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   div   sp   

#include<iostream>
using namespace std;
struct Queue
{
    int maxCnt;
    int* elements;
    int front,rear;
};
Queue* createQueue(int max=7)
{
     Queue* queue = (Queue*)malloc(sizeof(Queue));
     queue->front=0;
     queue->rear=0;
     queue->maxCnt=max+1;//预留一个空间来预防假溢出的情况
     queue->elements = new int[ queue->maxCnt];
     return  queue;
}


bool isFull(Queue* queue)
{
    return ((queue->rear+1)%queue->maxCnt)==queue->front;
}
bool isEmpty(Queue* queue)
{
    return queue->rear==queue->front;
}
 
bool InQueue(Queue* queue,int value)
{
    if(isFull(queue))
        return 0;
    queue->elements[queue->rear++]=value;
    queue->rear%=queue->maxCnt;
    return 1;
}
bool DeQueue(Queue* queue,int* ans)
{
    if(isEmpty(queue))
      return 0;
    *ans = queue->elements[queue->front++];
    queue->front%=queue->maxCnt;
    return 1;
}
bool front(Queue* queue,int* ans)
{
    if(isEmpty(queue))
      return 0;
    *ans = queue->elements[queue->front];
    return 1;
}

void clearQueue(Queue* queue)
{
    int x;
    while(!isEmpty(queue))
    {
        DeQueue(queue,&x);
        cout<<x<<" ";
    }
    cout<<endl;
}

void outPut(Queue* queue)
{
    int st = queue->front;
    int ed = queue->rear;
    while(st!=ed)
    {
        cout<<queue->elements[st]<<" ";
        st++;
        st%=queue->maxCnt;
    }
    cout<<endl;
}

 
void main()
{
    int len=4;
    Queue* queue = createQueue();
    int v;
    for(int i=0;i<len;i++)
    {
         v = rand() % 100;
         cout<<v<<" ";
         InQueue(queue,v);

    }
    cout<<endl;
    outPut(queue);
    clearQueue(queue);

    for(int i=0;i<5;i++)
    {
         v = rand() % 100;
         cout<<v<<" ";
         InQueue(queue,v);

    }
    cout<<endl;
    outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    front(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    for(int i=0;i<5;i++)
    {
         v = rand() % 100;
         cout<<v<<" ";
         InQueue(queue,v);

    }
    cout<<endl;
    outPut(queue);
    if(!InQueue(queue,v))
        cout<<"fail"<<endl;
    else outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    DeQueue(queue,&v);
    cout<<v<<endl;
    outPut(queue);

    for(int i=0;i<3;i++)
    {
         v = rand() % 100;
         cout<<v<<" ";
         InQueue(queue,v);

    }
    cout<<endl;

    outPut(queue);

    cin>>len;
}

 

队列之数组实现

标签:style   blog   color   io   os   ar   for   div   sp   

原文地址:http://www.cnblogs.com/kbyd/p/3991674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!