zoukankan      html  css  js  c++  java
  • 单向链表JAVA代码

     
     
    1. //单向链表类
    2. publicclassLinkList{
    3.  
    4.     //结点类
    5.     publicclassNode{
    6.         publicObject data;
    7.         publicNode next;
    8.  
    9.         publicNode(Object obj,Node next){
    10.             this.data = obj;
    11.             this.next = next;
    12.         }
    13.     }
    14.  
    15.     Node head;          //记录头结点信息即可(头结点下标为-1)
    16.     int size;
    17.  
    18.     publicLinkList()
    19.     {
    20.         this.head =newNode(null, null);
    21.         this.size =0;
    22.     }
    23.  
    24.     //定位
    25.     publicNode locate(int index) throws Exception
    26.     {
    27.         //容错性
    28.         if(index <-1|| index > size)
    29.             thrownewException("参数错误!");
    30.  
    31.         //定位到temp指向第index个(index为下标,从0开始)
    32.         Node temp = head;
    33.         for(int i =-1; i < index; i++)
    34.             if(temp != null)
    35.                 temp = temp.next;
    36.  
    37.         return  temp;
    38.     }
    39.  
    40.  
    41.     publicvoiddelete(int index) throws Exception
    42.     {
    43.         //容错性
    44.         if(isEmpty())
    45.             thrownewException("链表为空,无法删除!");
    46.         if(index <0|| index > size -1)
    47.             thrownewException("参数错误!");
    48.  
    49.         Node temp = locate(index -1);                        //定位到要操作结点的前一个结点对象
    50.         temp.next = temp.next.next;
    51.         size--;
    52.     }
    53.  
    54.  
    55.     publicvoid insert(int index,Object obj) throws Exception
    56.     {
    57.         //容错性
    58.         if(index <0|| index > size )
    59.             thrownewException("参数错误!");
    60.  
    61.         Node temp = locate(index -1);                        //定位到要操作结点的前一个结点对象
    62.         Node p =newNode(obj,temp.next);
    63.         temp.next = p;
    64.         size++;
    65.     }
    66.  
    67.     public boolean isEmpty(){
    68.         return size==0;
    69.     }
    70.  
    71.     publicint size(){
    72.         returnthis.size;
    73.     }
    74.  
    75. }
     
     
    1. publicclassTest{
    2.  
    3.     publicstaticvoid main(String[] args) throws Exception{
    4.         LinkListlist=newLinkList();
    5.         for(int i =0; i <10; i++){
    6.             int temp =((int)(Math.random()*100))%100;
    7.             list.insert(i, temp);
    8.             System.out.print(temp +" ");
    9.         }
    10.  
    11.         list.delete(4);
    12.         System.out.println(" "+"after deleting the 5th number:");
    13.         for(int i =0; i <list.size; i++){
    14.             System.out.print(list.locate(i).data.toString()+" ");
    15.         }
    16.     }
    17.  
    18. }
     
    输出:
    1. 29263748496266877839
    2. after deleting the 5th number:
    3. 292637486266877839 
     
     
     
     





  • 相关阅读:
    SAP 移动类型 整理
    VB6及VS2005 相关的 树TREE控件,网格控件、电子表格控件、网络图及甘持图控件(项目进度)
    金蝶 PK 用友,第三方评论与自我评价(1)
    谁在开发“工作流”WORKFLOW 产品?
    协同及ERP开发平台,我们如何选择?
    关注“北京广联达软件公司”的项目成本管理系统 !
    一个免费提供的开发平台___"KCOM 商业工程"
    企业 ISO“质量、安全和环境” 三大体系认证的管理系统的开发者 !
    MAXWELL 万胜系统软件公司——为工程建设承包商提供优秀的软件套件!
    Contractor Anywhere (任何地方的承包商)也被 SAGE “赛捷”公司收购 !
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5533090.html
Copyright © 2011-2022 走看看