
文章前言
- 【一起学算法】专栏持续更新中,会在这里记录算法学习的过程
- 文末获取【一起学算法】
Github仓库手写算法源码
,一起跟着写一遍吧~- 【一起学算法】中所涉及的部分关于
leetcode
中的原题均可在leetcode官网的运行器通关~
栈
栈的概念就是: 数据先进后出(子弹匣原理)
手写栈
主要功能
单链表实现
思路图解
关键代码
/**
* @author Rhys.Ni
* @version 1.0
* @date 2021/12/9 2:45 上午
* @Description
*/
public class MyStack<T> {
private Node<T> head;
private int size;
public int size() {
return this.size;
}
public Boolean isEmpty() {
return size == 0;
}
/**
* 压栈
* @param value
*/
public void push(T value) {
//遵循队列先进后出
//从头节点开始弹出,符合队列先进后出机制
Node<T> currNode = new Node<>(value);
if (head != null) {
currNode.next = head;
}
head = currNode;
size++;
}
/**
* 弹栈
* @return
*/
public T pop() {
T val = null;
if (head != null) {
val = head.value;
head = head.next;
size--;
}
return val;
}
/**
* 查看栈顶值
* @return
*/
public T peek() {
return head == null ? null : head.value;
}
}
队列
数据先进先出(排队)
手写队列
主要功能
单链表实现
思路图解
关键代码
/**
* @author Rhys.Ni
* @version 1.0
* @date 2021/12/9 12:05 上午
* @Description
*/
public class MyQueue<T> {
private Node<T> head;
private Node<T> tail;
private int size;
public int size() {
return this.size;
}
public Boolean isEmpty() {
return size == 0;
}
public void offer(T value) {
//遵循队列先进先出
//从头节点开始弹出,符合队列先进先出机制
Node<T> currNode = new Node<>(value);
if (tail == null) {
head = currNode;
tail = currNode;
} else {
tail.next = currNode;
tail = currNode;
}
size++;
}
public T poll() {
T value = null;
if (head != null) {
//获取当前头结点的值
value = head.value;
//让当前头节点的下一个节点成为新的头节点
head = head.next;
//每弹出一个值size同步递减
size--;
}
if (head==null){
//说明没有值了,保持头尾节点一致;
tail = null;
}
return value;
}
public T peek() {
T value = null;
if (head != null) {
//获取当前头结点的值
value = head.value;
}
return value;
}
public MyQueue() {
this.head = null;
this.tail = null;
this.size = 0;
}
}
手写双端队列
主要功能
思路图解
关键代码
/**
* @author Rhys.Ni
* @version 1.0
* @date 2021/12/13 1:34 上午
* @Description
*/
public class MyDeque<T> {
private DoubleNode<T> head;
private DoubleNode<T> tail;
private int size;
public void pushHead(T value) {
DoubleNode<T> curr = new DoubleNode<>(value);
if (head == null) {
head = curr;
tail = curr;
} else {
head.last = curr;
curr.next = head;
head = curr;
}
size++;
}
public void pushTail(T value) {
DoubleNode<T> curr = new DoubleNode<>(value);
if (head == null) {
head = curr;
tail = curr;
} else {
tail.next = curr;
curr.last = tail;
tail = curr;
}
size++;
}
public T pollHead() {
T value = null;
if (head == null) {
return value;
}
value = head.value;
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
head.last = null;
}
size--;
return value;
}
public T pollTail() {
T value = null;
if (tail == null) {
return value;
}
value = tail.value;
if (tail == head) {
tail = null;
head = null;
} else {
tail = tail.last;
tail.next = null;
}
size--;
return value;
}
public T peekHead() {
T value = null;
if (!isEmpty()) {
value = head.value;
}
return value;
}
public T peekTail() {
T value = null;
if (!isEmpty()) {
value = tail.value;
}
return value;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public MyDeque() {
this.head = null;
this.tail = null;
this.size = 0;
}
}
源码获取地址
加油小伙伴~ 点击获取算法篇代码~