注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
面的 embedded software engineer,面了两轮是因为第一轮那个组面完不知道什么原因觉得不合适,把我扔给了另一个组。
第一轮电面,给一个 Exampl关于起点和终点的更新稍微糊涂了几分种,唉。
不多说,这是楼主事后写的一个可以跑的。
- #include <stdio.h>
- #include <stdlib.h>
- class NewQueue {
- public:
- NewQueue();
- ~NewQueue();
- void creatNew(int size);
- bool enQueue(int& element); // return true if data is not been overwritten
- bool deQueue(int& element); // return is data is been successfully poped
- bool isFull();
- bool isEmpty();
- private:
- int capacity;
- int head; // head points to the next element to be poped
- int tail; // tail points to the next position for insertion
- bool coincide; // indicate queue empty if head == tail
- int* data;
- };
- // both head and tail moves forward
- // no need to track the size of the buffer, no member function uses it directly
- NewQueue::NewQueue() {
- head = 0;
- tail = 0;
- coincide = true;
- data = NULL;
- }
- NewQueue::~NewQueue() {
- if (data != NULL)
- free(data);
- }
- void NewQueue::creatNew(int size) {
- data = (int*) malloc(size*sizeof(int));
- capacity = size;
- }
- bool NewQueue::isFull() {
- return (head == tail && !coincide);
- }
- bool NewQueue::isEmpty() {
- return (head == tail && coincide);
- }
- bool NewQueue::enQueue(int& element) {
- bool result = isFull();
- if (isEmpty())
- coincide = false;
- data[tail] = element;
- tail = (tail + 1) % capacity;
- printf("enQueue success: %d", element);
- if (result) {
- head = (head + 1) % capacity;
- printf(" (overwritting element)");
- }
- printf("\n");
- return result;
- }
- bool NewQueue::deQueue(int& element) {
- if (isEmpty()) {
- printf("deQueue failed\n");
- return false;
- }
- element = data[head];
- head = (head + 1) % capacity;
- if (head == tail)
- coincide = true;
- printf("deQueue success: %d\n", element);
- return true;
- }
- int main() {
- NewQueue q;
- q.creatNew(5);
- int var;
- for (int i=0; i<10; i++) {
- q.enQueue(i);
- }
- for (int i=0; i<10; i++) {
- q.deQueue(var);
- }
- return 0;
- }
复制代码
|