zoukankan      html  css  js  c++  java
  • 用java数组实现基本链表和可自扩充的链表

    package com.tongji.szx.base;

    import com.tongji.szx.baseDao.ListInterface;

    public class ExtendList<T> implements ListInterface<T>{
     
     private T[] entry;
     private int length;
     private int capacity;
     private static final int BASE_LENGTH=10;
     
     /**
      * 默认的构造函数
      */
     public ExtendList(){
      this(BASE_LENGTH);
     }
     @SuppressWarnings("unchecked")
     /**
      * 提供一个初始链表长度的构造函数
      */
     public ExtendList(int intLength){
      if(intLength>0){
       this.length=intLength;
       this.capacity=intLength;
       entry=(T[])new Object[intLength];
      }else{
       this.length=BASE_LENGTH;
       this.capacity=BASE_LENGTH;
       entry=(T[])new Object[BASE_LENGTH];
      }
     }
     @Override
     /**
      * 在链表的末尾插入元素
      */
     public boolean add(T anEntry) {
      // TODO Auto-generated method stub
      try{
       if(length==capacity){
        copyEntry();
       }
       entry[length]=anEntry;
       length++;
       return true;
      }catch(Exception e){
       return false;
      }
     }
     
     @SuppressWarnings("unchecked")
     private void copyEntry(){
      T[] newEntry=entry;
      this.capacity*=2;
      entry=(T[])new Object[this.capacity];
      for(int index=0;index<newEntry.length;++index){
       entry[index]=newEntry[index];
      }
     }
     @Override
     public boolean add(int index, T anEntry) {
      // TODO Auto-generated method stub
      try{
       while(index>=capacity){
        copyEntry();
       }
       if(index<length){
        moveEntry(index,anEntry);
        length++;
        return true;
       }else{
        entry[index]=anEntry;
        //length+=(index);
        int sub=index-length+1;
        length+=sub;
        return true;
       }
      }catch(Exception e){
       return false;
      }
      //return false;
     }
     
     /**
      * 当插入位置在length之前时移动元素
      * @param index
      * @param anEntry
      */
     private boolean moveEntry(int aPosition,T anEntry){
      if(aPosition<length){
       for(int index=length-1;index>=aPosition;++index){
        entry[index+1]=entry[index];
       }
       entry[aPosition]=anEntry;
       return true;
      }
      return false;
     }

     @Override
     public boolean contains(T anEntry) {
      // TODO Auto-generated method stub
      for(T t:entry){
       if(t.equals(anEntry)){
        return true;
       }
      }
      return false;
     }

     @Override
     public T get(int index) {
      // TODO Auto-generated method stub
      if(index<length){
       return entry[index];
      }
      return null;
     }

     @Override
     public boolean isEmpty() {
      // TODO Auto-generated method stub
      return (length==0);
     }

     @Override
     public boolean isFull() {
      // TODO Auto-generated method stub
      return (length==capacity);
      //return false;
     }

     @Override
     public T remove(int index) {
      // TODO Auto-generated method stub
      if(index<length){
       T newEntry=entry[index];
       for(int i=index;i<length;++i){
        entry[i]=entry[i+1];
       }
       length--;
       return newEntry;
      }
      return null;
     }

     @Override
     public T replace(int index, T anEntry) {
      // TODO Auto-generated method stub
      if(index<length){
       T newEntry=entry[index];
       entry[index]=anEntry;
       return newEntry;
      }
      return null;
     }
     @Override
     public int getLength(){
      return length;
     }
     public int getCapacity(){
      return this.capacity;
     }
     public void Display(){
      for(int index=0;index<length;++index){
       if(entry[index]!=null){
        System.out.println(index+":"+entry[index].toString());
       }else{
        System.out.println(index+":"+"对象为空!");
       }
      }
     }
     
     public static void main(String[] args){
      System.out.println("====================");
      ExtendList<String> elst=new ExtendList<String>(5);
      elst.add("sunzhenxing");
      elst.add(10, "sunhailong");
      
      elst.Display();
      System.out.println(elst.getLength());
      System.out.println(elst.getCapacity());
      elst.remove(10);
      System.out.println(elst.getLength());
      elst.Display();
     }
     
     
     
     
     
    }

  • 相关阅读:
    uCOS-II 学习笔记之任务管理--------任务控制块OS_TCB
    uCOS-II 学习笔记之事件管理--------信号量管理的学习
    格子计划
    phpcms二次开发之base.php的桥梁作用
    [leedcode 215] Kth Largest Element in an Array
    [leedcode 214] Shortest Palindrome
    [leedcode 213] House Robber II
    [leedcode 212] Word Search II
    [leedcode 211] Add and Search Word
    [leedcode 210] Course Schedule II
  • 原文地址:https://www.cnblogs.com/sunzhenxing19860608/p/1580721.html
Copyright © 2011-2022 走看看