This commit is contained in:
krahets
2023-04-23 14:58:03 +08:00
parent 881ece517f
commit fe8027e64a
26 changed files with 344 additions and 626 deletions

View File

@@ -1067,7 +1067,7 @@ comments: true
typedef struct linkedListDeque linkedListDeque;
/* 构造j */
/* 构造函数 */
linkedListDeque *newLinkedListDeque() {
linkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque));
deque->front = NULL;
@@ -1606,14 +1606,14 @@ comments: true
/* 访问队首元素 */
public int peekFirst() {
if (isEmpty())
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
return nums[front];
}
/* 访问队尾元素 */
public int peekLast() {
if (isEmpty())
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
// 计算尾元素索引
int last = index(front + queSize - 1);
return nums[last];
@@ -1812,12 +1812,14 @@ comments: true
def peek_first(self) -> int:
"""访问队首元素"""
assert not self.is_empty(), "双向队列为空"
if self.is_empty():
raise IndexError("双向队列为空")
return self.__nums[self.__front]
def peek_last(self) -> int:
"""访问队尾元素"""
assert not self.is_empty(), "双向队列为空"
if self.is_empty():
raise IndexError("双向队列为空")
# 计算尾元素索引
last = self.index(self.__front + self.__size - 1)
return self.__nums[last]

View File

@@ -328,7 +328,7 @@ comments: true
/* 访问队首元素 */
public int peek() {
if (size() == 0)
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
return front.val;
}
@@ -768,43 +768,35 @@ comments: true
```csharp title="linkedlist_queue.cs"
/* 基于链表实现的队列 */
class LinkedListQueue
{
class LinkedListQueue {
private ListNode? front, rear; // 头节点 front ,尾节点 rear
private int queSize = 0;
public LinkedListQueue()
{
public LinkedListQueue() {
front = null;
rear = null;
}
/* 获取队列的长度 */
public int size()
{
public int size() {
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty()
{
public bool isEmpty() {
return size() == 0;
}
/* 入队 */
public void push(int num)
{
public void push(int num) {
// 尾节点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空,则令头、尾节点都指向该节点
if (front == null)
{
if (front == null) {
front = node;
rear = node;
// 如果队列不为空,则将该节点添加到尾节点后
}
else if (rear != null)
{
} else if (rear != null) {
rear.next = node;
rear = node;
}
@@ -812,8 +804,7 @@ comments: true
}
/* 出队 */
public int pop()
{
public int pop() {
int num = peek();
// 删除头节点
front = front?.next;
@@ -822,23 +813,20 @@ comments: true
}
/* 访问队首元素 */
public int peek()
{
public int peek() {
if (size() == 0 || front == null)
throw new Exception();
return front.val;
}
/* 将链表转化为 Array 并返回 */
public int[] toArray()
{
public int[] toArray() {
if (front == null)
return Array.Empty<int>();
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.Length; i++)
{
for (int i = 0; i < res.Length; i++) {
res[i] = node.val;
node = node.next;
}
@@ -1086,7 +1074,7 @@ comments: true
/* 访问队首元素 */
public int peek() {
if (isEmpty())
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
return nums[front];
}
@@ -1207,9 +1195,10 @@ comments: true
def push(self, num: int) -> None:
"""入队"""
assert self.__size < self.capacity(), "队列已满"
if self.__size == self.capacity():
raise IndexError("队列已满")
# 计算尾指针,指向队尾索引 + 1
# 通过取余操作,实现 rear 越过数组尾部后回到头部
# 通过取余操作,实现 rear 越过数组尾部后回到头部F
rear: int = (self.__front + self.__size) % self.capacity()
# 将 num 添加至队尾
self.__nums[rear] = num
@@ -1225,7 +1214,8 @@ comments: true
def peek(self) -> int:
"""访问队首元素"""
assert not self.is_empty(), "队列为空"
if self.is_empty():
raise IndexError("队列为空")
return self.__nums[self.__front]
def to_list(self) -> list[int]:
@@ -1537,41 +1527,34 @@ comments: true
```csharp title="array_queue.cs"
/* 基于环形数组实现的队列 */
class ArrayQueue
{
class ArrayQueue {
private int[] nums; // 用于存储队列元素的数组
private int front; // 队首指针,指向队首元素
private int queSize; // 队列长度
public ArrayQueue(int capacity)
{
public ArrayQueue(int capacity) {
nums = new int[capacity];
front = queSize = 0;
}
/* 获取队列的容量 */
public int capacity()
{
public int capacity() {
return nums.Length;
}
/* 获取队列的长度 */
public int size()
{
public int size() {
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty()
{
public bool isEmpty() {
return queSize == 0;
}
/* 入队 */
public void push(int num)
{
if (queSize == capacity())
{
public void push(int num) {
if (queSize == capacity()) {
Console.WriteLine("队列已满");
return;
}
@@ -1584,8 +1567,7 @@ comments: true
}
/* 出队 */
public int pop()
{
public int pop() {
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
@@ -1594,20 +1576,17 @@ comments: true
}
/* 访问队首元素 */
public int peek()
{
public int peek() {
if (isEmpty())
throw new Exception();
return nums[front];
}
/* 返回数组 */
public int[] toArray()
{
public int[] toArray() {
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++)
{
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[j % this.capacity()];
}
return res;

View File

@@ -320,7 +320,7 @@ comments: true
/* 访问栈顶元素 */
public int peek() {
if (size() == 0)
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
return stackPeek.val;
}
@@ -706,31 +706,26 @@ comments: true
```csharp title="linkedlist_stack.cs"
/* 基于链表实现的栈 */
class LinkedListStack
{
class LinkedListStack {
private ListNode? stackPeek; // 将头节点作为栈顶
private int stkSize = 0; // 栈的长度
public LinkedListStack()
{
public LinkedListStack() {
stackPeek = null;
}
/* 获取栈的长度 */
public int size()
{
public int size() {
return stkSize;
}
/* 判断栈是否为空 */
public bool isEmpty()
{
public bool isEmpty() {
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
public void push(int num) {
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
@@ -738,8 +733,7 @@ comments: true
}
/* 出栈 */
public int pop()
{
public int pop() {
if (stackPeek == null)
throw new Exception();
@@ -750,23 +744,20 @@ comments: true
}
/* 访问栈顶元素 */
public int peek()
{
public int peek() {
if (size() == 0 || stackPeek == null)
throw new Exception();
return stackPeek.val;
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray()
{
public int[] toArray() {
if (stackPeek == null)
return Array.Empty<int>();
ListNode node = stackPeek;
int[] res = new int[size()];
for (int i = res.Length - 1; i >= 0; i--)
{
for (int i = res.Length - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
}
@@ -956,14 +947,14 @@ comments: true
/* 出栈 */
public int pop() {
if (isEmpty())
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
return stack.remove(size() - 1);
}
/* 访问栈顶元素 */
public int peek() {
if (isEmpty())
throw new EmptyStackException();
throw new IndexOutOfBoundsException();
return stack.get(size() - 1);
}
@@ -1042,12 +1033,14 @@ comments: true
def pop(self) -> int:
"""出栈"""
assert not self.is_empty(), "栈为空"
if self.is_empty():
raise IndexError("栈为空")
return self.__stack.pop()
def peek(self) -> int:
"""访问栈顶元素"""
assert not self.is_empty(), "栈为空"
if self.is_empty():
raise IndexError("栈为空")
return self.__stack[-1]
def to_list(self) -> list[int]:
@@ -1262,36 +1255,30 @@ comments: true
```csharp title="array_stack.cs"
/* 基于数组实现的栈 */
class ArrayStack
{
class ArrayStack {
private List<int> stack;
public ArrayStack()
{
public ArrayStack() {
// 初始化列表(动态数组)
stack = new();
}
/* 获取栈的长度 */
public int size()
{
public int size() {
return stack.Count();
}
/* 判断栈是否为空 */
public bool isEmpty()
{
public bool isEmpty() {
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
public void push(int num) {
stack.Add(num);
}
/* 出栈 */
public int pop()
{
public int pop() {
if (isEmpty())
throw new Exception();
var val = peek();
@@ -1300,16 +1287,14 @@ comments: true
}
/* 访问栈顶元素 */
public int peek()
{
public int peek() {
if (isEmpty())
throw new Exception();
return stack[size() - 1];
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray()
{
public int[] toArray() {
return stack.ToArray();
}
}