栈的两种实现

数据结构---栈

  • 使用数组模拟栈的思路分析
    1. 定义一个top来表示栈顶,初始值为 top=-1;
    2. 入栈的操作---->top++; stack[top]=data;
    3. 出栈的操作---->int value=stack[top]; top--; return value;
    4. 遍历栈的操作---->stack[i]

代码实现

//定义一个ArrayStack表示栈
class ArrayStack{
    private int maxSize;
    private int[] stack;
    private int top=-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 value){
        if (isFull()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top]=value;
    }
    //出栈
    public int pop(){
        if (isEmpty()){
            throw  new RuntimeException("栈空");
        }
        int value=stack[top];
        top--;
        return value;
    }
    //显示栈,遍历时从栈顶开始显示数据
    public void showStack(){
        if (isEmpty()){
            System.out.println("栈空,无数据");
            return;
        }
        for (int i= top;i>-1;i--){
            System.out.printf("stack[%d]:%d\n",i,stack[i]);
        }
    }
}

测试数组模拟栈

/**
 * @ author Guo daXia
 * @ create 2022/11/17
 */
public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(3);
        String key="";
        boolean loop = true;
        Scanner sc = 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= sc.next();
            switch (key){
                case "show":
                    stack.showStack();
                    break;
                case "exit":
                    sc.close();
                    loop=false;
                    break;
                case "push":
                    System.out.print("请输入要入栈的值:");
                    int value = sc.nextInt();
                    stack.push(value);
                    System.out.printf("%d入栈成功!",value);
                    break;
                case "pop":
                    try {
                        int res = stack.pop();
                        System.out.printf("出栈的数据是%d \n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                default:
                    break;
            }
        }
        System.out.println("程序退出啦!");
    }
}
  • 使用带头节点单链表模拟栈的思路分析
    1. 定义一个top头节点来表示栈顶,初始值为 top=-1;
    2. 判断栈空---->top.next==null;
    3. 链表没有栈满的情况
    4. 入栈的操作,采用头插法----->
      • 原始单链表入栈表示:newNode.next=top.next; top.next=nexNode;
      • 实际是:newNode.setNext(top.getNext());top.setNext(newNode);
      • 因为我们已经把节点类的属性都私有了。
      • 题外知识:
        • node.setXxx(newNode)方法表示将node节点连接到newNode节点
        • node=node.getXxx()方法表示将node节点移动到下一个位置
    5. 出栈的操作----->top = top.getNext();
    6. 遍历栈的操作---->Node temp=top; temp = temp.getNext();

代码实现

创建节点类

class Node {
    private int value;
    private Node next;

    public Node(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

创建栈

class SingleLinkedListStack {
    private Node top = new Node(-1);//头指针代表栈顶

    //判断栈空
    public boolean isEmpty() {
        return top.getNext() == null;
    }

    //入栈,采用头插法
    public void push(Node newNode) {
        //第一个节点的插入,与其他新节点的插入不同
        if (top.getNext() == null) {
            top.setNext(newNode);
            return;
        }
        //用一个辅助变量将它指向top节点的下一个节点
        Node temp = top;
        newNode.setNext(top.getNext());//将新节点连到旧节点上
        top.setNext(newNode);//将top节点连到n新节点上
    }

    //出栈
    public void pop() {
        if (isEmpty()) {
            System.out.println("栈空,不能出栈!");
            return;
        }
        top = top.getNext();
        System.out.printf("出栈的数据是:%d \n", top.getValue());
    }

    //展示栈,即遍历栈
    public void show() {
        if (isEmpty()) {
            System.out.println("栈空!");
            return;
        }
        Node temp=top;
        while (temp.getNext() != null) {
            temp = temp.getNext();
            System.out.printf("数据:%d \n", temp.getValue());
        }
    }
}

测试单链表模拟栈

/**
 * @ author Guo daXia
 * @ create 2022/11/17
 */
public class LinkedListStackDemo {
    public static void main(String[] args) {
        SingleLinkedListStack stack = new SingleLinkedListStack();
        String key = "";
        boolean loop = true;
        Scanner sc = new Scanner(System.in);

        while (loop) {
            System.out.println("show:表示显示栈");
            System.out.println("push:表示添加数据到栈(入栈)");
            System.out.println("pop:表示从栈中取出数据(出栈)");
            System.out.println("exit:退出程序");
            System.out.println("请输入您的选择");
            key = sc.next();
            switch (key) {
                case "show":
                    stack.show();
                    break;
                case "exit":
                    sc.close();
                    loop = false;
                    break;
                case "push":
                    System.out.print("请输入要入栈的值:");
                    int value = sc.nextInt();
                    stack.push(new Node(value));
                    System.out.printf("数据%d入栈成功!", value);
                    System.out.println();
                    break;
                case "pop":
                    stack.pop();
                    break;
                default:
                    System.out.println("请按照格,对栈操作");
                    break;
            }
        }
        System.out.println("程序退出啦!");
    }
}
posted @ 2022-11-17 22:28  郭培鑫同学  阅读(28)  评论(0编辑  收藏  举报