栈的介绍
-
栈的英文为(stack) 栈是一个先入后出(FILO-First In Last Out)的有序列表。
-
栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
-
根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除
思路
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/64a8f55afb9d9a5a93dea0f56915792b.png)
数组实现栈代码
package com.structure.stack;
/**
* @author zzq
* @Date 2021-07-26 9:36
*/
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(5);
arrayStack.push(1);
arrayStack.push(2);
arrayStack.push(3);
;
System.out.println("出栈:"+arrayStack.pop());
arrayStack.showStack();
}
}
class ArrayStack {
private int top;
private int[] stack;
private int maxSize;
public ArrayStack(int size) {
this.top = -1;
this.stack = new int[size];
this.maxSize = size;
}
public boolean isFull(){
return top == maxSize-1;
}
public boolean isEmpty(){
return top == -1;
}
public void push(int ele) {
if (isFull()){
System.out.println("栈已满");
return;
}
top++;
stack[top] = ele;
}
public int pop() {
if (isEmpty()){
System.out.println("栈空了");
throw new RuntimeException("stack is empty");
}
return stack[top--];
}
public void showStack() {
if (isEmpty()){
System.out.println("栈空了");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("栈元素:%d \n",stack[i]);
}
}
public int getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public int[] getStack() {
return stack;
}
public void setStack(int[] stack) {
this.stack = stack;
}
}
链表实现栈代码
package com.structure.stack;
/**
* @author zzq
* @Date 2021-07-23 14:09
*/
public class StackDemo {
public static void main(String[] args) {
StackNode stackNode1 = new StackNode(1);
StackNode stackNode2 = new StackNode(2);
StackNode stackNode3 = new StackNode(3);
StackNode stackNode4 = new StackNode(4);
Stack stack = new Stack();
stack.add(stackNode1);
stack.add(stackNode2);
stack.add(stackNode3);
stack.add(stackNode4);
stack.showStack();
System.out.println("```````````````");
stack.reversePrint();
stack.get();
stack.get();
stack.get();
stack.get();
stack.showStack();
}
}
class Stack {
private StackNode stackHead;
private StackNode stackTail = new StackNode(0);
/**
* 添加时从栈顶压入栈底,栈底是动的
* @param stackNode
*/
public void add(StackNode stackNode){
if(stackHead == null){
stackHead = stackNode;
stackTail.setPre(stackNode);
stackNode.setNext(stackTail);
return;
}
StackNode temp = stackTail;
while (true){
if(temp.getPre() == null){
//找到最上面的一个
break;
}
temp = temp.getPre();
}
temp.setPre(stackNode);
stackNode.setNext(temp);
stackHead = stackNode;
}
/**
* 出栈,元素减少
*/
public void get(){
if(stackHead==null || stackHead == stackTail){
System.out.println("栈空了");
return;
}
System.out.printf("元素 %d 出栈 \n",stackHead.getNo());
stackHead = stackHead.getNext();
stackHead.setPre(null);
}
public void showStack(){
StackNode temp = stackTail.getPre();
while (true){
if(temp == null){
//找到最上面的一个
break;
}
System.out.printf("元素%d \n",temp.getNo());
temp = temp.getPre();
}
}
public void reversePrint(){
if(stackHead==null || stackHead == stackTail){
System.out.println("栈空了");
return;
}
StackNode head = stackHead;
while (true){
if(head == stackTail){
break;
}
System.out.println("元素:"+head.getNo());
head = head.getNext();
}
}
public StackNode getStackHead() {
return stackHead;
}
public void setStackHead(StackNode stackHead) {
this.stackHead = stackHead;
}
public StackNode getStackTail() {
return stackTail;
}
public void setStackTail(StackNode stackTail) {
this.stackTail = stackTail;
}
}
class StackNode {
private int no;
private StackNode pre;
private StackNode next;
public StackNode(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public StackNode getPre() {
return pre;
}
public void setPre(StackNode pre) {
this.pre = pre;
}
public StackNode getNext() {
return next;
}
public void setNext(StackNode next) {
this.next = next;
}
}