用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();

 }

 

 

 

 

 

}

以上是 用java数组实现基本链表和可自扩充的链表 的全部内容, 来源链接: utcz.com/389873.html

回到顶部