基于java的数据结构学习手记1

 一.高级接口数组类的设计与实现:

   a.数组类的设计

Code:
  1. //highArray.java  
  2. //the definition of Array class with high-level interface  
  3. /  
  4. public class highArray {  
  5.     private long[]a;//ref to array a  
  6.     private int nElems;//number of array data items  
  7.     //---------------------------------------------------  
  8.     public highArray(int nax)//construtor  
  9.     {  
  10.         a=new long[nax];//creat the array,declaration  
  11.         nElems=0;//there is no items yet  
  12.     }  
  13.     //...................................................  
  14.     public boolean find(long searchKey)//find the specified value  
  15.     {  
  16.         int j;  
  17.         for(j=0;j<nElems;j++)  
  18.         {  
  19.             if(a[j]==searchKey)  
  20.                 break;//found it and jump out of the loop  
  21.         }  
  22.         if (j==nElems)return false;//not found  
  23.         else return true;  
  24.     }  
  25.     //...................................................  
  26.     public void insert(long value)//put element into array  
  27.     {  
  28.         a[nElems]=value;//put the value into the array  
  29.         nElems++;//add the total number of the data items     
  30.     }  
  31.     public boolean delete(long value)//delete an array item  
  32.     {  
  33.         int j,k;  
  34.         for(j=0;j<nElems;j++)//search the item  
  35.         {  
  36.             if(a[j]==value)  
  37.                 break;  
  38.         }  
  39.         if(j==nElems)return false;//not found  
  40.         else   
  41.             {  
  42.                 for(k=j;k<nElems;k++)//put every item behind the target one ahead  
  43.                 {  
  44.                     a[k]=a[k+1];  
  45.                 }  
  46.                 nElems--;     
  47.                 return true;  
  48.             }  
  49.     }  
  50.     public void display()  
  51.     {  
  52.         for(int j=0;j<nElems;j++)  
  53.         {  
  54.             System.out.println(a[j]+" ");  
  55.               
  56.         }  
  57.     //....................................................................    
  58.     }  
  59.       
  60. }  

b.主函数类

Code:
  1. public class HighArrayApp {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int maxSize=100;  
  9.         highArray arr;  
  10.         arr=new highArray(maxSize);//初始化  
  11.        arr.insert(77);  
  12.        arr.insert(99);  
  13.        arr.insert(44);  
  14.          
  15.        arr.insert(55);  
  16.          
  17.        arr.insert(22);  
  18.        arr.insert(16);  
  19.        arr.insert(33);  
  20.          
  21.        arr.insert(56);  
  22.        arr.insert(33);  
  23.        arr.display();   //display items  
  24.          
  25.        int searchKey=44;  
  26.        if(arr.find(searchKey))  
  27.            System.out.println("Found"+searchKey);  
  28.        else  
  29.            System.out.println("Can't find"+searchKey);  
  30.        arr.delete(22);  
  31.        arr.delete(56);  
  32.        arr.delete(55);  
  33.        arr.display();  
  34.     }  
  35.       
  36.   
  37. }  

c.输出结果

Code:
  1. 77   
  2. 99   
  3. 44   
  4. 55   
  5. 22   
  6. 16   
  7. 33   
  8. 56   
  9. 33   
  10. Found44  
  11. 77   
  12. 99   
  13. 44   
  14. 16   
  15. 33   
  16. 33   

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值