【数据结构与算法】使用数组及链表 实现队列

Java编码: 使用数组链表实现队列的数据结构

队列作为一种简单的数据结构,实现方式多种多样,即使只使用数组,不同的人的实现方式也不尽相同,关键在于对其结构的理解。
在这里我使用数组链表两种方式来实现队列。
队列的具体操作首先抽象在一个接口ListInterface中,接口中注明了各方法的参数及作用。使用数组和链表的具体实现都要首先继承ListInterface接口。

ListInterface接口编码(ListInterface.java):

package packetlist;

/** ADT线性表接口
 *  线性表位置从0开始
 */
public interface ListInterface<T> {

    /**
     * Task:向线性表的末尾插入新元素
     * @param newEntry 作为新元素插入的对象
     * @return 成功插入返回true,失败返回false
     */
    public boolean add(T newEntry);

    /**
     * Task:向线性表的指定位置插入新元素。
     *       原来位置的元素移至线性表下一个更高的位置。
     *       线性表长度加一
     * @param index 指定线性表元素的下标,index>=0
     * @param newEntry 作为新元素插入的对象
     * @return 成功插入返回true,失败返回false
     */
    public boolean add(int index,T newEntry);

    /**
     * Task:从线性表中删除指定下标的元素。原本位于比指定元素更高位置
     *      的元素移至线性表中上一位置。线性表超度减 1
     * @param index 指定线性表元素的下标
     * @return 删除成功返回index位置的元素,否则返回null
     */
    public void remove(int index);

    /**
     * Task:删除线性表所有的元素
     */
    public void clear();

    /**
     * Task:替换线性表指定位置的元素
     * @param index 所要替换的元素的位置;index>=0
     * @param newEntry 用以替换的元素的对象
     * @return 替换成功返回true,线性表为空或index非法返回false
     */
    public boolean replace(int index,T newEntry);

    /**
     * Task:检索线性表指定位置的元素
     * @param index 指定检索元素的索引
     * @return 找到指定元素,返回对他的引用,失败返回null
     */
    public void get(int index);

    /**
     * Task:确定线性表是否含有给定的元素
     * @param anEntry 待查的元素对象
     * @return 找到指定元素,返回true,失败返回false
     */
    public void contains(T anEntry);

    /**
     * Task:获得线性表的长度
     * @return 返回当前线性表中所含元素的个数
     */
    public int getLength();

    /**
     * Task:判断线性表是否为空
     * @return 为空返回true,否则返回false
     */
    public boolean isEmpty();

    /**
     * Task:判断线性表是否为满
     * @return 为满返回true,否则返回false
     */
    public boolean isFull();

    /**
     * Task:按照线性表的顺序显示线性表中所有的元素
     */
    public void display();

}//end ListInterface


使用数组编码实现(ArrayList.java):

package packetlist;

public class ArrayList<T> implements ListInterface<T>{
    private T[] array;                        //线性表元素数组
    private int length=0;                      //线性表元素当前个数
    private static final int MAX_SIZE=50;    //线性表最大长度

    public ArrayList(){
        array=(T[]) new Object[MAX_SIZE];    //初始化50个元素的数组
    }
//    public ArrayList(int maxSize){
//        entry=(T[]) new Object[maxSize];
//    }

    @Override
    public boolean add(T newEntry) {
        boolean isSuccessful=true;
        if(!isFull()){               //未满
            array[length]=newEntry;   //赋值
            length++;
        }
        else isSuccessful=false;
        return isSuccessful;
    }

    @Override
    public boolean add(int index, T newEntry) {
        boolean isSuccessful=true;
        if((index>=0)&&(index<length)&&length<MAX_SIZE){
            for(int tempIndex=length;tempIndex>=index+1;tempIndex--){  //为要插入的元素腾出空间,所有的元素移至下一位置
                array[tempIndex]=array[tempIndex-1];
            }
            array[index+1]=newEntry;
            length++;
        }
        else isSuccessful=false;

        return isSuccessful;
    }

    @Override
    public void remove(int index) {
        if((index>=0)&&(index<length)){
            assert !isEmpty();   //断言
            for(int temp=index+1;temp<length;temp++){   //后续元素向被删元素方向移动
                array[temp-1]=array[temp];
            }
            length--;
        }
    }

    @Override
    public void clear() {     //将length 置0.(线性表的方法在线性表为空时也可以正常的工作,但位于线性表的对象却没有被释放)
        length=0;
        System.out.println("This queue has been empty");
    }

    @Override
    public boolean replace(int index, T newEntry) {      //改变值
        boolean isSuccessful=true;
        if((index>=0)&&(index<length))
            array[index]=newEntry;
        else
            isSuccessful=false;
        return isSuccessful;
    }

    @Override
    public void get(int index) {
        T result=null;            //欲返回的结果
        if(index>=0&&index<length) {
            assert !isEmpty();
            result = array[index];
        }
        System.out.println("The element value with "+index+" is: "+result);
    }

    @Override
    public void contains(T anEntry) {
        boolean found=false;
        for (int index=0;index<length;index++){   //顺序查找
            if (anEntry.equals(array[index]))
                found=true;
        }
        if(found)
            System.out.println(anEntry+" Found!");
        else
            System.out.println(anEntry+" not Found.");
    }

    @Override
    public int getLength() {
        return length;
    }

    @Override
    public boolean isEmpty() {
        return length==0;
    }

    @Override
    public boolean isFull() {
        return length>=MAX_SIZE;
    }

    @Override
    public void display() {
        for(int index=0;index<length;index++)
            System.out.print(array[index]+" ");
        System.out.println();
    }
}


使用链表编码实现(LinkList.java):


主函数(Test.java):

主函数主要用于测试各方法的正确性,可按顺序进行测试。

package packetlist;

public class Test {
    public static void main(String[] args){
        ArrayList aList=new ArrayList();
        aList.add(12);
        aList.add(23);
        aList.display();
        aList.add(78);
        aList.display();

        aList.add(0,45);
        aList.display();
        aList.add(3,89);
        aList.add(4,10);
        aList.display();
        aList.get(4);

        aList.remove(3);
        aList.display();

//        aList.clear();     //测试clear()
//        aList.display();
//        aList.add(1);
//        aList.display();

        aList.replace(0,0);
        aList.display();

        aList.get(3);
        aList.contains(10);
        aList.contains(404);
        System.out.println("Length: "+aList.getLength());


//        LinkList linkList=new LinkList();
//        linkList.add(12);
//        linkList.add(24);
//        linkList.display();

    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值