队列(Queue)也是一种抽象数据类型,其同样定义了两个基本操作,入队(enqueue)以及出队(dequeue),其遵循先进先出原则(FIFO, first in, first out),就像我们去买火车票排的那个队列一样,遵循先来后到,先来的先买,买完后出队(从队头删除),后来的自动站在队尾(在队尾添加)。
inttop(bool pop_last) { int from = q[0].empty() ? 1 : 0; int to = 1 - from; int size = q[from].size(); for (int i = 0; i < size - 1; ++i) { q[to].push(q[from].front()); q[from].pop(); } int ret = q[from].front(); if (!pop_last) { q[to].push(ret); } q[from].pop(); return ret; } public: /** Initialize your data structure here. */ MyStack() {
}
/** Push element x onto stack. */ voidpush(int x) { (q[0].empty() ? q[1] : q[0]).push(x); }
/** Removes the element on top of the stack and returns that element. */ intpop() { returntop(true); }
/** Get the top element. */ inttop(){ returntop(false); }
/** Returns whether the stack is empty. */ boolempty(){ return q[0].empty() && q[1].empty(); } };
classMyCircularQueue { int *a; int front, rear, size, capacity; public: /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) : a(newint[k]), front(0), rear(-1), size(0), capacity(k) { }
/** Insert an element into the circular queue. Return true if the operation is successful. */ boolenQueue(int value){ if (isFull()) { returnfalse; } ++size; a[rear = (rear + 1) % capacity] = value; returntrue; }
/** Delete an element from the circular queue. Return true if the operation is successful. */ booldeQueue(){ if (isEmpty()) { returnfalse; } --size; front = (front + 1) % capacity; returntrue; }
/** Get the front item from the queue. */ intFront(){ returnisEmpty() ? -1 : a[front]; }
/** Get the last item from the queue. */ intRear(){ returnisEmpty() ? -1 : a[rear]; }
/** Checks whether the circular queue is empty or not. */ boolisEmpty(){ return size == 0; }
/** Checks whether the circular queue is full or not. */ boolisFull(){ return size == capacity; } };
classMyCircularDeque { int *a; int front, rear, size, capacity; public: /** Initialize your data structure here. Set the size of the deque to be k. */ MyCircularDeque(int k) : a(newint[k]), front(0), rear(-1), size(0), capacity(k) { }
/** Adds an item at the front of Deque. Return true if the operation is successful. */ boolinsertFront(int value){ if (isFull()) { returnfalse; } ++size; if (front - 1 < 0) { a[front = capacity - 1] = value; } else { a[--front] = value; } rear = (front + size - 1) % capacity; returntrue; }
/** Adds an item at the rear of Deque. Return true if the operation is successful. */ boolinsertLast(int value){ if (isFull()) { returnfalse; } ++size; a[rear = (rear + 1) % capacity] = value; returntrue; }
/** Deletes an item from the front of Deque. Return true if the operation is successful. */ booldeleteFront(){ if (isEmpty()) { returnfalse; } --size; front = (front + 1) % capacity; returntrue; }
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */ booldeleteLast(){ if (isEmpty()) { returnfalse; } --size; if (rear - 1 < 0) { rear = capacity - 1; } else { --rear; } returntrue; }
/** Get the front item from the deque. */ intgetFront(){ returnisEmpty() ? -1 : a[front]; }
/** Get the last item from the deque. */ intgetRear(){ returnisEmpty() ? -1 : a[rear]; }
/** Checks whether the circular deque is empty or not. */ boolisEmpty(){ return size == 0; }
/** Checks whether the circular deque is full or not. */ boolisFull(){ return size == capacity; } };
classMyQueue { std::stack<int> stk1, stk2; public: /** Initialize your data structure here. */ MyQueue() {
}
/** Push element x to the back of queue. */ voidpush(int x){ while (!stk1.empty()) { stk2.push(stk1.top()); stk1.pop(); } stk2.push(x); while (!stk2.empty()) { stk1.push(stk2.top()); stk2.pop(); } }
/** Removes the element from in front of queue and returns that element. */ intpop(){ int t = stk1.top(); stk1.pop(); return t; }
/** Get the front element. */ intpeek(){ return stk1.top(); }
/** Returns whether the queue is empty. */ boolempty(){ return stk1.empty(); } };