1.1数组栈
package datastructure;
/*只有栈顶元素可以访问
* Array([]):最高效;但是其容量固定且无法动态改变;只能存基础数据类型
ArrayList: 容量可动态增长;但牺牲效率;存对象不可以存基础数据类型,有多种方法调用
size(),add(),remove()*/
public class ArrayStack {
static final int CAPACITY = 1997;
//topOfStack是顶端的值,其实他是一个操作,考察表顶端元素并返回他的值
int topOfStack;
int arrayStack[];
public ArrayStack() {
topOfStack = -1;
arrayStack = new int[CAPACITY];
}
//将数据压进栈;
public boolean push(int val) {
if(topOfStack >= (CAPACITY-1)) {
System.out.println("栈溢出(overflow)");
return false;
}
arrayStack[++topOfStack] = val;
return true;
}
//弹出栈顶元素(取出),显示新的栈顶元素
public int pop() {
if(topOfStack < 0) {
System.out.println("栈下溢(Underflow) ");
return 0;
}
//指针减一位,直接override
arrayStack[topOfStack] = 0;
int newTop = arrayStack[topOfStack--];
return newTop;
}
//查看当前栈顶元素
public int peek() {
if(topOfStack < 0) {
System.out.println("栈是空的(下溢)");
}
return arrayStack[topOfStack];
}
public boolean isEmpty() {
return topOfStack < 0;
}
public static void main(String[] args) {
ArrayStack test1 = new ArrayStack();
test1.push(1);
test1.push(9);
test1.push(9);
System.out.println(test1.push(7));
for(int a = 0;a < 4;a++) {
System.out.print(test1.arrayStack[a]);
}
/*打印所有堆栈
* for(int a : test1.arrayStack) {
System.out.println(a);
}*/
test1.push(0);
test1.pop();
System.out.println();
System.out.println(test1.pop());
for(int a = 0;a < 4;a++) {
System.out.print(test1.arrayStack[a]);
}
test1.push(6);
System.out.println();
for(int a = 0;a < 4;a++) {
System.out.print(test1.arrayStack[a]);
}
}
}
1.2链表栈
package datastructure;
public class LinkedListStack {
static class StackNode{
int val;
StackNode next;
public StackNode(int val){
this.val = val;
}
}
int val;
StackNode topNode;
public LinkedListStack() {
topNode = null;
}
public void push(int val) {
StackNode newNode = new StackNode(val);
if(topNode == null) {
topNode = newNode;
}else {
/*
* 就将新元素的下一节点指向当前的top,并将newNode更新为top节点。
* 注意,此时新的top节点就不再是指向null而是前一个节点*/
StackNode prevTop = topNode;
topNode = newNode;
newNode.next = prevTop;
}
System.out.println(val + " 已进栈.");
}
public void pop() {
if(topNode == null) {
System.out.println("栈是空的");
}
int popped = topNode.val;
//因为指针是反的所以,下一个节点就是他前一个节点;
topNode = topNode.next;
System.out.println(popped + " 已出栈.");
}
public int peek() {
if(topNode == null) {
System.out.println("栈是空的");
}
return topNode.val;
}
public boolean isEmpty() {
return topNode == null;
}
public void display(int val) {
StackNode curTop = new StackNode(val);
while (curTop != null) {
System.out.println(curTop.val);
curTop = curTop.next;
}
}
public static void main(String[] args) {
LinkedListStack test2 = new LinkedListStack();
test2.push(1);
test2.push(9);
test2.push(9);
test2.push(7);
test2.pop();
test2.pop();
test2.push(0);
test2.push(3);
test2.pop();
test2.pop();
test2.pop();
}
}
2.1顺序队列(数组)
package datastructure;
public class ArrayQueue {
int front,back,currentSize;
int capacity;
int arrayQueue[];
public ArrayQueue(int capacity) {
this.capacity = capacity;
front = back = currentSize = 0;
arrayQueue = new int[capacity];
}
public boolean isEmpty() {
return currentSize == 0;
}
//队列是否满了
public boolean isFull() {
return currentSize == capacity;
}
//进队
public void enqueue(int val) {
if(isFull()) return;
arrayQueue[back] = val;
back = (back + 1) % capacity;
currentSize++;
System.out.println(val + "已入队");
}
public int dequeue() {
if(isEmpty()) {
System.out.println("队列是空的");
}
int val = arrayQueue[front];
front = (front + 1) % capacity;
currentSize--;
return val;
}
public int peek() {
if(isEmpty()) {
System.out.println("队列是空的");
}
return arrayQueue[front];
}
public static void main(String[] args) {
ArrayQueue queue1 = new ArrayQueue(3);
queue1.enqueue(1);
queue1.enqueue(9);
queue1.enqueue(9);
System.out.println(queue1.dequeue());
System.out.println(queue1.dequeue());
for(int a = 0;a < 3;a++) {
System.out.print(queue1.arrayQueue[a]);
}
}
}
2.2链式队列
package datastructure;
public class LinkedListQueue {
QueueNode front;
QueueNode back;
static class QueueNode{
int value;
QueueNode next;
public QueueNode(int value) {
this.value = value;
}
}
public void enqueue(int value) {
QueueNode newNode = new QueueNode(value);
if(this.back == null) {
this.front = this.back = newNode;
return;
}
this.back.next = newNode;
this.back = newNode;
}
public int dequeue() {
if(this.front == null) {
System.out.println("队列是空的");
}
QueueNode frontNode = this.front;
this.front = this.front.next;
//如果只有一个节点,那么出去后依然是空的
if(this.front == null) {
this.back = null;
}
return frontNode.value;
}
public static void main(String[] args) {
LinkedListQueue queue = new LinkedListQueue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
}
}
B栈参考视频:https://www.bilibili.com/video/BV1RQ4y1K7bb