looked and push

This commit is contained in:
flykhan 2024-02-23 14:47:33 +08:00
parent b44d2932fc
commit 898b446f27
8 changed files with 263 additions and 192 deletions

9
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,9 @@
{
"files.associations": {
"stack": "cpp",
"type_traits": "cpp",
"deque": "cpp",
"iostream": "cpp",
"vector": "cpp"
}
}

View File

@ -6,107 +6,123 @@
#include "../utils/common.hpp"
/* 基于环形数组实现的双向队列 */
class ArrayDeque {
private:
vector<int> nums; // 用于存储双向队列元素的数组
int front; // 队首指针,指向队首元素
int queSize; // 双向队列长度
/* 基于环形数组实现的双向队列 */
class ArrayDeque
{
private:
vector<int> nums; // 用于存储双向队列元素的数组
int front; // 队首指针,指向队首元素
int queSize; // 双向队列长度
public:
/* 构造方法 */
ArrayDeque(int capacity) {
public:
/* 构造方法 */
ArrayDeque(int capacity)
{
nums.resize(capacity);
front = queSize = 0;
}
/* 获取双向队列的容量 */
int capacity() {
/* 获取双向队列的容量 */
int capacity()
{
return nums.size();
}
/* 获取双向队列的长度 */
int size() {
/* 获取双向队列的长度 */
int size()
{
return queSize;
}
/* 判断双向队列是否为空 */
bool isEmpty() {
/* 判断双向队列是否为空 */
bool isEmpty()
{
return queSize == 0;
}
/* 计算环形数组索引 */
int index(int i) {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return (i + capacity()) % capacity();
/* 计算环形数组索引 */
int index(int i)
{
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return (i + capacity()) % capacity(); // 保证返回值在 [0, capacity) 范围内
}
/* 队首入队 */
void pushFirst(int num) {
if (queSize == capacity()) {
cout << "双向队列已满" << endl;
/* 队首入队 */
void pushFirst(int num)
{
if (queSize == capacity())
{
cout << "双向队列已满" << endl;
return;
}
// 队首指针向左移动一位
// 通过取余操作实现 front 越过数组头部后回到尾部
// 队首指针向左移动一位
// 通过取余操作实现 front 越过数组头部后回到尾部
front = index(front - 1);
// 将 num 添加至队首
// 将 num 添加至队首
nums[front] = num;
queSize++;
}
/* 队尾入队 */
void pushLast(int num) {
if (queSize == capacity()) {
cout << "双向队列已满" << endl;
/* 队尾入队 */
void pushLast(int num)
{
if (queSize == capacity())
{
cout << "双向队列已满" << endl;
return;
}
// 计算队尾指针,指向队尾索引 + 1
// 计算队尾指针,指向队尾索引 + 1
int rear = index(front + queSize);
// 将 num 添加至队尾
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 队首出队 */
int popFirst() {
/* 队首出队 */
int popFirst()
{
int num = peekFirst();
// 队首指针向后移动一位
front = index(front + 1);
// 队首指针向后移动一位
front = index(front + 1); // 移动 front 指针的目的是为了使得队首元素不再属于队列
queSize--;
return num;
}
/* 队尾出队 */
int popLast() {
/* 队尾出队 */
int popLast()
{
int num = peekLast();
queSize--;
return num;
}
/* 访问队首元素 */
int peekFirst() {
/* 访问队首元素 */
int peekFirst()
{
if (isEmpty())
throw out_of_range("双向队列为空");
throw out_of_range("双向队列为空");
return nums[front];
}
/* 访问队尾元素 */
int peekLast() {
/* 访问队尾元素 */
int peekLast()
{
if (isEmpty())
throw out_of_range("双向队列为空");
// 计算尾元素索引
int last = index(front + queSize - 1);
throw out_of_range("双向队列为空");
// 计算尾元素索引
int last = index(front + queSize - 1); // 通过 front 和 queSize 计算出队尾元素索引
return nums[last];
}
/* 返回数组用于打印 */
vector<int> toVector() {
// 仅转换有效长度范围内的列表元素
/* 返回数组用于打印 */
vector<int> toVector()
{
// 仅转换有效长度范围内的列表元素
vector<int> res(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) {
for (int i = 0, j = front; i < queSize; i++, j++)
{
res[i] = nums[index(j)];
}
return res;
@ -114,43 +130,44 @@ class ArrayDeque {
};
/* Driver Code */
int main() {
/* 初始化双向队列 */
int main()
{
/* 初始化双向队列 */
ArrayDeque *deque = new ArrayDeque(10);
deque->pushLast(3);
deque->pushLast(2);
deque->pushLast(5);
cout << "双向队列 deque = ";
cout << "双向队列 deque = ";
printVector(deque->toVector());
/* 访问元素 */
/* 访问元素 */
int peekFirst = deque->peekFirst();
cout << "队首元素 peekFirst = " << peekFirst << endl;
cout << "队首元素 peekFirst = " << peekFirst << endl;
int peekLast = deque->peekLast();
cout << "队尾元素 peekLast = " << peekLast << endl;
cout << "队尾元素 peekLast = " << peekLast << endl;
/* 元素入队 */
/* 元素入队 */
deque->pushLast(4);
cout << "元素 4 队尾入队后 deque = ";
cout << "元素 4 队尾入队后 deque = ";
printVector(deque->toVector());
deque->pushFirst(1);
cout << "元素 1 队首入队后 deque = ";
cout << "元素 1 队首入队后 deque = ";
printVector(deque->toVector());
/* 元素出队 */
/* 元素出队 */
int popLast = deque->popLast();
cout << "队尾出队元素 = " << popLast << ",队尾出队后 deque = ";
cout << "队尾出队元素 = " << popLast << ",队尾出队后 deque = ";
printVector(deque->toVector());
int popFirst = deque->popFirst();
cout << "队首出队元素 = " << popFirst << ",队首出队后 deque = ";
cout << "队首出队元素 = " << popFirst << ",队首出队后 deque = ";
printVector(deque->toVector());
/* 获取双向队列的长度 */
/* 获取双向队列的长度 */
int size = deque->size();
cout << "双向队列长度 size = " << size << endl;
cout << "双向队列长度 size = " << size << endl;
/* 判断双向队列是否为空 */
/* 判断双向队列是否为空 */
bool isEmpty = deque->isEmpty();
cout << "双向队列是否为空 = " << boolalpha << isEmpty << endl;
cout << "双向队列是否为空 = " << boolalpha << isEmpty << endl;
return 0;
}

View File

@ -6,76 +6,88 @@
#include "../utils/common.hpp"
/* 基于环形数组实现的队列 */
class ArrayQueue {
private:
int *nums; // 用于存储队列元素的数组
int front; // 队首指针,指向队首元素
int queSize; // 队列长度
int queCapacity; // 队列容量
/* 基于环形数组实现的队列 */
class ArrayQueue
{
private:
int *nums; // 用于存储队列元素的数组
int front; // 队首指针,指向队首元素
int queSize; // 队列长度
int queCapacity; // 队列容量
public:
ArrayQueue(int capacity) {
// 初始化数组
public:
ArrayQueue(int capacity)
{
// 初始化数组
nums = new int[capacity];
queCapacity = capacity;
front = queSize = 0;
front = queSize = 0; // 初始化队首指针和队列长度
}
~ArrayQueue() {
delete[] nums;
~ArrayQueue()
{
delete[] nums; // 释放内存
}
/* 获取队列的容量 */
int capacity() {
/* 获取队列的容量 */
int capacity()
{
return queCapacity;
}
/* 获取队列的长度 */
int size() {
/* 获取队列的长度 */
int size()
{
return queSize;
}
/* 判断队列是否为空 */
bool isEmpty() {
/* 判断队列是否为空 */
bool isEmpty()
{
return size() == 0;
}
/* 入队 */
void push(int num) {
if (queSize == queCapacity) {
cout << "队列已满" << endl;
/* 入队 */
void push(int num)
{
if (queSize == queCapacity)
{
cout << "队列已满" << endl; // 队列已满,无法入队
return;
}
// 计算队尾指针,指向队尾索引 + 1
// 通过取余操作实现 rear 越过数组尾部后回到头部
// 计算队尾指针,指向队尾索引 + 1
// 通过取余操作实现 rear 越过数组尾部后回到头部
int rear = (front + queSize) % queCapacity;
// 将 num 添加至队尾
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 出队 */
int pop() {
/* 出队 */
int pop()
{
int num = peek();
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
front = (front + 1) % queCapacity;
queSize--;
return num;
}
/* 访问队首元素 */
int peek() {
/* 访问队首元素 */
int peek() // peek 意为窥视,这里指查看队首元素
{
if (isEmpty())
throw out_of_range("队列为空");
throw out_of_range("队列为空");
return nums[front];
}
/* 将数组转化为 Vector 并返回 */
vector<int> toVector() {
// 仅转换有效长度范围内的列表元素
/* 将数组转化为 Vector 并返回 */
vector<int> toVector()
{
// 仅转换有效长度范围内的列表元素
vector<int> arr(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) {
for (int i = 0, j = front; i < queSize; i++, j++)
{
arr[i] = nums[j % queCapacity];
}
return arr;
@ -83,46 +95,48 @@ class ArrayQueue {
};
/* Driver Code */
int main() {
/* 初始化队列 */
int capacity = 10;
int main()
{
/* 初始化队列 */
int capacity = 10; // 队列容量
ArrayQueue *queue = new ArrayQueue(capacity);
/* 元素入队 */
/* 元素入队 */
queue->push(1);
queue->push(3);
queue->push(2);
queue->push(5);
queue->push(4);
cout << "队列 queue = ";
cout << "队列 queue = ";
printVector(queue->toVector());
/* 访问队首元素 */
/* 访问队首元素 */
int peek = queue->peek();
cout << "队首元素 peek = " << peek << endl;
cout << "队首元素 peek = " << peek << endl;
/* 元素出队 */
/* 元素出队 */
peek = queue->pop();
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
printVector(queue->toVector());
/* 获取队列的长度 */
/* 获取队列的长度 */
int size = queue->size();
cout << "队列长度 size = " << size << endl;
cout << "队列长度 size = " << size << endl;
/* 判断队列是否为空 */
/* 判断队列是否为空 */
bool empty = queue->isEmpty();
cout << "队列是否为空 = " << empty << endl;
cout << "队列是否为空 = " << empty << endl;
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
/* 测试环形数组 */
for (int i = 0; i < 10; i++)
{
queue->push(i);
queue->pop();
cout << "" << i << " 轮入队 + 出队后 queue = ";
cout << "" << i << " 轮入队 + 出队后 queue = ";
printVector(queue->toVector());
}
// 释放内存
// 释放内存
delete queue;
return 0;

View File

@ -6,79 +6,87 @@
#include "../utils/common.hpp"
/* 基于数组实现的栈 */
class ArrayStack {
private:
/* 基于数组实现的栈 */
class ArrayStack
{
private:
vector<int> stack;
public:
/* 获取栈的长度 */
int size() {
public:
/* 获取栈的长度 */
int size()
{
return stack.size();
}
/* 判断栈是否为空 */
bool isEmpty() {
/* 判断栈是否为空 */
bool isEmpty()
{
return stack.size() == 0;
}
/* 入栈 */
void push(int num) {
/* 入栈 */
void push(int num)
{
stack.push_back(num);
}
/* 出栈 */
int pop() {
int num = top();
stack.pop_back();
return num;
/* 出栈 */
int pop()
{
int num = top(); // 先获取栈顶元素并保存
stack.pop_back(); // 再删除栈顶元素
return num; // 返回栈顶元素 (用于反馈给用户出栈元素是什么)
}
/* 访问栈顶元素 */
int top() {
/* 访问栈顶元素 */
int top()
{
if (isEmpty())
throw out_of_range("栈为空");
return stack.back();
throw out_of_range("栈为空"); // 如果栈为空,抛出异常
return stack.back(); // 返回栈顶元素
}
/* 返回 Vector */
vector<int> toVector() {
/* 返回 Vector */
vector<int> toVector()
{
return stack;
}
};
/* Driver Code */
int main() {
/* 初始化栈 */
int main()
{
/* 初始化栈 */
ArrayStack *stack = new ArrayStack();
/* 元素入栈 */
stack->push(1);
/* 元素入栈 */
stack->push(1);
stack->push(3);
stack->push(2);
stack->push(5);
stack->push(4);
cout << "栈 stack = ";
cout << "栈 stack = ";
printVector(stack->toVector());
/* 访问栈顶元素 */
/* 访问栈顶元素 */
int top = stack->top();
cout << "栈顶元素 top = " << top << endl;
cout << "栈顶元素 top = " << top << endl;
/* 元素出栈 */
/* 元素出栈 */
top = stack->pop();
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
printVector(stack->toVector());
/* 获取栈的长度 */
/* 获取栈的长度 */
int size = stack->size();
cout << "栈的长度 size = " << size << endl;
cout << "栈的长度 size = " << size << endl;
/* 判断是否为空 */
/* 判断是否为空 */
bool empty = stack->isEmpty();
cout << "栈是否为空 = " << empty << endl;
cout << "栈是否为空 = " << empty << endl;
// 释放内存
// 释放内存
delete stack;
return 0;

View File

@ -8,39 +8,41 @@
/* Driver Code */
int main() {
/* 初始化双向队列 */
/* 初始化双向队列 */
deque<int> deque;
/* 元素入队 */
/* 元素入队 */
deque.push_back(2);
deque.push_back(5);
deque.push_back(4);
deque.push_front(3);
deque.push_front(1);
cout << "双向队列 deque = ";
cout << "双向队列 deque = ";
printDeque(deque);
cout << "从后往前输出双向队列 deque = ";
printDequeFromBack(deque);
/* 访问元素 */
/* 访问元素 */
int front = deque.front();
cout << "队首元素 front = " << front << endl;
cout << "队首元素 front = " << front << endl;
int back = deque.back();
cout << "队尾元素 back = " << back << endl;
cout << "队尾元素 back = " << back << endl;
/* 元素出队 */
/* 元素出队 */
deque.pop_front();
cout << "队首出队元素 popFront = " << front << ",队首出队后 deque = ";
cout << "队首出队元素 popFront = " << front << ",队首出队后 deque = ";
printDeque(deque);
deque.pop_back();
cout << "队尾出队元素 popLast = " << back << ",队尾出队后 deque = ";
cout << "队尾出队元素 popLast = " << back << ",队尾出队后 deque = ";
printDeque(deque);
/* 获取双向队列的长度 */
/* 获取双向队列的长度 */
int size = deque.size();
cout << "双向队列长度 size = " << size << endl;
cout << "双向队列长度 size = " << size << endl;
/* 判断双向队列是否为空 */
/* 判断双向队列是否为空 */
bool empty = deque.empty();
cout << "双向队列是否为空 = " << empty << endl;
cout << "双向队列是否为空 = " << empty << endl;
return 0;
}

View File

@ -8,34 +8,34 @@
/* Driver Code */
int main() {
/* 初始化队列 */
/* 初始化队列 */
queue<int> queue;
/* 元素入队 */
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
cout << "队列 queue = ";
cout << "队列 queue = ";
printQueue(queue);
/* 访问队首元素 */
/* 访问队首元素 */
int front = queue.front();
cout << "队首元素 front = " << front << endl;
cout << "队首元素 front = " << front << endl;
/* 元素出队 */
/* 元素出队 */
queue.pop();
cout << "出队元素 front = " << front << ",出队后 queue = ";
cout << "出队元素 front = " << front << ",出队后 queue = ";
printQueue(queue);
/* 获取队列的长度 */
/* 获取队列的长度 */
int size = queue.size();
cout << "队列长度 size = " << size << endl;
cout << "队列长度 size = " << size << endl;
/* 判断队列是否为空 */
/* 判断队列是否为空 */
bool empty = queue.empty();
cout << "队列是否为空 = " << empty << endl;
cout << "队列是否为空 = " << empty << endl;
return 0;
}

View File

@ -8,34 +8,34 @@
/* Driver Code */
int main() {
/* 初始化栈 */
/* 初始化栈 */
stack<int> stack;
/* 元素入栈 */
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
cout << "栈 stack = ";
cout << "栈 stack = ";
printStack(stack);
/* 访问栈顶元素 */
/* 访问栈顶元素 */
int top = stack.top();
cout << "栈顶元素 top = " << top << endl;
cout << "栈顶元素 top = " << top << endl;
/* 元素出栈 */
stack.pop(); // 无返回值
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
/* 元素出栈 */
stack.pop(); // 无返回值
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
printStack(stack);
/* 获取栈的长度 */
/* 获取栈的长度 */
int size = stack.size();
cout << "栈的长度 size = " << size << endl;
cout << "栈的长度 size = " << size << endl;
/* 判断是否为空 */
/* 判断是否为空 */
bool empty = stack.empty();
cout << "栈是否为空 = " << empty << endl;
cout << "栈是否为空 = " << empty << endl;
return 0;
}

View File

@ -250,6 +250,27 @@ void printDeque(deque<T> deque) // deque
cout << "[" + s.str() + "]" << '\n';
}
/* Print a deque from back*/ // 从后往前打印一个双端队列
template <typename T>
void printDequeFromBack(deque<T> deque) // deque 是双端队列(念做 deck)
{
// Generate the string to print // 生成要打印的字符串
ostringstream s;
bool flag = true;
while (!deque.empty()) // 当双端队列不为空时
{
if (flag)
{
s << deque.back();
flag = false;
}
else
s << ", " << deque.back();
deque.pop_back(); // 弹出队首元素
}
cout << "[" + s.str() + "]" << '\n';
}
/* Print a HashMap */ // 打印一个哈希表
// 定义模板参数 TKey 和 TValue ,用于指定键值对的类型
template <typename TKey, typename TValue>