几个月前,去一软件公司面试,要求写一个循环队列,要求能编译,功能基本全。
class circular_queue
{
private:
enum {max_len = 10};
int buffer[max_len];
int head;
int rear;
public:
circular_queue():head(0),rear(0){};
virtual ~circular_queue(){};
bool is_empty()
{
return head == rear;
}
bool is_full()
{
return head == ((rear+1) % max_len);
}
bool push(int t)
{
if(is_full())
return false;
buffer[rear] = t;
rear = (rear+1) % max_len;
return true;
}
int front()
{
return buffer[head];
}
void pop()
{
if (is_empty()) return;
head = (head+1) % max_len;
}
};