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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -250,6 +250,27 @@ void printDeque(deque<T> deque) // deque
cout << "[" + s.str() + "]" << '\n'; 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 */ // 打印一个哈希表 /* Print a HashMap */ // 打印一个哈希表
// 定义模板参数 TKey 和 TValue ,用于指定键值对的类型 // 定义模板参数 TKey 和 TValue ,用于指定键值对的类型
template <typename TKey, typename TValue> template <typename TKey, typename TValue>