zoukankan      html  css  js  c++  java
  • Sicily 2302. Queue Implementation Using a Circular Array 解题报告

    题目:

    Constraints

    Time Limit: 1 secs, Memory Limit: 256 MB , Framework Judge

    Description

    template <typename T> class Queue {
    public:
         Queue();   // construct an empty queue
          ~Queue()  // destructor
          Queue(const Queue &rhs);
          const Queue & operator(const Queue &rhs)
          bool empty()const;
          bool full()const;
          int size()const;
          bool push(const T &x);//enqueue
          bool pop();//dequeue
          const T & front()const;//returns a reference to the front element
    private:
     //using a static array of size 100.   

    };

    Input

    None

    Output

    None

    Hint

    Submit your implementation only.

    首先简单说明一下queue的用法:

    back()返回队列最后一个元素引用
    empty()是检查是否为空的方法 
    front()获得队列最前面一个元素引用
    push()在队列尾添加一个数据
    pop()删除队列头的一个数据
    size()队列中元素个数

    用数组实现双端队列,数据存放在容量100的数组中,用begin,end两个下标实现对数组开头与末尾的移动或者读写操作。

    当begin=end的时候说明队列为空

    需要注意的是判断size的时候,由于end加到100之后要继续存储数据必须将end重新赋值为0,所以end-begin可能为负值,解决办法是size=(100+end-begin)%100

    begin加到100的时候也要重新赋值为0

     1 #include<iostream>
     2 using namespace std;
     3 
     4 template<typename T>class Queue{
     5     T data[100];
     6     int begin,end;
     7 public:
     8     Queue(){
     9         begin=0;
    10         end=0;
    11     }
    12     Queue(const Queue &rhs){
    13         begin=rhs.begin;
    14         end=rhs.end;
    15         for(int i=0;i<100;i++){
    16             data[i]=rhs.data[i];
    17         }
    18     }
    19     ~Queue(){
    20     }
    21     bool empty()const{
    22         return begin==end;
    23     }
    24     bool full() const{
    25         return size()==100;
    26     }
    27     int size() const{
    28         return (end-begin+100)%100;//end可能比begin小
    29     }
    30     bool push(const T &x){
    31         if(size()==100)
    32             return false;
    33         else{
    34             data[end]=x;
    35             end++;
    36             if(end==100)
    37                 end=0;
    38             return true;
    39         }
    40     }
    41     bool pop(){
    42         if(empty())
    43             return false;
    44         else{
    45             begin++;
    46             if(begin==100)
    47                 begin=0;
    48             return true;
    49         }
    50     }
    51     const T & front() const{
    52         return data[begin];
    53     }
    54 };
  • 相关阅读:
    使用Netty4实现基本的消息分发
    【Netty官方文档翻译】引用计数对象(reference counted objects)
    nio复习总结
    redis tutorail
    服装设计
    linux nat网络配置
    关闭linux退格键和vi发出的嘟嘟声
    CentOS/Linux 网卡设置 IP地址配置
    WCF Security基本概念(转载)
    WCF使用net.tcp寄宿到IIS中(转)
  • 原文地址:https://www.cnblogs.com/jolin123/p/3329959.html
Copyright © 2011-2022 走看看