1.栈:好比放一叠书,一本一本网上叠,拿的时候要先拿走上面的再拿走下面的(先进后出)
理解
(1) 逆波兰表示法是一种将运算符卸载操作数后加的描述程序的方法
比如:(1+2)*(3+4)
逆波兰表示法: 1 2 + 3 4 + *
java编程思想例子:
public class MyQueue1 {
private LinkedList queue = new LinkedList<>();
public void push(T v) {
queue.addFirst(v);
}
public T peek() {
return queue.getFirst();
}
public T pop() {
return queue.removeFirst();
}
public boolean isEmpty() {
return queue.isEmpty();
}
}
使用数组实现:
public class MyStack {
private Integer[] arr;
private int index;
private int size;
public MyStack(int capacity) {
this.arr = new Integer[capacity];
this.index = -1;
this.size = capacity;
}
public void push(Integer value) {
arr[++index] = value;
}
public Integer pop() {
// arr[index] = null;
// index--;
return arr[index--];
}
public long peek() {
return arr[index];
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return index == -1;
}
/**
* 判断是否满了
*/
public boolean isFull() {
return index == arr.length - 1;
}
}
测试
public static void myStackTest() {
MyStack stack = new MyStack(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
stack.push(4);
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
输出结果
4
2
1
@see Stack
2.列队
有头有尾,相当于排队,排在前面的先买,遵循先进先出
public class MyQueue {
private Integer[] arr;
private int first;
private int last;
private int elements;
public MyQueue(int size) {
arr = new Integer[size];
this.first = 0;
this.elements = 0;
this.last = -1;
}
public void insert(int value) {
arr[++last] = value;
elements++;
}
public Integer remove() {
elements--;
return arr[first++];
}
public Integer peek() {
return arr[first];
}
public boolean isEmpty() {
return elements == 0;
}
public boolean isFull() {
return elements == arr.length;
}
}
3.链表
单链表
看图,可以了解到 它适合增删,
只要简单的修改某个元素的指向,然后把该元素指向它原指向的数据即可
public class Nodes {
private int data;
private Nodes next;
public Nodes(Integer value) {
this.data = value;
}
public void display() {
System.out.print(this.data + ",");
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Nodes getNext() {
return next;
}
public void setNext(Nodes next) {
this.next = next;
}
}
private Nodes first;
public LinkList() {
first = null;
}
public void insertFirst(Integer value) {
Nodes nodes = new Nodes(value);
if (first == null) {
first = nodes;
} else {
nodes.setNext(first);
first = nodes;
}
}
public Nodes removeFirst() {
Nodes f1 = first;
first = f1.getNext();
return f1;
}
public void display() {
Nodes current = first;
while (current != null) {
current.display();
current = current.getNext();
}
}
public Nodes find(Integer value) {
Nodes current = first;
while (current.getData() != value) {
if (current.getNext() == null) {
return null;
}
current = current.getNext();
}
return current;
}
public Nodes deleteByDate(Integer value) {
Nodes current = first;
Nodes previous = first;
while (current.getData() != value) {
if (current.getNext() == null) {
return null;
}
previous = current;
current = current.getNext();
}
if (first == current) {
first = first.getNext();
} else {
previous.setNext(current.getNext());
// first.setNext(previous.getNext());
}
return current;
}
测试:
private static void myLinkList() {
LinkList linkList = new LinkList();
linkList.insertFirst(1);
linkList.insertFirst(2);
linkList.insertFirst(3);
linkList.insertFirst(31);
linkList.display();
System.out.println("***********");
//linkList.removeFirst();
linkList.display();
Nodes s = linkList.find(2);
System.out.println();
System.out.println(s.getData());
System.out.println();
linkList.deleteByDate(31);
linkList.display();
}
结果:
31,3,2,1,***********
31,3,2,1,
2
3,2,1,
4.双端链表,双向链表
(1)双端链表,尾部插入一个元素
当我们需要在链表的尾部插入一个元素这样就挺麻烦,所以在定义LinkList的时候,定义一个last的成员变量,记住最后一个节点,当insertLast的时候,只要将原先的last指向新的last node
public class LinkList {
private Nodes first;
private Nodes last;
public LinkList() {
first = null;
}
public void insertFirst(Integer value) {
Nodes nodes = new Nodes(value);
if (isEmpty()) {
last = nodes;
}
nodes.setNext(first);
first = nodes;
}
public void insertLast(Integer value) {
Nodes nodes = new Nodes(value);
if (isEmpty()) {
first = nodes;
} else {
last.setNext(nodes);
}
last = nodes;
}
public Nodes removeFirst() {
Nodes f1 = first;
first = f1.getNext();
if (first == null) {
last = null;
}
return f1;
}
public void display() {
Nodes current = first;
while (current != null) {
current.display();
current = current.getNext();
}
}
public Nodes find(Integer value) {
Nodes current = first;
while (current.getData() != value) {
if (current.getNext() == null) {
return null;
}
current = current.getNext();
}
return current;
}
public Nodes deleteByDate(Integer value) {
Nodes current = first;
Nodes previous = first;
while (current.getData() != value) {
if (current.getNext() == null) {
return null;
}
previous = current;
current = current.getNext();
}
if (current.getNext() == null) {
last = previous;
}
if (first == current) {
first = first.getNext();
} else {
previous.setNext(current.getNext());
// first.setNext(previous.getNext());
}
return current;
}
public boolean isEmpty() {
return first == null;
}
(2)双向链表,尾部删除元素
无法马上得到链表尾部的上一个节点
public class DouPosiLinkList {
private Nodes first;
/**
* 记录最后一个节点
*/
private Nodes last;
public DouPosiLinkList() {
first = null;
}
public void insertFirst(Integer value) {
Nodes nodes = new Nodes(value);
if (isEmpty()) {
last = nodes;
} else {
first.setPrevious(nodes);
}
nodes.setNext(first);
first = nodes;
}
public void insertLast(Integer value) {
Nodes nodes = new Nodes(value);
if (isEmpty()) {
first = nodes;
} else {
last.setNext(nodes);
nodes.setPrevious(last);
}
last = nodes;
}
public Nodes removeFirst() {
Nodes f1 = first;
first = f1.getNext();
if (first == null) {
last = null;
} else {
first.getNext().setPrevious(null);
}
return f1;
}
public Nodes removeLast() {
Nodes tmp = last;
if (first.getNext() == null) {
first = null;
} else {
last.getPrevious().setNext(null);
}
last = last.getPrevious();
return last;
}
public void display() {
Nodes current = first;
while (current != null) {
current.display();
current = current.getNext();
}
System.out.println();
}
public Nodes find(Integer value) {
Nodes current = first;
while (current.getData() != value) {
if (current.getNext() == null) {
return null;
}
current = current.getNext();
}
return current;
}
public Nodes deleteByDate(Integer value) {
Nodes current = first;
Nodes previous = first;
while (current.getData() != value) {
if (current.getNext() == null) {
return null;
}
previous = current;
current = current.getNext();
}
if (current.getNext() == null) {
last = previous;
}
if (first == current) {
first = first.getNext();
} else {
previous.setNext(current.getNext());
// first.setNext(previous.getNext());
}
return current;
}
public boolean isEmpty() {
return first == null;
}
}
测试:
{
DouPosiLinkList linkList = new DouPosiLinkList();
linkList.insertFirst(1);
linkList.insertFirst(2);
linkList.insertFirst(3);
linkList.insertFirst(31);
linkList.display();
linkList.removeFirst();
System.out.println("==================");
linkList.display();
linkList.removeLast();
System.out.println("==================");
linkList.display();
}
输出结果
31,3,2,1,
==================
3,2,1,
==================
3,2,