157 lines
3.7 KiB
C++
157 lines
3.7 KiB
C++
|
/**
|
|||
|
* File: array_deque.cpp
|
|||
|
* Created Time: 2023-03-02
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
class ArrayDeque {
|
|||
|
private:
|
|||
|
vector<int> nums; // <20><><EFBFBD>ڴ洢˫<E6B4A2><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int front; // <20><><EFBFBD><EFBFBD>ָ<EFBFBD>룬ָ<EBA3AC><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|||
|
int queSize; // ˫<><CBAB><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD>
|
|||
|
|
|||
|
public:
|
|||
|
/* <20><><EFBFBD>췽<EFBFBD><ECB7BD> */
|
|||
|
ArrayDeque(int capacity) {
|
|||
|
nums.resize(capacity);
|
|||
|
front = queSize = 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int capacity() {
|
|||
|
return nums.size();
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
|
|||
|
int size() {
|
|||
|
return queSize;
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ж<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty() {
|
|||
|
return queSize == 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>㻷<EFBFBD><E3BBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int index(int i) {
|
|||
|
// ͨ<><CDA8>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD>
|
|||
|
// <20><> i Խ<><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>ص<F3A3ACBB>ͷ<EFBFBD><CDB7>
|
|||
|
// <20><> i Խ<><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ص<F3A3ACBB>β<EFBFBD><CEB2>
|
|||
|
return (i + capacity()) % capacity();
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void pushFirst(int num) {
|
|||
|
if (queSize == capacity()) {
|
|||
|
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
|||
|
return;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һλ
|
|||
|
// ͨ<><CDA8>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5> front Խ<><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>β<EFBFBD><CEB2>
|
|||
|
front = index(front - 1);
|
|||
|
// <20><> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
nums[front] = num;
|
|||
|
queSize++;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD> */
|
|||
|
void pushLast(int num) {
|
|||
|
if (queSize == capacity()) {
|
|||
|
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
|||
|
return;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>βָ<CEB2>룬ָ<EBA3AC><D6B8><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD> + 1
|
|||
|
int rear = index(front + queSize);
|
|||
|
// <20><> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β
|
|||
|
nums[rear] = num;
|
|||
|
queSize++;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>׳<EFBFBD><D7B3><EFBFBD> */
|
|||
|
int popFirst() {
|
|||
|
int num = peekFirst();
|
|||
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һλ
|
|||
|
front = index(front + 1);
|
|||
|
queSize--;
|
|||
|
return num;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD> */
|
|||
|
int popLast() {
|
|||
|
int num = peekLast();
|
|||
|
queSize--;
|
|||
|
return num;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
|
|||
|
int peekFirst() {
|
|||
|
if (isEmpty())
|
|||
|
throw out_of_range("˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
|
|||
|
return nums[front];
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ʶ<EFBFBD>βԪ<CEB2><D4AA> */
|
|||
|
int peekLast() {
|
|||
|
if (isEmpty())
|
|||
|
throw out_of_range("˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
|
|||
|
// <20><><EFBFBD><EFBFBD>βԪ<CEB2><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int last = index(front + queSize - 1);
|
|||
|
return nums[last];
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ӡ */
|
|||
|
vector<int> toVector() {
|
|||
|
// <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD>ȷ<EFBFBD>Χ<EFBFBD>ڵ<EFBFBD><DAB5>б<EFBFBD>Ԫ<EFBFBD><D4AA>
|
|||
|
vector<int> res(queSize);
|
|||
|
for (int i = 0, j = front; i < queSize; i++, j++) {
|
|||
|
res[i] = nums[index(j)];
|
|||
|
}
|
|||
|
return res;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
/* <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
ArrayDeque *deque = new ArrayDeque(10);
|
|||
|
deque->pushLast(3);
|
|||
|
deque->pushLast(2);
|
|||
|
deque->pushLast(5);
|
|||
|
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> deque = ";
|
|||
|
printVector(deque->toVector());
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
|
|||
|
int peekFirst = deque->peekFirst();
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> peekFirst = " << peekFirst << endl;
|
|||
|
int peekLast = deque->peekLast();
|
|||
|
cout << "<EFBFBD><EFBFBD>βԪ<EFBFBD><EFBFBD> peekLast = " << peekLast << endl;
|
|||
|
|
|||
|
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
deque->pushLast(4);
|
|||
|
cout << "Ԫ<EFBFBD><EFBFBD> 4 <20><>β<EFBFBD><CEB2><EFBFBD>Ӻ<EFBFBD> deque = ";
|
|||
|
printVector(deque->toVector());
|
|||
|
deque->pushFirst(1);
|
|||
|
cout << "Ԫ<EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
|
|||
|
printVector(deque->toVector());
|
|||
|
|
|||
|
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
|
|||
|
int popLast = deque->popLast();
|
|||
|
cout << "<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> = " << popLast << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
|
|||
|
printVector(deque->toVector());
|
|||
|
int popFirst = deque->popFirst();
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> = " << popFirst << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
|
|||
|
printVector(deque->toVector());
|
|||
|
|
|||
|
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
|
|||
|
int size = deque->size();
|
|||
|
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
|
|||
|
|
|||
|
/* <20>ж<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty = deque->isEmpty();
|
|||
|
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << boolalpha << isEmpty << endl;
|
|||
|
return 0;
|
|||
|
}
|