zoukankan      html  css  js  c++  java
  • 队列之数组实现

    #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;
    }
  • 相关阅读:
    The Castle
    洛谷七月月赛
    Superprime Rib
    Leetcode 记录(201~300)
    03爬虫 爬取hfutxc成绩
    Leetcode 记录(101~200)
    LeetCode Weekly Contest 32
    Leetcode 记录(1~100)
    C++,java信息,文件传输
    毕业设计-自然场景下显著目标的检测
  • 原文地址:https://www.cnblogs.com/kbyd/p/3991674.html
Copyright © 2011-2022 走看看