java 自己实现 队列 demo (顺序存储)

2017-07-06

38

0

技术:java8

运行环境:IDEA 15.2 + jdk8 + windows 10

demo功能:java自己实现的一个以顺序存储的队列代码demo

源代码

http://git.oschina.net/youlixishi/demo-world/blob/master/src/algorithm/binary-search/src/main/java/com/demoworld/JArrayQueue.java

 

public class JArrayQueue implements IQueue {
    public JArrayQueue(int maxLength) {
        this.maxLength = maxLength;
        arr = new int[this.maxLength];
        tailIndex = -1;
    }

    private int[] arr;
    private int tailIndex;//当前队列的尾巴索引, 这个位置是最后一个数据
    private int maxLength;

    @Override
    public void push(int n) {
        if (tailIndex + 1 >= arr.length) {
            System.out.println("warn: stack is full");
            return;
        }
        tailIndex++;
        arr[tailIndex] = n;
    }

    @Override
    public int pop() {
        int result = arr[0];

        int[] tmp = arr;
        arr = new int[tmp.length - 1];
        copyArryTo(tmp, 1, arr);
        tailIndex--;

        return result;
    }

    @Override
    public boolean isEmpty() {
        return tailIndex == -1;
    }

    //从from 数组中 fromIndex这个下标开始拷贝数据到to数组
    private void copyArryTo(int[] from, int fromIndex, int[] to) {
        if (from == null || to == null || from.length == 0 || to.length == 0) {
            return;
        }
        int maxIndex = from.length - 1;
        if (from.length > to.length) {
            maxIndex = to.length - 1;
        }
        for (int i = 0; i <= maxIndex; i++) {
            to[i] = from[i + fromIndex];
        }
    }

    private void copyArryTo(int[] from, int[] to) {
        if (from == null || to == null || from.length == 0 || to.length == 0) {
            return;
        }
        int maxIndex = from.length - 1;
        if (from.length > to.length) {
            maxIndex = to.length - 1;
        }
        for (int i = 0; i <= maxIndex; i++) {
            to[i] = from[i];
        }
    }
}

测试类

http://git.oschina.net/youlixishi/demo-world/blob/master/src/algorithm/binary-search/src/main/java/com/demoworld/JArrayQueueTest.java

public class JArrayQueueTest {
    //正常的 push pop
    @Test
    public void test1() {
        JArrayQueue j = new JArrayQueue(2);
        for (int i = 1; i <= 2; i++) {
            j.push(i);
        }

        while (!j.isEmpty()) {
            System.out.println(j.pop());
        }
    }

    //超过最长长度
    @Test
    public void test2() {
        JArrayQueue j = new JArrayQueue(2);
        for (int i = 1; i <= 10; i++) {
            j.push(i);
            if (i == 5) {
                System.out.println(j.pop());
            }
        }

        while (!j.isEmpty()) {
            System.out.println(j.pop());
        }
    }

    //push pop混合操作
    @Test
    public void test3() {
        JArrayQueue j = new JArrayQueue(10);
        for (int i = 1; i <= 10; i++) {
            j.push(i);
            if (i == 5) {
                System.out.println(j.pop());
                j.push(12);
            }
        }

        while (!j.isEmpty()) {
            System.out.println(j.pop());
        }
    }
}

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think