一、栈
- 一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则。
- 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
- 出栈:栈的删除操作叫做出栈,出数据在栈顶。
栈的核心操作:
- 入栈:把元素放到栈里
- 出栈:把最后进来的元素删掉
- 取栈顶元素:获取到最后一个进来的元素的结果
使用顺序表实现栈:
使用尾插操作表示入栈;使用尾删操作表示出栈;使用根据下表获取元素的操作表示取栈顶元素
public class MyStack {
private int[] data = new int[100];
private int size = 0;//有效元素个数
// 基本操作
// 1. 入栈--尾插
public void push(int val) {
if (size >= data.length) {
// 满了,在这里也可以进行扩容.
return;
}
data[size] = val;
size++;
}
// 2. 出栈,返回值就是被出栈了的那个元素--尾删
public Integer pop() {
if (size == 0) {
//空栈
return null;
}
// 栈顶元素就是最后一个元素
int ret = data[size - 1];
size--;
return ret;
}
// 3. 取栈顶元素--根据下标获取元素
public Integer peek() {
if (size == 0) {
return null;
}
return data[size - 1];
}
}
使用链表实现栈
使用头插操作表示入栈;使用头删操作表示出栈;直接取头结点表示取栈顶元素
class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
}
}
public class MyStack2 {
// 此处使用不带傀儡节点的链表来表示.
// 如果使用带傀儡节点的链表的话, 就更简单了.
private Node head = null;
// 核心操作
// 1. 入栈--头插
public void push(int val) {
Node newNode = new Node(val);
// 把新节点进行头插
// 由于当前是不带傀儡节点的, 所以就需要判定当前链表是空还是非空
if (head == null) {
head = newNode;
return;
}
//非空
newNode.next = head;
head = newNode;
}
// 2. 出栈--头删,返回值就是被出栈了的那个元素
public Integer pop() {
if (head == null) {
return null;
}
//只有一个元素,删完是空链表
if (head.next == null) {
int ret = head.val;
head = null;
return ret;
}
int ret = head.val;
head = head.next;
return ret;
}
// 3. 取栈顶元素--取头结点
public Integer peek() {
if (head == null) {
return null;
}
return head.val;
}
}
二、队列
- 只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
- 入队列:进行插入操作的一端称为队尾(Tail/Rear)
- 出队列:进行删除操作的一端称为队头(Head/Front)
使用链表实现队列
使用尾插操作表示入队列;使用头删操作表示出队列;直接获取头结点表示取队列首元素
public class MyQueue {
static class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
}
}
// 创建一个链表的头结点
// 为了方便的进行尾插, 也记录尾节点.
private Node head = null;
private Node tail = null;
// 队列的核心操作
// 1. 入队列--尾插, 返回值表示插入成功/失败 (也是为了和标准库的队列的 offer 接口看齐)
public boolean offer(int val) {
Node newNode = new Node(val);
// 插入到链表尾部. 需要考虑当前链表是否为空
if (head == null) {
// 直接让 head 和 tail 指向新节点即可.
head = newNode;
tail = newNode;
return true;
}
tail.next = newNode;
tail = tail.next;
return true;
}
// 2. 出队列--头删
public Integer poll() {
if (head == null) {
return null;
}
int ret = head.val;
if (head.next == null) {
head = null;
return ret;
}
head = head.next;
return ret;
}
// 3. 取队首元素--获取头结点
public Integer peek() {
if (head == null) {
return null;
}
return head.val;
}
}
使用数组来实现环形队列
- 队列有效元素区间为 [ head,Tail )
- 入队列:把新元素放到Tail对应的下标上,同时Tail++
- 出队列:就是head++
public class MyQueue2 {
private int[] data = new int[100];
// 队列有效区间 [head, tail)
private int head = 0;
private int tail = 0;
private int size = 0;//记录元素个数,区分空队列和满队列
// 核心操作
// 1. 入队列
public boolean offer(int val) {
if (size == data.length) {
// 队列满了. 此处也可以实现扩容逻辑.
return false;
}
// 把新元素放到 tail 对应的下标上.
data[tail] = val;
// 自增 tail
tail++;
// 一旦 tail 到达了数组的末尾, 就需要让 tail 从头开始
if (tail == data.length) {
tail = 0;
}
// 更新 size 的值.
size++;
return true;
}
// 2. 出队列
public Integer poll() {
if (size == 0) {
return null;
}
int ret = data[head];
// 更新 head 的位置
head++;
// 一旦 head 到达了数组的末尾, 就需要让 head 从头开始
if (head == data.length) {
head = 0;
}
// 更新 size 的值.
size--;
return ret;
}
// 3. 取队首元素
public Integer peek() {
if (size == 0) {
return null;
}
return data[head];
}
}