Sicily 2302. Queue Implementation Using a Circular Array 解题报告
2013-09-19 20:41
351 查看
题目:
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.
};
首先简单说明一下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
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB , Framework JudgeDescription
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
NoneOutput
NoneHint
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
#include<iostream> using namespace std; template<typename T>class Queue{ T data[100]; int begin,end; public: Queue(){ begin=0; end=0; } Queue(const Queue &rhs){ begin=rhs.begin; end=rhs.end; for(int i=0;i<100;i++){ data[i]=rhs.data[i]; } } ~Queue(){ } bool empty()const{ return begin==end; } bool full() const{ return size()==100; } int size() const{ return (end-begin+100)%100;//end可能比begin小 } bool push(const T &x){ if(size()==100) return false; else{ data[end]=x; end++; if(end==100) end=0; return true; } } bool pop(){ if(empty()) return false; else{ begin++; if(begin==100) begin=0; return true; } } const T & front() const{ return data[begin]; } };
相关文章推荐
- Sicily 2302. Queue Implementation Using a Circular
- soj 2302. Queue Implementation Using a Circular Array
- Sicily 2500. 看风景 解题报告
- LeetCode-Kth Largest Element in an Array-解题报告
- 【LeetCode】189.Rotate Array(Easy)解题报告
- [leetcode]215. Kth Largest Element in an Array@Java解题报告
- [LeetCode]Product of Array Except Self,解题报告
- [leetcode] 225. Implement Stack using Queues 解题报告
- Sicily 1156. Binary tree 解题报告
- 【LeetCode】Implement Queue using Stacks 解题报告
- [leetcode] 360. Sort Transformed Array 解题报告
- [LeetCode] Search in Rotated Sorted Array 解题报告
- Sicily 2502. 买珍珠 解题报告
- 【LeetCode】Search in Rotated Sorted Array 解题报告
- Sicily 7531. Chains 解题报告
- Rotated Array 旋转数组的解题报告--leetcode
- [Array]-118. Pascal's Triangle(@python)解题报告
- Partition Array by Odd and Even 解题报告
- [Array]-189. Rotate Array(@python)解题报告
- Sicily 1936. Knight Moves 解题报告