diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0243980 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "files.associations": { + "stack": "cpp", + "type_traits": "cpp", + "deque": "cpp", + "iostream": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/chapter_stack_and_queue/array_deque.cpp b/chapter_stack_and_queue/array_deque.cpp index 00bf776..e92679c 100644 --- a/chapter_stack_and_queue/array_deque.cpp +++ b/chapter_stack_and_queue/array_deque.cpp @@ -6,107 +6,123 @@ #include "../utils/common.hpp" -/* 基于环形数组实现的双向队列 */ -class ArrayDeque { - private: - vector nums; // 用于存储双向队列元素的数组 - int front; // 队首指针,指向队首元素 - int queSize; // 双向队列长度 +/* ڻʵֵ˫ */ +class ArrayDeque +{ +private: + vector 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() { + /* ȡ˫еij */ + 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 toVector() { - // 仅转换有效长度范围内的列表元素 + /* ڴӡ */ + vector toVector() + { + // תЧȷΧڵбԪ vector 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()); - /* 获取双向队列的长度 */ + /* ȡ˫еij */ 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; } diff --git a/chapter_stack_and_queue/array_queue.cpp b/chapter_stack_and_queue/array_queue.cpp index e6def13..fc73046 100644 --- a/chapter_stack_and_queue/array_queue.cpp +++ b/chapter_stack_and_queue/array_queue.cpp @@ -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() { + /* ȡеij */ + 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 toVector() { - // 仅转换有效长度范围内的列表元素 + /* תΪ Vector */ + vector toVector() + { + // תЧȷΧڵбԪ vector 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()); - /* 获取队列的长度 */ + /* ȡеij */ 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; diff --git a/chapter_stack_and_queue/array_stack.cpp b/chapter_stack_and_queue/array_stack.cpp index c59e076..9ef1c84 100644 --- a/chapter_stack_and_queue/array_stack.cpp +++ b/chapter_stack_and_queue/array_stack.cpp @@ -6,79 +6,87 @@ #include "../utils/common.hpp" -/* 基于数组实现的栈 */ -class ArrayStack { - private: +/* ʵֵջ */ +class ArrayStack +{ +private: vector stack; - public: - /* 获取栈的长度 */ - int size() { +public: + /* ȡջij */ + 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 toVector() { + /* Vector */ + vector 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()); - /* 获取栈的长度 */ + /* ȡջij */ int size = stack->size(); - cout << "栈的长度 size = " << size << endl; + cout << "ջij size = " << size << endl; - /* 判断是否为空 */ + /* жǷΪ */ bool empty = stack->isEmpty(); - cout << "栈是否为空 = " << empty << endl; + cout << "ջǷΪ = " << empty << endl; - // 释放内存 + // ͷڴ delete stack; return 0; diff --git a/chapter_stack_and_queue/deque.cpp b/chapter_stack_and_queue/deque.cpp index 16da9ae..bf3f605 100644 --- a/chapter_stack_and_queue/deque.cpp +++ b/chapter_stack_and_queue/deque.cpp @@ -8,39 +8,41 @@ /* Driver Code */ int main() { - /* 初始化双向队列 */ + /* ʼ˫ */ deque 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); - /* 获取双向队列的长度 */ + /* ȡ˫еij */ int size = deque.size(); - cout << "双向队列长度 size = " << size << endl; + cout << "˫г size = " << size << endl; - /* 判断双向队列是否为空 */ + /* ж˫ǷΪ */ bool empty = deque.empty(); - cout << "双向队列是否为空 = " << empty << endl; + cout << "˫ǷΪ = " << empty << endl; return 0; } diff --git a/chapter_stack_and_queue/queue.cpp b/chapter_stack_and_queue/queue.cpp index b94ba40..c8a5bb3 100644 --- a/chapter_stack_and_queue/queue.cpp +++ b/chapter_stack_and_queue/queue.cpp @@ -8,34 +8,34 @@ /* Driver Code */ int main() { - /* 初始化队列 */ + /* ʼ */ queue 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); - /* 获取队列的长度 */ + /* ȡеij */ int size = queue.size(); - cout << "队列长度 size = " << size << endl; + cout << "г size = " << size << endl; - /* 判断队列是否为空 */ + /* ж϶ǷΪ */ bool empty = queue.empty(); - cout << "队列是否为空 = " << empty << endl; + cout << "ǷΪ = " << empty << endl; return 0; } diff --git a/chapter_stack_and_queue/stack.cpp b/chapter_stack_and_queue/stack.cpp index 18743bf..55ec887 100644 --- a/chapter_stack_and_queue/stack.cpp +++ b/chapter_stack_and_queue/stack.cpp @@ -8,34 +8,34 @@ /* Driver Code */ int main() { - /* 初始化栈 */ + /* ʼջ */ stack 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); - /* 获取栈的长度 */ + /* ȡջij */ int size = stack.size(); - cout << "栈的长度 size = " << size << endl; + cout << "ջij size = " << size << endl; - /* 判断是否为空 */ + /* жǷΪ */ bool empty = stack.empty(); - cout << "栈是否为空 = " << empty << endl; + cout << "ջǷΪ = " << empty << endl; return 0; } diff --git a/utils/print_utils.hpp b/utils/print_utils.hpp index 6686644..cfc367c 100644 --- a/utils/print_utils.hpp +++ b/utils/print_utils.hpp @@ -250,6 +250,27 @@ void printDeque(deque deque) // deque cout << "[" + s.str() + "]" << '\n'; } +/* Print a deque from back*/ // Ӻǰӡһ˫˶ +template +void printDequeFromBack(deque 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