现在的位置: 首页 > 综合 > 正文

java 用动态数组实现线性表

2013年09月04日 ⁄ 综合 ⁄ 共 2994字 ⁄ 字号 评论关闭

 package com.jzm;
/**
 * @author jzm
 * @param <T>
 */
class  Alist<T>{
 private T[] a;                         //用于存放值的数组
 private int  length;                   //线性表元素中当前个数
 private final static int MAX = 2;      //默认线性表大小
 
    private void  makeRoom (int  newPosition){ 
     for (int i = length; i>= newPosition; i--) {
         a[i] = a[i-1];
   }     
   }
  
   
    private void  doubleArray(){
       System.out.println("开始扩展链表:");
       T[]  oldlist = a;
       int  oldsize  =  oldlist.length;
       a =  (T[])new  Object[2*oldsize];
       for (int i = 0; i < oldsize; i++) {
    a[i] = oldlist[i];
    }
       System.out.println("扩链表长度为:"+a.length);
       System.out.println("结束扩展链表:");
    }
   
    public Alist(){ 
   length = 0;
   a = (T[]) new Object [MAX];  
  } 
      
 public Alist(int size){          //自定义线性表大小
    length = 0;
    a = (T[])new Object[size];  
 }
 
 public int getLength(){          //得到当前线性表元素的总个数
  return length;
 }
  
 public void clear(){              //清空线性表
  length = 0;
  a = null;
 }
 
 public boolean replace (int givenPosition ,T t){   //替代线性表中的第givenPosition个元素
  
     if(givenPosition > length || givenPosition < 1)
           return false;
     else { a[givenPosition-1] = t; return true;}
 }
 
 public boolean isFull(){          //判断线性表是否满了
     if(length >= MAX){
      return true;
     }else {
   return  false;
  }  
 }
 
 public boolean isArrayFull(){        //判断线性表是否满了
     if(length >=  a.length){
      return true;
     }else {
   return  false;
  }  
 }
 
 
 public boolean isEmpty(){       //判断线性表是否为空
   if(length <= 0){
    return true;
   }else {
    return false;
   }  
 }
   
    public boolean add(T  x){           //向线性表加入一个元素
         if(isArrayFull())           //当数组不够用时才扩展为原来的两倍
             doubleArray();      
             a[length++] = x;
       return true;    
    }
      
    public boolean add(int newPosition ,T x){   //加入一个元素  放在第几个位置  
     if(!isFull()  &&  newPosition >=1  &&  newPosition <= length+1){
       makeRoom(newPosition);
       a[newPosition-1] = x;
       length++;
       return true;
     }else {
     System.out.println("插入值出现错误");
     return false;   
  }    
   }    
     
   public boolean add(int newPosition ,T x,boolean b){
  /**加入一个元素放在第几个位置        当数组长度不够用时动态扩展*/   
    if (isArrayFull()){
     doubleArray();
     }     
   
    if(!isArrayFull()  &&  newPosition >=1  &&  newPosition <= length+1){
       makeRoom(newPosition);
       a[newPosition-1] = x;
       length++;
       return true;
       }
       return true;
   }    
   
   
 
   public void display(){                      //打印出当前线性链表的值
     if(isEmpty())
     System.out.println("线性链表当前值为空:");
    else{  
      System.out.println("线性链表当前值为:");
      for (int i = 0; i < length; i++) {
   System.out.println(a[i]+" ");   
  }       
    }
   }
  
   public T remove(int givenPosition){         //删除第givenPosition个值       
          T t = null;
          if(givenPosition >=1 && givenPosition <= length)
                assert !isEmpty();
          t = a[givenPosition-1];
        
          for (int i = givenPosition; i < length;  i++) {
      a[i-1] = a[i];
    }
          a[length-1] = null;
          length--;
          return  t;         
     }
  
     public boolean contains(T x){          //判断线性表是否包含元素X
        boolean found = false;
        for (int i = 0; i < length; i++) {
      if (a[i].equals(x)) {
      found = true;
      break;
      }
      }     
         return  found;
     }
    
}

抱歉!评论已关闭.