zoukankan      html  css  js  c++  java
  • 数据结构--队列的思想与数组实现

         队列和栈一样,也是一种表,和栈不同的是队列的插入在一端进行而删除操作在另一端进行,因此队列是一种先进先出的表,队列的基本操作是入队(Enqueue),他是在表的末端(叫做队尾(rear))插入一个元素,还有出队(Dequeue),他是删除或者返回在表的开头(叫做队头(front))的元素,但这样会存在一个问题,假如一个列队的大小为10,当执行10次入队操作和9次出队操作后,队列似乎满了,因为最后一个元素放在了数组的最后一个位置,再执行入队操作将造成数组的溢出(虽然最后一个元素前面都是空的),一个简单的解决方法是只要Front或者Rear到达数组的尾端,他就绕回到开头,这叫做队列循环数组的实现。

        队列用数组实现的代码如下:

    #include <iostream>
    using namespace std;
    
    typedef struct QueueRecord *Queue;
    #define MinQueueSize  10
    struct QueueRecord
    {
       int Capacity;    //队的容量
       int Front;       //队头
       int Rear;        //队尾
       int Size;        //队中元素的个数
       int *Array;       //数组
    };
    ///////////////////函数声明////////////////////////
    int IsEmpty(Queue Q);    //判断队列是否为空
    int IsFull(Queue Q);     //判断队列是否满了
    void MakeEmpty(Queue Q);   //构造一个空列队
    Queue CreateQueue(int MaxElements);  //创建一个列队
    void DisposeQueue(Queue Q);      //释放一个列队
    void Enqueue(int x, Queue Q);    //入队
    int Dequeue(Queue Q);      //出队
    int Front(Queue Q);     //返回队首值,不出队
    
    
    ///////////////////函数定义////////////////////////
    int IsEmpty(Queue Q)
    {
       return Q->Size == 0;
    }
    
    int IsFull(Queue Q)
    {
        if(Q->Size > Q->Capacity )
        {
           cout << "queue full" << endl;
           return -1;
        }
        else
        {
           return 0;
        }
    }
    
    void MakeEmpty(Queue Q)
    {
       Q->Size = 0;
       Q->Front = 1;
       Q->Rear = 0;
    }
    
    Queue CreateQueue(int MaxElements)
    {
       Queue Q;
       if (MaxElements < MinQueueSize)
       {
          cout << "queue size is too small" << endl;
       }
       Q = static_cast<Queue> (malloc(sizeof(struct QueueRecord)));
       if(Q == NULL)
       {
          cout << "out of space!!!";
       }
       Q->Array =static_cast<int*>(malloc(sizeof(int)*MaxElements));
       if(Q->Array == NULL)
       {
         cout << "out of space!!!";
       }
       Q->Capacity = MaxElements;
       MakeEmpty(Q);
       return Q;
    }
    
    void DisposeQueue(Queue Q)
    {
        if (Q != NULL)
       {
          free(Q->Array );
          free(Q);
       }
    } 
    
    
    static int Succ(int Value, Queue Q)  //循环数组,用于回绕
    {
       if(++Value == Q->Capacity )
           Value = 0;
       return Value;
    }
    void Enqueue(int x, Queue Q)
    {
        if(IsFull(Q))
        {
           cout << "Full queue" << endl;
        }
        else
        {
           Q->Size ++;
           Q->Rear = Succ(Q->Rear, Q);
           Q->Array [Q->Rear] = x;
        }
    }
    
    
    int Dequeue(Queue Q)
    {
        if(IsEmpty(Q))
        {
           cout << "Empty Queue" << endl;
           return false;    //仅表示错误
        }
        else
        {
           Q->Size --;
           Q->Front = Succ(Q->Front, Q);
           return Q->Array[(Q->Front)-1];
        }
    }
    
    int Front(Queue Q)
    { 
        if(IsEmpty(Q))
        {
           cout << "Empty Queue" << endl;
           return false;    //仅表示错误
        }
        else 
        return Q->Array[Q->Front];
    }
    
    
    int main ()
    {
        /////////测试队列/////////////////////
      Queue queue_1 = CreateQueue(15);
      Enqueue(5,queue_1);
      Enqueue(25,queue_1);
      Enqueue(89,queue_1);
     
      cout << Dequeue(queue_1) << endl;
      cout << Dequeue(queue_1) << endl;
      cout << Dequeue(queue_1) << endl;
      cout << Front(queue_1) << endl;
    
      DisposeQueue(queue_1);
    
       return 0;
    }
    

      

        和栈这种数据结构一样,队列也可以用链表实现,最重要的还是思想。

         夜深了,离天亮似乎不远了。

  • 相关阅读:
    友盟消息推送api、python sdk问题、测试demo代码
    Django的时区设置问题
    优酷视频上传api及demo代码
    git回滚线上代码
    charles的使用
    django+ajax用FileResponse文件下载到浏览器过程中遇到的问题
    scrapy框架
    几个简单的算法
    SQLAlchemy
    redis
  • 原文地址:https://www.cnblogs.com/1242118789lr/p/6720076.html
Copyright © 2011-2022 走看看