数据结构-分别用数组和单链表模拟栈

本文对比了数组和链表作为数据结构实现栈的特点,详细介绍了使用Java编程语言通过数组和链表两种方式来模拟栈的具体实现过程。数组实现的栈具有快速的压栈和出栈操作,但受限于固定大小;链表实现的栈则可以动态调整大小,但压栈和出栈速度相对较慢。

两者的优缺点:
数组
优点:压栈出栈应该比链表的快
缺点:开始时就限制了栈的最大容量

链表
优点:只要内存够,理论上容量无限大
缺点:压栈出栈应该比数组的慢

两种模拟栈的方式

用数组模拟栈

package.用数组模拟栈;

import java.util.Scanner;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(3);

        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.print("请输入一个数:");
                    stack.push(scanner.nextInt());
                    break;
                case "pop":
                    try{
                        System.out.println("出栈的数据为"+stack.pop());
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }



    }
}

class ArrayStack{
    private int maxSize;
    private int top = -1;
    private int[] stack;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[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;
        }
        stack[++top] = value;
    }

    //弹栈
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈已空,无法出栈");
        }
        return stack[top--];
    }

    //遍历栈
    public void list(){
        if (isEmpty()){
            System.out.println("栈为空");
            return;
        }
        int temp = top;
        while (temp != -1){
            System.out.println("stack["+temp+"]="+stack[temp]);
            temp--;
        }
    }

}


用链表模拟栈

package.用链表模拟栈;

public class LinkedStackDemo {
    public static void main(String[] args) {
        LinkedStack linkedStack = new LinkedStack();

        linkedStack.push(new Node(1));
        linkedStack.push(new Node(2));
        linkedStack.push(new Node(3));
        linkedStack.push(new Node(4));
        linkedStack.pop();

        linkedStack.list();
    }
}

class LinkedStack{
    Node head = new Node(-1);

    //判断栈是否为空
    public boolean isEmpty(){
        return head.next == null;
    }

    //加节点的时候倒着加
    public void push(Node newNode){
        if (isEmpty()){
            head.next = newNode;
            return;
        }
        newNode.next = head.next;
        head.next = newNode;
    }

    //出栈
    public Node pop(){
        if (isEmpty()){
            throw new RuntimeException("栈已空。无法出栈");
        }
        Node temp = head.next;
        head.next = head.next.next;
        return temp;
    }

    //遍历
    public void list(){
        if (isEmpty()){
            System.out.println("栈为空");
            return;
        }
        Node temp = head.next;
        while (temp != null){
            System.out.println(temp);
            temp = temp.next;
        }

    }

}
class Node{
    int value;
    Node next;

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

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值