先进后出 FILO(First-Input-Last-Out)的有序列表,限制线性表中元素的插入和删除只能在线性表的同一端进行
package com.b0.stack;
import java.util.Scanner;
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
String key = "";
boolean loop = true;//控制是否退出菜单
Scanner scanner = new Scanner(System.in);
while (loop){
System.out.println("show:表示显示栈");
System.out.println("exit:退出程序");
System.out.println("push:入栈");
System.out.println("pop:出栈");
System.out.println("请输入你的选择");
key = scanner.next();
switch (key){
case "show":
stack.list();
break;
case "push":
System.out.println("请输入一个数");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {
int res = stack.pop();
System.out.println("出栈数据是:"+res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case "exit":
scanner.close();
loop = false;
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;
}
//入栈
public void push(int num){
//判断是否栈满
if (isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = num;
}
//出栈
public int pop(){
//判断是否栈空
if (isEmpty()){
//抛出异常
throw new RuntimeException("栈空,没有数据!");
}
int value = stack[top];
top--;
return value;
}
//遍历栈
public void list(){
if (isEmpty()){
System.out.println("栈空!");
return;
}
while (top != -1){
//从栈顶开始遍历
System.out.println(stack[top]);
top--;
}
}
}
package com.b0.stack;
public class LinkedStackDemo {
public static void main(String[] args) {
LinkedStack linkedStack = new LinkedStack();
linkedStack.push(1);
linkedStack.push(2);
linkedStack.push(3);
linkedStack.push(4);
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
}
}
class LinkedStack{
//头节点
Node head = new Node(0);
//栈空,尾插法
public boolean isEmpty(){
return head.next == null;
}
//入栈,头插法
public void push(int num){
Node node = new Node(num);
if (isEmpty()){
head.next = node;
}
node.next = head.next;
head.next = node;
}
public int pop(){
if (isEmpty()){
//抛出异常
throw new RuntimeException("栈空,没有数据!");
}
//定义零时变量,不修改head指针指向
Node cur = head;
int value = cur.next.no;
cur.next = cur.next.next;
return value;
}
}
class Node{
public int no;
public Node next;
public Node(int no) {
this.no = no;
}
}
package com.b0.stack;
public class LinkedStackDemo {
public static void main(String[] args) {
LinkedStack linkedStack = new LinkedStack();
linkedStack.push(1);
linkedStack.push(2);
linkedStack.push(3);
linkedStack.push(4);
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
System.out.println("出栈值为:"+linkedStack.pop());
}
}
class LinkedStack{
//头节点
Node head = new Node(0);
//栈空
public boolean isEmpty(){
return head.next == null;
}
//入栈
public void push(int num){
Node node = new Node(num);
Node cur = head;
while (true){
if (cur.next == null)break;
cur = cur.next;
}
cur.next = node;
}
//出栈
public int pop(){
if (isEmpty()){
//抛出异常
throw new RuntimeException("栈空,没有数据!");
}
//定义零时变量,不修改head指针指向
Node cur = head;
int value;
while (true){
if (cur.next.next == null){
value = cur.next.no;
cur.next = null;
break;
}
cur = cur.next;
}
return value;
}
}
class Node{
public int no;
public Node next;
public Node(int no) {
this.no = no;
}
}