LinkList(JAVA版,contain rear)

來源:互聯網
上載者:User

標籤:out   print   lis   null   for   轉換   near   empty   ddl   

//含有rear,尾插時時O(1)的複雜度
package linearList;
//凡是實現後插後刪都比較容易,盡量向著這個方向轉換
public  class linearList {
      class node {
     private  int data;
     private  node next;
     public node(int data,node next){
      this.data=data;
      this.next=next;}
     }
    private node head;
    private node rear;//兩個引用,指向空
    private  int length=0;
    public linearList(){
      head=new node(0,null);
      rear=head;
     }
    void addfirst(int data)//頭插
    {if(head.next==null){
      node n=new node(data,null);
      head.next=n; rear=n;length++;}
     else {node n=new node(data,head.next);head.next=n;length++;}}
    void addlast(int data)//尾插
    {node n=new node(data,null);
     rear.next=n;
     rear=n;length++;}
    void addafter(node n,int data)//後插
    {node n1=n.next;
    if(n1==null){
     node n2=new node(data,null);
    n.next=n2;rear=n2;length++;
    }
    else{
     node n2=new node(data,n.next);
     n.next=n2;length++;
    }
    } 
    void addbefore(node n,int data)//前插
    {addafter(n,n.data);
    n.data=data;//沒有length++;
    }
    int removefirst()//頭刪
    {if(head.next ==null){return 0;}
    if(head.next==rear){
    int data=rear.data;
    head.next=null;
    rear=head;length--; return data;}
    int data=head.next.data;
    head.next=head.next.next;length--; 
    return data;
    }
    int removelast()//尾刪
    {node n=head;int data=rear.data;
     while(n!=null){
      if(n.next.equals(rear))
      {n.next=null;rear=n;}
      n=n.next;
     }length--; 
     return data;
    }
   
    int removeafter(node n)//後刪
    {if(n.next==null)return 0;
     node n1=n.next;int data=n1.data; 
     if(n1.next==null){
      n.next =null;
      rear=n;}
     n.next=n1.next;
     length--; 
     return data;}
    int removebefore(node n)//前刪
    {node n1=head.next;
    if(n1==n)return 0;
     while(n1!=null){
      if(n1.next.next.equals(n)){
       return removeafter(n1);}//遇到第一個滿足條件的節點便跳出
      n1=n1.next;
     }//沒有length--;
     return 0;
    }
    int ofdex(int data)//找序
    {node n=head;int i=0;
     while(n!=null){
      if(n.data==data)break;
      n=n.next;i++;
     }
        return i;
    }
    boolean Isempty()//判空
    {if(head.next==null)
     return true;
    return false;}
   
    int contains(int data)//判含
    {node n=head;
    while(n!=null){
     if(n.data==data)return 1;
     n=n.next;
    }
    return -1; 
    }
 node find(int data)//找節點
 {node n=head;
 while(n!=null){
  if(n.data==data)return n;
 n=n.next;
 }
 return null;
 }
 public void clear()//清空
 {head.next=null;
 rear=head; length=0;
 }
 int size()//鏈表長度
    {return length;}
 void traverse()//遍曆
 {if(head.next==null){System.out.println("該鏈表為空白");return;}
 node n=head.next;
 for(int i=0;i<length;i++){
 System.out.println(n.data);
 n=n.next;
 }
 }

LinkList(JAVA版,contain rear)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.