Rhyme/Java自定义队列

来源:互联网 发布:淘宝静物相机 编辑:程序博客网 时间:2024/06/05 20:42

Java自定义队列

package com.maple.myqueue;import java.util.Arrays;/** * the implements of my queue * @author RhymeChiang * @date 2017/12/04 **/public class MyQueue {    /**     * the size of currrnt     */    private int size;    private int capacity=3;    /**     * the container     */    private int data[]=new int[capacity];    /**     * the first point     */    private int first;    /**     * the end point     */    private int end;    /**     * append a data into my queue     * @param a     */    public void append(int a){        data[size]=a;        size++;        // expand the capacity        if(size==data.length-1){            capacity=capacity*2;            int newData[] = new int[capacity];            for(int i = 0;i<size;i++){                newData[i]=data[i];            }            data = newData;        }    }    /**     * pop up the top element of this queue     * @return     */    public int popUp(){        int top = data[0];        for(int i=0;i<size;i++){            data[i]=data[i+1];        }        size--;        return top;    }    public int[] getData() {        return data;    }    public void setData(int[] data) {        this.data = data;    }    public int getSize() {        return size;    }    public void setSize(int size) {        this.size = size;    }    public static void main(String[] args) {        MyQueue queue = new MyQueue();        queue.append(1);        queue.append(2);        queue.append(3);        queue.append(4);        queue.append(5);        System.out.println("pop ele: "+queue.popUp());        System.out.print("the elements in the queue:");        for(int i=0;i<queue.getSize();i++){            System.out.print(queue.getData()[i]+" ");        }    }}

测试结果

这里写图片描述

原创粉丝点击