栈和队列的基本原理

栈是先进后出
入栈:

public class zhan {
    private int[] data = new int[5];

    private int size = 0;
//    size数组的长度(data的最后一个元素)
    public void push(int x){//入栈
        data[size++] = x;
        if(size == data.length){
            data = Arrays.copyOf(data,size*2);//数组的扩容
        }
    }

出栈:

public int pop(){//出栈
        if(size == 0){
            System.out.println("栈内为空");
            return 0;
        }
        int result = data[size-1];
        size--;
        return result;
    }

完整的栈代码实现

import java.util.Arrays;

public class zhan {
    private int[] data = new int[5];

    private int size = 0;
//    size数组的长度(data的最后一个元素)
    public void push(int x){//入栈
        data[size++] = x;
        if(size == data.length){
            data = Arrays.copyOf(data,size*2);//数组的扩容
        }
    }

    public int pop(){//出栈
        if(size == 0){
            System.out.println("栈内为空");
            return 0;
        }
        int result = data[size-1];
        size--;
        return result;
    }
}

测试类:

public class zhanTest {
    public static void main(String[] args) {
        zhan z = new zhan();
        z.push(1);
        z.push(2);
        z.push(3);
        z.push(4);
        z.push(5);
        z.push(6);
        z.push(7);
        System.out.println(z.pop());
        System.out.println(z.pop());
        System.out.println(z.pop());
        System.out.println(z.pop());
        System.out.println(z.pop());
        System.out.println(z.pop());
        System.out.println(z.pop());
        System.out.println(z.pop());
    }

}

按照 1 2 3 4 5 6 7的顺序的入栈,出栈的顺序是7 6 5 4 3 2 1

队列

队列是先进先出
入队列:

public void push(int x){
        arr[size++] = x;
        if (size>=arr.length){
            arr = Arrays.copyOf(arr,size*2);
        }
    }

出队列:

public int pop(){
        if (size == count){
            System.out.println("队列为空");
        }
        int result = arr[count];
        count++;
        return result;
    }

完整的队列代码实现

import java.util.Arrays;

public class duilie {
    private int[] arr = new int[5];
    private int size = 0;
    public void push(int x){
        arr[size++] = x;
        if (size>=arr.length){
            arr = Arrays.copyOf(arr,size*2);
        }
    }
    private int count = 0;
    public int pop(){
        if (size == count){
            System.out.println("队列为空");
        }
        int result = arr[count];
        count++;
        return result;
    }
}

测试类:

public class duilieTest {
    public static void main(String[] args) {
        duilie d = new duilie();
        d.push(1);
        d.push(2);
        d.push(3);
        d.push(4);
        d.push(5);
        d.push(6);
        d.push(7);
        System.out.println(d.pop());
        System.out.println(d.pop());
        System.out.println(d.pop());
        System.out.println(d.pop());
        System.out.println(d.pop());
        System.out.println(d.pop());
        System.out.println(d.pop());
        System.out.println(d.pop());
    }
}

按照 1 2 3 4 5 6 7的顺序的入队列,出队列的顺序是1 2 3 4 5 6 7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值