update stack and queue.

pull/55/head
Yudong Jin 2 years ago
parent 8669e06414
commit acfdc713ba

@ -9,26 +9,27 @@
/* 基于环形数组实现的队列 */ /* 基于环形数组实现的队列 */
class ArrayQueue { class ArrayQueue {
private: private:
vector<int> nums; // 用于存储队列元素的数组 int *nums; // 用于存储队列元素的数组
int cap; // 队列容量
int front = 0; // 头指针,指向队首 int front = 0; // 头指针,指向队首
int rear = 0; // 尾指针,指向队尾 + 1 int rear = 0; // 尾指针,指向队尾 + 1
public: public:
ArrayQueue(int capacity) { ArrayQueue(int capacity) {
// 初始化数组 // 初始化数组
nums.resize(capacity); cap = capacity;
nums = new int[capacity];
} }
/* 获取队列的容量 */ /* 获取队列的容量 */
int capacity() { int capacity() {
return nums.size(); return cap;
} }
/* 获取队列的长度 */ /* 获取队列的长度 */
int size() { int size() {
int cap = capacity();
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数 // 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (cap + rear - front) % cap; return (capacity() + rear - front) % capacity();
} }
/* 判断队列是否为空 */ /* 判断队列是否为空 */
@ -67,6 +68,14 @@ public:
return nums[front]; return nums[front];
} }
/* 访问指定索引元素 */
int get(int index) {
if (index >= size())
throw out_of_range("索引越界");
return nums[(front + index) % capacity()];
}
/* 将数组转化为 Vector 并返回 */
vector<int> toVector() { vector<int> toVector() {
int siz = size(); int siz = size();
int cap = capacity(); int cap = capacity();
@ -99,6 +108,10 @@ int main() {
int peek = queue->peek(); int peek = queue->peek();
cout << "队首元素 peek = " << peek << endl; cout << "队首元素 peek = " << peek << endl;
/* 访问指定索引元素 */
int num = queue->get(2);
cout << "队列第 3 个元素为 num = " << num << endl;
/* 元素出队 */ /* 元素出队 */
int poll = queue->poll(); int poll = queue->poll();
cout << "出队元素 poll = " << poll << ",出队后 queue = "; cout << "出队元素 poll = " << poll << ",出队后 queue = ";

@ -69,6 +69,10 @@ int main() {
int top = stack->top(); int top = stack->top();
cout << "栈顶元素 top = " << top << endl; cout << "栈顶元素 top = " << top << endl;
/* 访问索引 index 处元素 */
int num = stack->get(3);
cout << "栈索引 3 处的元素为 num = " << num << endl;
/* 元素出栈 */ /* 元素出栈 */
int pop = stack->pop(); int pop = stack->pop();
cout << "出栈元素 pop = " << pop << ",出栈后 stack = "; cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";

@ -9,60 +9,68 @@
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */
class LinkedListQueue { class LinkedListQueue {
private: private:
ListNode *front, *back; ListNode *front, *rear; // 头结点 front ,尾结点 rear
int qSize; int queSize;
public: public:
LinkedListQueue() { LinkedListQueue() {
front = nullptr; front = nullptr;
back = nullptr; rear = nullptr;
qSize = 0; queSize = 0;
} }
/* 获取队列的长度 */ /* 获取队列的长度 */
int size() { int size() {
return qSize; return queSize;
} }
/* 判断队列是否为空 */ /* 判断队列是否为空 */
bool empty() { bool empty() {
return qSize == 0; return queSize == 0;
} }
/* 入队 */ /* 入队 */
void offer(int num) { void offer(int num) {
// 尾结点后添加 num // 尾结点后添加 num
ListNode* node = new ListNode(num); ListNode* node = new ListNode(num);
node->next = back; // 如果队列为空,则令头、尾结点都指向该结点
back = node; if (front == nullptr) {
qSize++; front = node;
rear = node;
}
// 如果队列不为空,则将该结点添加到尾结点后
else {
rear->next = node;
rear = node;
}
queSize++;
} }
/* 出队 */ /* 出队 */
int poll() { int poll() {
if (qSize == 0) int num = peek();
throw out_of_range("队列为空");
// 删除头结点 // 删除头结点
ListNode* node = front;
front = front->next; front = front->next;
return node->val; queSize--;
return num;
} }
/* 访问队首元素 */ /* 访问队首元素 */
int peek() { int peek() {
if (size() == 0)
throw out_of_range("队列为空");
return front->val; return front->val;
} }
/* 将 List 转化为 Array 并返回 */ /* 将链表转化为 Vector 并返回 */
vector<int> toVector() { vector<int> toVector() {
ListNode* node = front; ListNode* node = front;
vector<int> vec; vector<int> res(size());
while (node != nullptr) { for (int i = 0; i < res.size(); i++) {
vec.push_back(node->val); res[i] = node->val;
node = node->next; node = node->next;
} }
reverse(vec.begin(), vec.end()); return res;
return vec;
} }
}; };

@ -10,17 +10,17 @@
class LinkedListStack { class LinkedListStack {
private: private:
ListNode* stackTop; // 将头结点作为栈顶 ListNode* stackTop; // 将头结点作为栈顶
int stackSize; // 栈的长度 int stkSize; // 栈的长度
public: public:
LinkedListStack() { LinkedListStack() {
stackTop = nullptr; stackTop = nullptr;
stackSize = 0; stkSize = 0;
} }
/* 获取栈的长度 */ /* 获取栈的长度 */
int size() { int size() {
return stackSize; return stkSize;
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
@ -33,16 +33,14 @@ public:
ListNode* node = new ListNode(num); ListNode* node = new ListNode(num);
node->next = stackTop; node->next = stackTop;
stackTop = node; stackTop = node;
stackSize++; stkSize++;
} }
/* 出栈 */ /* 出栈 */
int pop() { int pop() {
if (size() == 0) int num = top();
throw out_of_range("栈为空");
int num = stackTop->val;
stackTop = stackTop->next; stackTop = stackTop->next;
stackSize--; stkSize--;
return num; return num;
} }

@ -67,15 +67,23 @@ class ArrayQueue {
return nums[front]; return nums[front];
} }
/* 访问索引 index 处元素 */
int get(int index) {
if (index >= size())
throw new IndexOutOfBoundsException();
return nums[(front + index) % capacity()];
}
/* 返回数组 */
public int[] toArray() { public int[] toArray() {
int size = size(); int size = size();
int capacity = capacity(); int capacity = capacity();
// 仅转换有效长度范围内的列表元素 // 仅转换有效长度范围内的列表元素
int[] arr = new int[size]; int[] res = new int[size];
for (int i = 0, j = front; i < size; i++, j++) { for (int i = 0, j = front; i < size; i++, j++) {
arr[i] = nums[j % capacity]; res[i] = nums[j % capacity];
} }
return arr; return res;
} }
} }

@ -69,6 +69,10 @@ public class array_stack {
int peek = stack.peek(); int peek = stack.peek();
System.out.println("栈顶元素 peek = " + peek); System.out.println("栈顶元素 peek = " + peek);
/* 访问索引 index 处元素 */
int num = stack.get(3);
System.out.println("栈索引 3 处的元素为 num = " + num);
/* 元素出栈 */ /* 元素出栈 */
int pop = stack.pop(); int pop = stack.pop();
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray())); System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray()));

@ -7,46 +7,69 @@
package chapter_stack_and_queue; package chapter_stack_and_queue;
import java.util.*; import java.util.*;
import include.*;
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */
class LinkedListQueue { class LinkedListQueue {
LinkedList<Integer> list; private ListNode front, rear; // 头结点 front ,尾结点 rear
private int queSize = 0;
public LinkedListQueue() { public LinkedListQueue() {
// 初始化链表 front = null;
list = new LinkedList<>(); rear = null;
} }
/* 获取队列的长度 */ /* 获取队列的长度 */
public int size() { public int size() {
return list.size(); return queSize;
} }
/* 判断队列是否为空 */ /* 判断队列是否为空 */
public boolean isEmpty() { public boolean isEmpty() {
return list.size() == 0; return size() == 0;
} }
/* 入队 */ /* 入队 */
public void offer(int num) { public void offer(int num) {
// 尾结点后添加 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() { public int poll() {
int num = peek();
// 删除头结点 // 删除头结点
return list.removeFirst(); front = front.next;
queSize--;
return num;
} }
/* 访问队首元素 */ /* 访问队首元素 */
public int peek() { public int peek() {
return list.getFirst(); if (size() == 0)
throw new IndexOutOfBoundsException();
return front.val;
} }
/* 将 List 转化为 Array 并返回 */ /* 将链表转化为 Array 并返回 */
public Object[] toArray() { public int[] toArray() {
return list.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;
} }
} }

@ -12,7 +12,7 @@ import include.*;
/* 基于链表实现的栈 */ /* 基于链表实现的栈 */
class LinkedListStack { class LinkedListStack {
private ListNode stackPeek; // 将头结点作为栈顶 private ListNode stackPeek; // 将头结点作为栈顶
private int stackSize = 0; // 栈的长度 private int stkSize = 0; // 栈的长度
public LinkedListStack() { public LinkedListStack() {
stackPeek = null; stackPeek = null;
@ -20,7 +20,7 @@ class LinkedListStack {
/* 获取栈的长度 */ /* 获取栈的长度 */
public int size() { public int size() {
return stackSize; return stkSize;
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
@ -33,16 +33,14 @@ class LinkedListStack {
ListNode node = new ListNode(num); ListNode node = new ListNode(num);
node.next = stackPeek; node.next = stackPeek;
stackPeek = node; stackPeek = node;
stackSize++; stkSize++;
} }
/* 出栈 */ /* 出栈 */
public int pop() { public int pop() {
if (size() == 0)
throw new IndexOutOfBoundsException();
int num = peek(); int num = peek();
stackPeek = stackPeek.next; stackPeek = stackPeek.next;
stackSize--; stkSize--;
return num; return num;
} }

@ -59,6 +59,10 @@ if __name__ == "__main__":
peek = stack.peek() peek = stack.peek()
print("栈顶元素 peek =", peek) print("栈顶元素 peek =", peek)
""" 访问索引 index 处元素 """
num = stack.get(3);
print("栈索引 3 处的元素为 num =", num);
""" 元素出栈 """ """ 元素出栈 """
pop = stack.pop() pop = stack.pop()
print("出栈元素 pop =", pop) print("出栈元素 pop =", pop)

@ -31,12 +31,10 @@ class LinkedListStack:
""" 出栈 """ """ 出栈 """
def pop(self): def pop(self):
# 判空处理 num = self.peek()
if not self.__peek: return None
pop = self.__peek.val
self.__peek = self.__peek.next self.__peek = self.__peek.next
self.__size -= 1 self.__size -= 1
return pop return num
""" 访问栈顶元素 """ """ 访问栈顶元素 """
def peek(self): def peek(self):

@ -40,38 +40,53 @@ comments: true
Deque<Integer> deque = new LinkedList<>(); Deque<Integer> deque = new LinkedList<>();
/* 元素入队 */ /* 元素入队 */
deque.offerLast(2); deque.offerLast(2); // 添加至队尾
deque.offerLast(5); deque.offerLast(5);
deque.offerLast(4); deque.offerLast(4);
deque.offerFirst(3); deque.offerFirst(3); // 添加至队首
deque.offerFirst(1); deque.offerFirst(1);
System.out.println("双向队列 deque = " + deque);
/* 访问队首元素 */ /* 访问队首元素 */
int peekFirst = deque.peekFirst(); int peekFirst = deque.peekFirst(); // 队首元素
System.out.println("队首元素 peekFirst = " + peekFirst); int peekLast = deque.peekLast(); // 队尾元素
int peekLast = deque.peekLast();
System.out.println("队尾元素 peekLast = " + peekLast);
/* 元素出队 */ /* 元素出队 */
int pollFirst = deque.pollFirst(); int pollFirst = deque.pollFirst(); // 队首元素出队
System.out.println("队首出队元素 pollFirst = " + pollFirst + ",队首出队后 deque = " + deque); int pollLast = deque.pollLast(); // 队尾元素出队
int pollLast = deque.pollLast();
System.out.println("队尾出队元素 pollLast = " + pollLast + ",队尾出队后 deque = " + deque);
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */
int size = deque.size(); int size = deque.size();
System.out.println("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */ /* 判断双向队列是否为空 */
boolean isEmpty = deque.isEmpty(); boolean isEmpty = deque.isEmpty();
System.out.println("双向队列是否为空 = " + isEmpty);
``` ```
=== "C++" === "C++"
```cpp title="deque.cpp" ```cpp title="deque.cpp"
/* 初始化双向队列 */
deque<int> 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" === "Python"

@ -44,29 +44,44 @@ comments: true
queue.offer(2); queue.offer(2);
queue.offer(5); queue.offer(5);
queue.offer(4); queue.offer(4);
System.out.println("队列 queue = " + queue);
/* 访问队首元素 */ /* 访问队首元素 */
int peek = queue.peek(); int peek = queue.peek();
System.out.println("队首元素 peek = " + peek);
/* 元素出队 */ /* 元素出队 */
int poll = queue.poll(); int poll = queue.poll();
System.out.println("出队元素 poll = " + poll + ",出队后 queue = " + queue);
/* 获取队列的长度 */ /* 获取队列的长度 */
int size = queue.size(); int size = queue.size();
System.out.println("队列长度 size = " + size);
/* 判断队列是否为空 */ /* 判断队列是否为空 */
boolean isEmpty = queue.isEmpty(); boolean isEmpty = queue.isEmpty();
System.out.println("队列是否为空 = " + isEmpty);
``` ```
=== "C++" === "C++"
```cpp title="queue.cpp" ```cpp title="queue.cpp"
/* 初始化队列 */
queue<int> 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" === "Python"
@ -88,33 +103,49 @@ comments: true
```java title="linkedlist_queue.java" ```java title="linkedlist_queue.java"
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */
class LinkedListQueue { class LinkedListQueue {
LinkedList<Integer> list; private ListNode front, rear; // 头结点 front ,尾结点 rear
private int queSize = 0;
public LinkedListQueue() { public LinkedListQueue() {
// 初始化链表 front = null;
list = new LinkedList<>(); rear = null;
} }
/* 获取队列的长度 */ /* 获取队列的长度 */
public int size() { public int size() {
return list.size(); return queSize;
} }
/* 判断队列是否为空 */ /* 判断队列是否为空 */
public boolean isEmpty() { public boolean isEmpty() {
return list.size() == 0; return size() == 0;
} }
/* 入队 */ /* 入队 */
public void offer(int num) { public void offer(int num) {
// 尾结点后添加 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() { public int poll() {
int num = peek();
// 删除头结点 // 删除头结点
return list.removeFirst(); front = front.next;
queSize--;
return num;
} }
/* 访问队首元素 */ /* 访问队首元素 */
public int peek() { public int peek() {
return list.getFirst(); if (size() == 0)
throw new IndexOutOfBoundsException();
return front.val;
} }
} }
``` ```
@ -122,7 +153,57 @@ comments: true
=== "C++" === "C++"
```cpp title="linkedlist_queue.cpp" ```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" === "Python"
@ -196,13 +277,80 @@ comments: true
throw new EmptyStackException(); throw new EmptyStackException();
return nums[front]; return nums[front];
} }
/* 访问指定索引元素 */
int get(int index) {
if (index >= size())
throw new IndexOutOfBoundsException();
return nums[(front + index) % capacity()];
}
} }
``` ```
=== "C++" === "C++"
```cpp title="array_queue.cpp" ```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" === "Python"

@ -45,23 +45,18 @@ comments: true
stack.addLast(2); stack.addLast(2);
stack.addLast(5); stack.addLast(5);
stack.addLast(4); stack.addLast(4);
System.out.println("栈 stack = " + stack);
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int peek = stack.peekLast(); int peek = stack.peekLast();
System.out.println("栈顶元素 peek = " + peek);
/* 元素出栈 */ /* 元素出栈 */
int pop = stack.removeLast(); int pop = stack.removeLast();
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + stack);
/* 获取栈的长度 */ /* 获取栈的长度 */
int size = stack.size(); int size = stack.size();
System.out.println("栈的长度 size = " + size);
/* 判断是否为空 */ /* 判断是否为空 */
boolean isEmpty = stack.isEmpty(); boolean isEmpty = stack.isEmpty();
System.out.println("栈是否为空 = " + isEmpty);
``` ```
=== "C++" === "C++"
@ -76,32 +71,25 @@ comments: true
stack.push(2); stack.push(2);
stack.push(5); stack.push(5);
stack.push(4); stack.push(4);
cout << "栈 stack = ";
PrintUtil::printStack(stack);
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int top = stack.top(); int top = stack.top();
cout << "栈顶元素 top = " << top << endl;
/* 元素出栈 */ /* 元素出栈 */
stack.pop(); stack.pop();
cout << "出栈元素 pop = " << top << " stack = ";
PrintUtil::printStack(stack);
/* 获取栈的长度 */ /* 获取栈的长度 */
int size = stack.size(); int size = stack.size();
cout << "栈的长度 size = " << size << endl;
/* 判断是否为空 */ /* 判断是否为空 */
bool empty = stack.empty(); bool empty = stack.empty();
cout << "栈是否为空 = " << empty << endl;
``` ```
=== "Python" === "Python"
```python title="stack.py" ```python title="stack.py"
""" 初始化栈 """ """ 初始化栈 """
# Python 没有内置的栈类,可以把 list 当作栈来使用 # Python 没有内置的栈类,可以把 List 当作栈来使用
stack = [] stack = []
""" 元素入栈 """ """ 元素入栈 """
@ -110,24 +98,18 @@ comments: true
stack.append(2) stack.append(2)
stack.append(5) stack.append(5)
stack.append(4) stack.append(4)
print("栈 stack =", stack)
""" 访问栈顶元素 """ """ 访问栈顶元素 """
peek = stack[-1] peek = stack[-1]
print("栈顶元素 peek =", peek)
""" 元素出栈 """ """ 元素出栈 """
pop = stack.pop() pop = stack.pop()
print("出栈元素 pop =", pop)
print("出栈后 stack =", stack)
""" 获取栈的长度 """ """ 获取栈的长度 """
size = len(stack) size = len(stack)
print("栈的长度 size =", size)
""" 判断是否为空 """ """ 判断是否为空 """
is_empty = len(stack) == 0 is_empty = len(stack) == 0
print("栈是否为空 =", is_empty)
``` ```
## 栈的实现 ## 栈的实现
@ -148,14 +130,14 @@ comments: true
/* 基于链表实现的栈 */ /* 基于链表实现的栈 */
class LinkedListStack { class LinkedListStack {
private ListNode stackPeek; // 将头结点作为栈顶 private ListNode stackPeek; // 将头结点作为栈顶
private int stackSize = 0; // 栈的长度 private int stkSize = 0; // 栈的长度
public LinkedListStack() { public LinkedListStack() {
stackPeek = null; stackPeek = null;
} }
/* 获取栈的长度 */ /* 获取栈的长度 */
public int size() { public int size() {
return stackSize; return stkSize;
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
public boolean isEmpty() { public boolean isEmpty() {
@ -166,15 +148,13 @@ comments: true
ListNode node = new ListNode(num); ListNode node = new ListNode(num);
node.next = stackPeek; node.next = stackPeek;
stackPeek = node; stackPeek = node;
stackSize++; stkSize++;
} }
/* 出栈 */ /* 出栈 */
public int pop() { public int pop() {
if (size() == 0)
throw new IndexOutOfBoundsException();
int num = peek(); int num = peek();
stackPeek = stackPeek.next; stackPeek = stackPeek.next;
stackSize--; stkSize--;
return num; return num;
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
@ -193,16 +173,16 @@ comments: true
class LinkedListStack { class LinkedListStack {
private: private:
ListNode* stackTop; // 将头结点作为栈顶 ListNode* stackTop; // 将头结点作为栈顶
int stackSize; // 栈的长度 int stkSize; // 栈的长度
public: public:
LinkedListStack() { LinkedListStack() {
stackTop = nullptr; stackTop = nullptr;
stackSize = 0; stkSize = 0;
} }
/* 获取栈的长度 */ /* 获取栈的长度 */
int size() { int size() {
return stackSize; return stkSize;
} }
/* 判断栈是否为空 */ /* 判断栈是否为空 */
bool empty() { bool empty() {
@ -213,15 +193,13 @@ comments: true
ListNode* node = new ListNode(num); ListNode* node = new ListNode(num);
node->next = stackTop; node->next = stackTop;
stackTop = node; stackTop = node;
stackSize++; stkSize++;
} }
/* 出栈 */ /* 出栈 */
int pop() { int pop() {
if (size() == 0) int num = top();
throw out_of_range("栈为空");
int num = stackTop->val;
stackTop = stackTop->next; stackTop = stackTop->next;
stackSize--; stkSize--;
return num; return num;
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
@ -259,12 +237,10 @@ comments: true
""" 出栈 """ """ 出栈 """
def pop(self): def pop(self):
# 判空处理 num = self.peek()
if not self.__peek: return None
pop = self.__peek.val
self.__peek = self.__peek.next self.__peek = self.__peek.next
self.__size -= 1 self.__size -= 1
return pop return num
""" 访问栈顶元素 """ """ 访问栈顶元素 """
def peek(self): def peek(self):

Loading…
Cancel
Save