1、栈的基本介绍
-
栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
-
栈是一个先入后出(FILO-First ln Last Out)的有序列表。
-
根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除
-
可以使用数组和链表模拟栈
2、数组模拟栈代码实现
//数组模拟栈
public class ArrayStackDemo {
public static void main(String[] args) {
//先创建一个ArrayStack对象
ArrayStack arrayStack = new ArrayStack(4);
String key = "";
boolean loop = true; //控制是否退出菜单
Scanner scanner = new Scanner(System.in);
while(loop){
System.out.println("push:添加数据到栈(入栈)");
System.out.println("pop:从栈取出数据(出栈)");
System.out.println("show:显示栈");
System.out.println("exit:退出栈");
System.out.println("请输入:");
key = scanner.next();
switch(key){
case "show":
arrayStack.show();
break;
case "exit":
loop = false;
break;
case "push":
System.out.println("请输入一个数;");
int n = scanner.nextInt();
arrayStack.push(n);
break;
case "pop":
try {
int res = arrayStack.pop();
System.out.println("出栈的数据是:"+res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
//表示栈结构
class ArrayStack{
private int maxSize; //栈的大小
private int[] stack; //数组,数组模拟栈,数组就放在数组
private int top = -1; //栈顶,初始化为-1,表示没有数据
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//栈满
public boolean isFull() {
return top == maxSize - 1;
}
//栈空
public boolean isEmpty() {
return top == -1;
}
//入栈-pop
public void push(int value){
//先判断栈是否满
if(isFull()){
System.out.println("z栈满");
return;
}
top++;
stack[top] = value;
}
//出栈-push,将栈顶的数据返回
public int pop(){
//先判断栈是否空
if(isEmpty()){
//抛出异常
throw new RuntimeException("栈空,没有数据");
}
int value = stack[top];
top--;
return value;
}
//显示栈的情况【遍历栈】从栈顶往下遍历
public void show(){
if(isEmpty()){
System.out.println("栈空,没有数据");
return;
}
for (int i = top;i >= 0; i--) {
System.out.printf("stack[%d] = %d\n",i,stack[i]);
}
}
}
3、单链表模拟栈代码实现
//单链表模拟栈
public class SingleLinkedListStackDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
String key = "";
boolean loop = true;
Scanner scanner = new Scanner(System.in);
while (loop) {
System.out.println("push:添加数据到栈(入栈)");
System.out.println("pop:从栈取出数据(出栈)");
System.out.println("show:显示栈");
System.out.println("exit:退出栈");
System.out.println("请输入:");
key = scanner.next();
switch (key) {
case "push":
System.out.println("请输入一个数:");
int n = scanner.nextInt();
Node node = new Node(n);
singleLinkedList.push(node);
break;
case "pop":
try {
int val = singleLinkedList.pop();
System.out.println("出栈的数" + val);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "show":
singleLinkedList.show();
break;
case "exit":
loop = false;
break;
default:
break;
}
}
}
}
class Node {
public int id;
public Node next;
public Node(int id) {
this.id = id;
}
}
class SingleLinkedList {
private Node head = new Node(0);
//将数据放到链表最后
public void push(Node node) {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
//将最后一个数据拿出
public int pop() {
if (head.next == null) {
throw new RuntimeException("栈空");
}
Node temp = head;
Node temp2 = head;
boolean flag = false;
int value = 0;
while (temp.next != null) {
temp = temp.next;
}
value = temp.id;
while (temp2.next.next != null) {
temp2 = temp2.next;
}
temp2.next = null;
return value;
}
//显示
public void show() {
if (head.next == null) {
System.out.println("栈空");
return;
}
Node temp = head.next;
while (temp != null) {
System.out.println("id=" + temp.id);
temp = temp.next;
}
}
}