数据结构和算法——基于Java——4.1栈(数组实现栈、链表实现栈)-创新互联

理论补充

在这里插入图片描述

公司主营业务:成都做网站、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出兴县免费做网站回馈大家。

先进后出 FILO(First-Input-Last-Out)的有序列表,限制线性表中元素的插入和删除只能在线性表的同一端进行

  • 栈顶:变化的一端
  • 栈底:固定的一端
代码实现 2.1 数组模拟栈
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--;
        }
    }
}
2.2 链表模拟栈(头插法)

在这里插入图片描述

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;
    }
}
2.3 链表模拟栈(尾插法,不推荐)
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;
    }
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文题目:数据结构和算法——基于Java——4.1栈(数组实现栈、链表实现栈)-创新互联
网页地址:http://ybzwz.com/article/digpse.html