关于栈的代码实现

it2025-04-03  8

用数组模拟栈

package Stack; import java.util.Scanner; public class ArrayStackDemo { public static void main(String[] args) { //测试栈的基本操作 //(1)先创建一个ArrayStack对象来表示栈 ArrayStack stack = new ArrayStack(4); String key = "";//写一个空串,因为一会要输入东西 boolean loop = true;//用来控制是否show // 退出菜单 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.printf("出栈的数据是%d\n",res); }catch (Exception e){ System.out.println(e.getMessage()); } break; case "exit": scanner.close(); loop = false; } } System.out.println("程序退出!!!"); } } //1.创建一个类ArrayStack表示栈 class ArrayStack{ private int maxSize; //栈的大小 private int[] stack;//数组模拟栈,数据就放在该数组 private int top = -1;//top表示栈顶,初始化为-1 //构造器 public ArrayStack(int maxSize){ this.maxSize = maxSize; //初始化数组 stack = new int[this.maxSize]; } //(1)判断栈满或栈空 public boolean isFull(){ return top == maxSize - 1; } public boolean isEmpty(){ return top == -1; } //(2)入栈 public void push(int value){ //先判断栈是否满 if (isFull()){ System.out.println("栈满!!!"); return; } //入栈操作 top++; stack[top] = value; } //(3)出栈 public int pop(){ //先判断是否为空 if (isEmpty()){ //由于这块有返回值,因此使用抛出异常处理 throw new RuntimeException("栈空,没有数据!!!"); } //出栈操作 int value = stack[top]; top--; return value; } //(4)显示栈的情况【遍历栈】,遍历时,需要从栈顶开始显示数据 public void list(){ if (isEmpty()){ System.out.println("占空,没有数据"); return; } for (int i = top;i > -1;i--){ System.out.printf("栈顶到栈底的数据依次是:stack[%d]=%d\n",i,stack[i]); } } }

用单链表模拟栈

package Stack; public class SingleLinkedStackDemo { public static void main(String[] args) { //测试栈的基本操作 SingleLinkedStack stack = new SingleLinkedStack(); stack.push(4); stack.push(5); stack.push(6); System.out.println("当前size:" + stack.getSize()); System.out.println(stack.pop()); System.out.println("当前size:" + stack.getSize()); System.out.println("当前出栈的是:"+ stack.pop()); System.out.println(stack.pop()); } } /** * 此栈实现基于链表,初始栈时未设置栈大小(栈大小可变) *此栈是将新元素插到链表头部(链表头部是栈顶) */ class SingleLinkedStack { private Node top;//记录栈顶元素 private int size = 0;//记录栈大小 public SingleLinkedStack() { top = null; } class Node{ public int data; //记录数据 public Node next; Node(int data){ this.data = data; } } //入栈 public void push(int data){ Node newNode = new Node(data); if (top == null) { top = newNode; } newNode.next = top;//将新元素插到链表头部 top = newNode; size++; } //出栈 public Integer pop(){ if(top == null) throw new RuntimeException("栈为空"); else{ int val = top.data; top = top.next; size--; return val; } } //查看栈顶元素但不出栈 public Integer peek(){ if(top == null) throw new RuntimeException("栈为空"); else{ return top.data; } } public boolean isEmpty(){ return top == null; } public int getSize(){ return size; } }
最新回复(0)