遍历线性表总结

java 代码
  1. 我总结了一下显示数组的方法:   
  2. 迭代方法:   
  3. public static void display(int arrays [] ,int first ,int last){   
  4.   
  5.     while(first<=last){   
  6.         System.out.println(arrays[first++];   
  7.     }   
  8. }   
  9.   
  10.   
  11. 下面是递归方法:   
  12. public static void display(int arrays [] ,int first ,int last){   
  13.   
  14.     System.out.println(arrays[first]);   
  15.     if(first<last){   
  16.        
  17.          display(arrays,first+1,last);   
  18.     }   
  19. }   
  20.   
  21.   
  22. public static void display(int arrays[] ,int first,int last){   
  23.   
  24.     if(first<=last){   
  25.        
  26.         display(arrays,first,last);   
  27.         System.out.println(arrays[last]);   
  28.            
  29.     }   
  30. }   
  31.   
  32. public static void display(int arrays [] ,int first ,int last){   
  33.   
  34.     if(first==last){   
  35.         System.out.println(arrays[first]);   
  36.     }else {   
  37.            
  38.         int mid = (first+last)/2;   
  39.         display(arrays,first,mid);   
  40.         display(arrays,mid+1,last);    
  41.     }   
  42.        
  43. }   
  44.   
  45. public static void display(int arrays [] ,int first ,int last){   
  46.   
  47.     if(first==last){   
  48.         System.out.println(arrays[first]);   
  49.     }else {   
  50.            
  51.         int mid = (first+last)/2;   
  52.         display(arrays,first,mid-1);   
  53.         System.out.println(arrays[mid]);   
  54.         display(arrays,mid+1,last);    
  55.     }   
  56.        
  57. }   
  58.   

 

java 代码
  1. 下面是如何遍历一个链表:   
  2.   
  3. public static void  displayChain(Node  nodeone){   
  4.   
  5.      if(nodeone!=null){   
  6.             
  7.           System.out.println(nodeone.data+" ");   
  8.           displayChain(nodeone.next);     
  9.      }   
  10. }   
  11.   
  12.   
  13. 如果想以相反的顺序输出,如果用迭代只遍历N次,所以麻烦。然而可以用递归:   
  14. public static void displayBackChain(node nodeone){
  15.    if(nodeone!=null){
  16.     displayBackChain(nodeone.next);
  17.     System.out.println(nodeone.data);
  18.     }
  19. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值