diff --git a/codes/cpp/chapter_stack_and_queue/array_queue.cpp b/codes/cpp/chapter_stack_and_queue/array_queue.cpp index 0aea80b..e98caa2 100644 --- a/codes/cpp/chapter_stack_and_queue/array_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_queue.cpp @@ -9,26 +9,27 @@ /* 基于环形数组实现的队列 */ class ArrayQueue { private: - vector nums; // 用于存储队列元素的数组 - int front = 0; // 头指针,指向队首 - int rear = 0; // 尾指针,指向队尾 + 1 + int *nums; // 用于存储队列元素的数组 + int cap; // 队列容量 + int front = 0; // 头指针,指向队首 + int rear = 0; // 尾指针,指向队尾 + 1 public: ArrayQueue(int capacity) { // 初始化数组 - nums.resize(capacity); + cap = capacity; + nums = new int[capacity]; } /* 获取队列的容量 */ int capacity() { - return nums.size(); + return cap; } /* 获取队列的长度 */ int size() { - int cap = capacity(); // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 - return (cap + rear - front) % cap; + return (capacity() + rear - front) % capacity(); } /* 判断队列是否为空 */ @@ -67,6 +68,14 @@ public: return nums[front]; } + /* 访问指定索引元素 */ + int get(int index) { + if (index >= size()) + throw out_of_range("索引越界"); + return nums[(front + index) % capacity()]; + } + + /* 将数组转化为 Vector 并返回 */ vector toVector() { int siz = size(); int cap = capacity(); @@ -99,6 +108,10 @@ int main() { int peek = queue->peek(); cout << "队首元素 peek = " << peek << endl; + /* 访问指定索引元素 */ + int num = queue->get(2); + cout << "队列第 3 个元素为 num = " << num << endl; + /* 元素出队 */ int poll = queue->poll(); cout << "出队元素 poll = " << poll << ",出队后 queue = "; diff --git a/codes/cpp/chapter_stack_and_queue/array_stack.cpp b/codes/cpp/chapter_stack_and_queue/array_stack.cpp index ccc1cb9..96a7510 100644 --- a/codes/cpp/chapter_stack_and_queue/array_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_stack.cpp @@ -69,6 +69,10 @@ int main() { int top = stack->top(); cout << "栈顶元素 top = " << top << endl; + /* 访问索引 index 处元素 */ + int num = stack->get(3); + cout << "栈索引 3 处的元素为 num = " << num << endl; + /* 元素出栈 */ int pop = stack->pop(); cout << "出栈元素 pop = " << pop << ",出栈后 stack = "; diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp index d3cb35b..1762532 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp @@ -9,60 +9,68 @@ /* 基于链表实现的队列 */ class LinkedListQueue { private: - ListNode *front, *back; - int qSize; + ListNode *front, *rear; // 头结点 front ,尾结点 rear + int queSize; public: LinkedListQueue() { front = nullptr; - back = nullptr; - qSize = 0; + rear = nullptr; + queSize = 0; } /* 获取队列的长度 */ int size() { - return qSize; + return queSize; } /* 判断队列是否为空 */ bool empty() { - return qSize == 0; + return queSize == 0; } /* 入队 */ void offer(int num) { // 尾结点后添加 num ListNode* node = new ListNode(num); - node->next = back; - back = node; - qSize++; + // 如果队列为空,则令头、尾结点都指向该结点 + if (front == nullptr) { + front = node; + rear = node; + } + // 如果队列不为空,则将该结点添加到尾结点后 + else { + rear->next = node; + rear = node; + } + queSize++; } /* 出队 */ int poll() { - if (qSize == 0) - throw out_of_range("队列为空"); + int num = peek(); // 删除头结点 - ListNode* node = front; front = front->next; - return node->val; + queSize--; + return num; } /* 访问队首元素 */ int peek() { + if (size() == 0) + throw out_of_range("队列为空"); return front->val; } - /* 将 List 转化为 Array 并返回 */ + /* 将链表转化为 Vector 并返回 */ vector toVector() { ListNode* node = front; - vector vec; - while (node != nullptr) { - vec.push_back(node->val); + vector res(size()); + for (int i = 0; i < res.size(); i++) { + res[i] = node->val; node = node->next; } - reverse(vec.begin(), vec.end()); - return vec; + return res; } }; diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp index 1ffae5e..1bb7c2a 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp @@ -10,17 +10,17 @@ class LinkedListStack { private: ListNode* stackTop; // 将头结点作为栈顶 - int stackSize; // 栈的长度 + int stkSize; // 栈的长度 public: LinkedListStack() { stackTop = nullptr; - stackSize = 0; + stkSize = 0; } /* 获取栈的长度 */ int size() { - return stackSize; + return stkSize; } /* 判断栈是否为空 */ @@ -33,16 +33,14 @@ public: ListNode* node = new ListNode(num); node->next = stackTop; stackTop = node; - stackSize++; + stkSize++; } /* 出栈 */ int pop() { - if (size() == 0) - throw out_of_range("栈为空"); - int num = stackTop->val; + int num = top(); stackTop = stackTop->next; - stackSize--; + stkSize--; return num; } diff --git a/codes/java/chapter_stack_and_queue/array_queue.java b/codes/java/chapter_stack_and_queue/array_queue.java index aab3154..ff22559 100644 --- a/codes/java/chapter_stack_and_queue/array_queue.java +++ b/codes/java/chapter_stack_and_queue/array_queue.java @@ -67,15 +67,23 @@ class ArrayQueue { return nums[front]; } + /* 访问索引 index 处元素 */ + int get(int index) { + if (index >= size()) + throw new IndexOutOfBoundsException(); + return nums[(front + index) % capacity()]; + } + + /* 返回数组 */ public int[] toArray() { int size = size(); int capacity = capacity(); // 仅转换有效长度范围内的列表元素 - int[] arr = new int[size]; + int[] res = new int[size]; for (int i = 0, j = front; i < size; i++, j++) { - arr[i] = nums[j % capacity]; + res[i] = nums[j % capacity]; } - return arr; + return res; } } diff --git a/codes/java/chapter_stack_and_queue/array_stack.java b/codes/java/chapter_stack_and_queue/array_stack.java index 0ac9b45..26a3e96 100644 --- a/codes/java/chapter_stack_and_queue/array_stack.java +++ b/codes/java/chapter_stack_and_queue/array_stack.java @@ -69,6 +69,10 @@ public class array_stack { int peek = stack.peek(); System.out.println("栈顶元素 peek = " + peek); + /* 访问索引 index 处元素 */ + int num = stack.get(3); + System.out.println("栈索引 3 处的元素为 num = " + num); + /* 元素出栈 */ int pop = stack.pop(); System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray())); diff --git a/codes/java/chapter_stack_and_queue/linkedlist_queue.java b/codes/java/chapter_stack_and_queue/linkedlist_queue.java index 9429cc3..291ff41 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_queue.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_queue.java @@ -7,46 +7,69 @@ package chapter_stack_and_queue; import java.util.*; +import include.*; /* 基于链表实现的队列 */ class LinkedListQueue { - LinkedList list; + private ListNode front, rear; // 头结点 front ,尾结点 rear + private int queSize = 0; public LinkedListQueue() { - // 初始化链表 - list = new LinkedList<>(); + front = null; + rear = null; } /* 获取队列的长度 */ public int size() { - return list.size(); + return queSize; } /* 判断队列是否为空 */ public boolean isEmpty() { - return list.size() == 0; + return size() == 0; } /* 入队 */ public void offer(int num) { // 尾结点后添加 num - list.addLast(num); + ListNode node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (front == null) { + front = node; + rear = node; + // 如果队列不为空,则将该结点添加到尾结点后 + } else { + rear.next = node; + rear = node; + } + queSize++; } /* 出队 */ public int poll() { + int num = peek(); // 删除头结点 - return list.removeFirst(); + front = front.next; + queSize--; + return num; } /* 访问队首元素 */ public int peek() { - return list.getFirst(); + if (size() == 0) + throw new IndexOutOfBoundsException(); + return front.val; } - /* 将 List 转化为 Array 并返回 */ - public Object[] toArray() { - return list.toArray(); + /* 将链表转化为 Array 并返回 */ + public int[] toArray() { + ListNode node = front; + int[] res = new int[size()]; + for (int i = 0; i < res.length; i++) { + res[i] = node.val; + node = node.next; + } + return res; } } diff --git a/codes/java/chapter_stack_and_queue/linkedlist_stack.java b/codes/java/chapter_stack_and_queue/linkedlist_stack.java index 0dc4798..8b1d67b 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_stack.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_stack.java @@ -12,7 +12,7 @@ import include.*; /* 基于链表实现的栈 */ class LinkedListStack { private ListNode stackPeek; // 将头结点作为栈顶 - private int stackSize = 0; // 栈的长度 + private int stkSize = 0; // 栈的长度 public LinkedListStack() { stackPeek = null; @@ -20,7 +20,7 @@ class LinkedListStack { /* 获取栈的长度 */ public int size() { - return stackSize; + return stkSize; } /* 判断栈是否为空 */ @@ -33,16 +33,14 @@ class LinkedListStack { ListNode node = new ListNode(num); node.next = stackPeek; stackPeek = node; - stackSize++; + stkSize++; } /* 出栈 */ public int pop() { - if (size() == 0) - throw new IndexOutOfBoundsException(); int num = peek(); stackPeek = stackPeek.next; - stackSize--; + stkSize--; return num; } diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index e0bf5f2..b1640a0 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -59,6 +59,10 @@ if __name__ == "__main__": peek = stack.peek() print("栈顶元素 peek =", peek) + """ 访问索引 index 处元素 """ + num = stack.get(3); + print("栈索引 3 处的元素为 num =", num); + """ 元素出栈 """ pop = stack.pop() print("出栈元素 pop =", pop) diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index 49d1366..5ee9035 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -31,12 +31,10 @@ class LinkedListStack: """ 出栈 """ def pop(self): - # 判空处理 - if not self.__peek: return None - pop = self.__peek.val + num = self.peek() self.__peek = self.__peek.next self.__size -= 1 - return pop + return num """ 访问栈顶元素 """ def peek(self): diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index 407d353..0665762 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -40,38 +40,53 @@ comments: true Deque deque = new LinkedList<>(); /* 元素入队 */ - deque.offerLast(2); + deque.offerLast(2); // 添加至队尾 deque.offerLast(5); deque.offerLast(4); - deque.offerFirst(3); + deque.offerFirst(3); // 添加至队首 deque.offerFirst(1); - System.out.println("双向队列 deque = " + deque); /* 访问队首元素 */ - int peekFirst = deque.peekFirst(); - System.out.println("队首元素 peekFirst = " + peekFirst); - int peekLast = deque.peekLast(); - System.out.println("队尾元素 peekLast = " + peekLast); + int peekFirst = deque.peekFirst(); // 队首元素 + int peekLast = deque.peekLast(); // 队尾元素 /* 元素出队 */ - int pollFirst = deque.pollFirst(); - System.out.println("队首出队元素 pollFirst = " + pollFirst + ",队首出队后 deque = " + deque); - int pollLast = deque.pollLast(); - System.out.println("队尾出队元素 pollLast = " + pollLast + ",队尾出队后 deque = " + deque); + int pollFirst = deque.pollFirst(); // 队首元素出队 + int pollLast = deque.pollLast(); // 队尾元素出队 /* 获取双向队列的长度 */ int size = deque.size(); - System.out.println("双向队列长度 size = " + size); /* 判断双向队列是否为空 */ boolean isEmpty = deque.isEmpty(); - System.out.println("双向队列是否为空 = " + isEmpty); ``` === "C++" ```cpp title="deque.cpp" - + /* 初始化双向队列 */ + deque deque; + + /* 元素入队 */ + deque.push_back(2); // 添加至队尾 + deque.push_back(5); + deque.push_back(4); + deque.push_front(3); // 添加至队首 + deque.push_front(1); + + /* 访问队首元素 */ + int front = deque.front(); // 队首元素 + int back = deque.back(); // 队尾元素 + + /* 元素出队 */ + deque.pop_front(); // 队首元素出队 + deque.pop_back(); // 队尾元素出队 + + /* 获取双向队列的长度 */ + int size = deque.size(); + + /* 判断双向队列是否为空 */ + bool empty = deque.empty(); ``` === "Python" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 0637688..f69f37e 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -44,29 +44,44 @@ comments: true queue.offer(2); queue.offer(5); queue.offer(4); - System.out.println("队列 queue = " + queue); /* 访问队首元素 */ int peek = queue.peek(); - System.out.println("队首元素 peek = " + peek); /* 元素出队 */ int poll = queue.poll(); - System.out.println("出队元素 poll = " + poll + ",出队后 queue = " + queue); /* 获取队列的长度 */ int size = queue.size(); - System.out.println("队列长度 size = " + size); /* 判断队列是否为空 */ boolean isEmpty = queue.isEmpty(); - System.out.println("队列是否为空 = " + isEmpty); ``` === "C++" ```cpp title="queue.cpp" + /* 初始化队列 */ + queue queue; + /* 元素入队 */ + queue.push(1); + queue.push(3); + queue.push(2); + queue.push(5); + queue.push(4); + + /* 访问队首元素 */ + int front = queue.front(); + + /* 元素出队 */ + queue.pop(); + + /* 获取队列的长度 */ + int size = queue.size(); + + /* 判断队列是否为空 */ + bool empty = queue.empty(); ``` === "Python" @@ -88,33 +103,49 @@ comments: true ```java title="linkedlist_queue.java" /* 基于链表实现的队列 */ class LinkedListQueue { - LinkedList list; + private ListNode front, rear; // 头结点 front ,尾结点 rear + private int queSize = 0; public LinkedListQueue() { - // 初始化链表 - list = new LinkedList<>(); + front = null; + rear = null; } /* 获取队列的长度 */ public int size() { - return list.size(); + return queSize; } /* 判断队列是否为空 */ public boolean isEmpty() { - return list.size() == 0; + return size() == 0; } /* 入队 */ public void offer(int num) { // 尾结点后添加 num - list.addLast(num); + ListNode node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (front == null) { + front = node; + rear = node; + // 如果队列不为空,则将该结点添加到尾结点后 + } else { + rear.next = node; + rear = node; + } + queSize++; } /* 出队 */ public int poll() { + int num = peek(); // 删除头结点 - return list.removeFirst(); + front = front.next; + queSize--; + return num; } /* 访问队首元素 */ public int peek() { - return list.getFirst(); + if (size() == 0) + throw new IndexOutOfBoundsException(); + return front.val; } } ``` @@ -122,7 +153,57 @@ comments: true === "C++" ```cpp title="linkedlist_queue.cpp" - + /* 基于链表实现的队列 */ + class LinkedListQueue { + private: + ListNode *front, *rear; // 头结点 front ,尾结点 rear + int queSize; + + public: + LinkedListQueue() { + front = nullptr; + rear = nullptr; + queSize = 0; + } + /* 获取队列的长度 */ + int size() { + return queSize; + } + /* 判断队列是否为空 */ + bool empty() { + return queSize == 0; + } + /* 入队 */ + void offer(int num) { + // 尾结点后添加 num + ListNode* node = new ListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (front == nullptr) { + front = node; + rear = node; + } + // 如果队列不为空,则将该结点添加到尾结点后 + else { + rear->next = node; + rear = node; + } + queSize++; + } + /* 出队 */ + int poll() { + int num = peek(); + // 删除头结点 + front = front->next; + queSize--; + return num; + } + /* 访问队首元素 */ + int peek() { + if (size() == 0) + throw out_of_range("队列为空"); + return front->val; + } + }; ``` === "Python" @@ -196,13 +277,80 @@ comments: true throw new EmptyStackException(); return nums[front]; } + /* 访问指定索引元素 */ + int get(int index) { + if (index >= size()) + throw new IndexOutOfBoundsException(); + return nums[(front + index) % capacity()]; + } } ``` === "C++" ```cpp title="array_queue.cpp" - + /* 基于环形数组实现的队列 */ + class ArrayQueue { + private: + int *nums; // 用于存储队列元素的数组 + int cap; // 队列容量 + int front = 0; // 头指针,指向队首 + int rear = 0; // 尾指针,指向队尾 + 1 + + public: + ArrayQueue(int capacity) { + // 初始化数组 + cap = capacity; + nums = new int[capacity]; + } + /* 获取队列的容量 */ + int capacity() { + return cap; + } + /* 获取队列的长度 */ + int size() { + // 由于将数组看作为环形,可能 rear < front ,因此需要取余数 + return (capacity() + rear - front) % capacity(); + } + /* 判断队列是否为空 */ + bool empty() { + return rear - front == 0; + } + /* 入队 */ + void offer(int num) { + if (size() == capacity()) { + cout << "队列已满" << endl; + return; + } + // 尾结点后添加 num + nums[rear] = num; + // 尾指针向后移动一位,越过尾部后返回到数组头部 + rear = (rear + 1) % capacity(); + } + /* 出队 */ + int poll() { + // 删除头结点 + if (empty()) + throw out_of_range("队列为空"); + int num = nums[front]; + // 队头指针向后移动,越过尾部后返回到数组头部 + front = (front + 1) % capacity(); + return num; + } + /* 访问队首元素 */ + int peek() { + // 删除头结点 + if (empty()) + throw out_of_range("队列为空"); + return nums[front]; + } + /* 访问指定位置元素 */ + int get(int index) { + if (index >= size()) + throw out_of_range("索引越界"); + return nums[(front + index) % capacity()] + } + }; ``` === "Python" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index b7b3e93..9991c51 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -45,23 +45,18 @@ comments: true stack.addLast(2); stack.addLast(5); stack.addLast(4); - System.out.println("栈 stack = " + stack); /* 访问栈顶元素 */ int peek = stack.peekLast(); - System.out.println("栈顶元素 peek = " + peek); /* 元素出栈 */ int pop = stack.removeLast(); - System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + stack); /* 获取栈的长度 */ int size = stack.size(); - System.out.println("栈的长度 size = " + size); /* 判断是否为空 */ boolean isEmpty = stack.isEmpty(); - System.out.println("栈是否为空 = " + isEmpty); ``` === "C++" @@ -76,32 +71,25 @@ comments: true stack.push(2); stack.push(5); stack.push(4); - cout << "栈 stack = "; - PrintUtil::printStack(stack); /* 访问栈顶元素 */ int top = stack.top(); - cout << "栈顶元素 top = " << top << endl; /* 元素出栈 */ stack.pop(); - cout << "出栈元素 pop = " << top << ",出栈后 stack = "; - PrintUtil::printStack(stack); /* 获取栈的长度 */ int size = stack.size(); - cout << "栈的长度 size = " << size << endl; /* 判断是否为空 */ bool empty = stack.empty(); - cout << "栈是否为空 = " << empty << endl; ``` === "Python" ```python title="stack.py" """ 初始化栈 """ - # Python 没有内置的栈类,可以把 list 当作栈来使用 + # Python 没有内置的栈类,可以把 List 当作栈来使用 stack = [] """ 元素入栈 """ @@ -110,24 +98,18 @@ comments: true stack.append(2) stack.append(5) stack.append(4) - print("栈 stack =", stack) """ 访问栈顶元素 """ peek = stack[-1] - print("栈顶元素 peek =", peek) """ 元素出栈 """ pop = stack.pop() - print("出栈元素 pop =", pop) - print("出栈后 stack =", stack) """ 获取栈的长度 """ size = len(stack) - print("栈的长度 size =", size) """ 判断是否为空 """ is_empty = len(stack) == 0 - print("栈是否为空 =", is_empty) ``` ## 栈的实现 @@ -148,14 +130,14 @@ comments: true /* 基于链表实现的栈 */ class LinkedListStack { private ListNode stackPeek; // 将头结点作为栈顶 - private int stackSize = 0; // 栈的长度 + private int stkSize = 0; // 栈的长度 public LinkedListStack() { stackPeek = null; } /* 获取栈的长度 */ public int size() { - return stackSize; + return stkSize; } /* 判断栈是否为空 */ public boolean isEmpty() { @@ -166,15 +148,13 @@ comments: true ListNode node = new ListNode(num); node.next = stackPeek; stackPeek = node; - stackSize++; + stkSize++; } /* 出栈 */ public int pop() { - if (size() == 0) - throw new IndexOutOfBoundsException(); int num = peek(); stackPeek = stackPeek.next; - stackSize--; + stkSize--; return num; } /* 访问栈顶元素 */ @@ -193,16 +173,16 @@ comments: true class LinkedListStack { private: ListNode* stackTop; // 将头结点作为栈顶 - int stackSize; // 栈的长度 + int stkSize; // 栈的长度 public: LinkedListStack() { stackTop = nullptr; - stackSize = 0; + stkSize = 0; } /* 获取栈的长度 */ int size() { - return stackSize; + return stkSize; } /* 判断栈是否为空 */ bool empty() { @@ -213,15 +193,13 @@ comments: true ListNode* node = new ListNode(num); node->next = stackTop; stackTop = node; - stackSize++; + stkSize++; } /* 出栈 */ int pop() { - if (size() == 0) - throw out_of_range("栈为空"); - int num = stackTop->val; + int num = top(); stackTop = stackTop->next; - stackSize--; + stkSize--; return num; } /* 访问栈顶元素 */ @@ -259,12 +237,10 @@ comments: true """ 出栈 """ def pop(self): - # 判空处理 - if not self.__peek: return None - pop = self.__peek.val + num = self.peek() self.__peek = self.__peek.next self.__size -= 1 - return pop + return num """ 访问栈顶元素 """ def peek(self):