update stack and queue.

This commit is contained in:
Yudong Jin
2022-11-30 03:46:53 +08:00
parent 8669e06414
commit acfdc713ba
13 changed files with 321 additions and 124 deletions

View File

@@ -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;
}
}

View File

@@ -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()));

View File

@@ -7,46 +7,69 @@
package chapter_stack_and_queue;
import java.util.*;
import include.*;
/* 基于链表实现的队列 */
class LinkedListQueue {
LinkedList<Integer> 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;
}
}

View File

@@ -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;
}