cpp-algo-cases/chapter_stack_and_queue/array_deque.cpp

174 lines
3.9 KiB
C++
Raw Permalink Normal View History

2024-02-21 09:31:34 +08:00
/**
* File: array_deque.cpp
* Created Time: 2023-03-02
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
2024-02-23 14:47:33 +08:00
/* <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)
{
2024-02-21 09:31:34 +08:00
nums.resize(capacity);
front = queSize = 0;
}
2024-02-23 14:47:33 +08:00
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> */
int capacity()
{
2024-02-21 09:31:34 +08:00
return nums.size();
}
2024-02-23 14:47:33 +08:00
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
int size()
{
2024-02-21 09:31:34 +08:00
return queSize;
}
2024-02-23 14:47:33 +08:00
/* <20>ж<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool isEmpty()
{
2024-02-21 09:31:34 +08:00
return queSize == 0;
}
2024-02-23 14:47:33 +08:00
/* <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><D6A4><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5> [0, capacity) <20><>Χ<EFBFBD><CEA7>
2024-02-21 09:31:34 +08:00
}
2024-02-23 14:47:33 +08:00
/* <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;
2024-02-21 09:31:34 +08:00
return;
}
2024-02-23 14:47:33 +08:00
// <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>
2024-02-21 09:31:34 +08:00
front = index(front - 1);
2024-02-23 14:47:33 +08:00
// <20><> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
nums[front] = num;
queSize++;
}
2024-02-23 14:47:33 +08:00
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD> */
void pushLast(int num)
{
if (queSize == capacity())
{
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
2024-02-21 09:31:34 +08:00
return;
}
2024-02-23 14:47:33 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>βָ<CEB2>룬ָ<EBA3AC><D6B8><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD> + 1
2024-02-21 09:31:34 +08:00
int rear = index(front + queSize);
2024-02-23 14:47:33 +08:00
// <20><> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β
2024-02-21 09:31:34 +08:00
nums[rear] = num;
queSize++;
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD>׳<EFBFBD><D7B3><EFBFBD> */
int popFirst()
{
2024-02-21 09:31:34 +08:00
int num = peekFirst();
2024-02-23 14:47:33 +08:00
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һλ
front = index(front + 1); // <20>ƶ<EFBFBD> front ָ<><D6B8><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʹ<EFBFBD>ö<EFBFBD><C3B6><EFBFBD>Ԫ<EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>
2024-02-21 09:31:34 +08:00
queSize--;
return num;
}
2024-02-23 14:47:33 +08:00
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD> */
int popLast()
{
2024-02-21 09:31:34 +08:00
int num = peekLast();
queSize--;
return num;
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
int peekFirst()
{
2024-02-21 09:31:34 +08:00
if (isEmpty())
2024-02-23 14:47:33 +08:00
throw out_of_range("˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
2024-02-21 09:31:34 +08:00
return nums[front];
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD>ʶ<EFBFBD>βԪ<CEB2><D4AA> */
int peekLast()
{
2024-02-21 09:31:34 +08:00
if (isEmpty())
2024-02-23 14:47:33 +08:00
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); // ͨ<><CDA8> front <20><> queSize <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>βԪ<CEB2><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
return nums[last];
}
2024-02-23 14:47:33 +08:00
/* <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>
2024-02-21 09:31:34 +08:00
vector<int> res(queSize);
2024-02-23 14:47:33 +08:00
for (int i = 0, j = front; i < queSize; i++, j++)
{
2024-02-21 09:31:34 +08:00
res[i] = nums[index(j)];
}
return res;
}
};
/* Driver Code */
2024-02-23 14:47:33 +08:00
int main()
{
/* <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-02-21 09:31:34 +08:00
ArrayDeque *deque = new ArrayDeque(10);
deque->pushLast(3);
deque->pushLast(2);
deque->pushLast(5);
2024-02-23 14:47:33 +08:00
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> deque = ";
2024-02-21 09:31:34 +08:00
printVector(deque->toVector());
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
int peekFirst = deque->peekFirst();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> peekFirst = " << peekFirst << endl;
2024-02-21 09:31:34 +08:00
int peekLast = deque->peekLast();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD>βԪ<EFBFBD><EFBFBD> peekLast = " << peekLast << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-02-21 09:31:34 +08:00
deque->pushLast(4);
2024-02-23 14:47:33 +08:00
cout << "Ԫ<EFBFBD><EFBFBD> 4 <20><>β<EFBFBD><CEB2><EFBFBD>Ӻ<EFBFBD> deque = ";
2024-02-21 09:31:34 +08:00
printVector(deque->toVector());
deque->pushFirst(1);
2024-02-23 14:47:33 +08:00
cout << "Ԫ<EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
2024-02-21 09:31:34 +08:00
printVector(deque->toVector());
2024-02-23 14:47:33 +08:00
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
2024-02-21 09:31:34 +08:00
int popLast = deque->popLast();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> = " << popLast << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
2024-02-21 09:31:34 +08:00
printVector(deque->toVector());
int popFirst = deque->popFirst();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> = " << popFirst << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
2024-02-21 09:31:34 +08:00
printVector(deque->toVector());
2024-02-23 14:47:33 +08:00
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
2024-02-21 09:31:34 +08:00
int size = deque->size();
2024-02-23 14:47:33 +08:00
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* <20>ж<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
2024-02-21 09:31:34 +08:00
bool isEmpty = deque->isEmpty();
2024-02-23 14:47:33 +08:00
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << boolalpha << isEmpty << endl;
2024-02-21 09:31:34 +08:00
return 0;
}