java 單向鏈表的操作

java 單向鏈表的操作

 今天覆習了一下數據結構,參考了網上的一個例子,自己寫了個單向鏈表的操作。以下附上代碼供大家參考:

 

/**
 * 鏈表節點
 * @author yangchuxi
 *
 */
public class Node<Object> {
  public Object t;
  public Node<Object> next;
  public Node(Object t){
   this.t=t;
  }
  public String toString(){
   return t.toString();
  }

/**
 * 單向鏈表的操作類
 * @author yangchuxi
 *
 */
public class SingleList {
  private Node<Object> head;
     private int size;
     //鏈表的初始化
     public SingleList() {
   size=0;
   //表頭不放數據
   head=new Node<Object>(null);
   head.next=null;
  }
      //插入到鏈表前段(表頭之後)
     public void insertfirst(Node<Object> n){
      n.next=head.next;
      head.next=n;
      size++;
     }
     //插入到鏈表的末尾
     public void insertlast(Node<Object> n){
          n.next=null;
          Node<Object> p=head;
          while(p.next!=null){
           p=p.next;
          }
          p.next=n;
          size++;
     }
     // 在指定節點後添加節點 
     public void insertinto(Node<Object> n1,Node<Object> n2){
      n2.next=n1.next;
      n1.next=n2;
      size++;
     }
     //// 刪除鏈表前端節點
     public void deletehead(){
      Node<Object> current =head.next;
      head.next=current.next;
      current=null;
      size--;
     }
     //// 刪除尾節點
     public void deletetail(){
      Node<Object> current =head;
      Node<Object> pre=null;
      while(current.next!=null){
       pre=current;
       current=current.next;
      }
      current=null;
      pre.next=null;
      size--;
     }
     // 刪除指定節點
     public boolean deltete(Node<Object> n){
      if(n!=null){
       if(head.next!=null &&n==head.next){
        deletehead();
        return true;
       }
       //獲得末端節點
        Node<Object> tail=head;
           while(tail.next!=null){
            tail=tail.next;
           }
           if(n==tail){
            deletetail();
            return true;
           }
           Node<Object> current=head;
           Node<Object> pre=null;
           if(current!=null && current!=n){
            current=current.next;
            while(current.next.next!=null){
             pre=current;
             current=current.next;
            }
            pre.next=current.next;
            current=null;
            return true;
           }
      }
      System.out.println("delete false...........");
      return false;
     }
  // 鏈表長度  
     public int getSize() {  
         return size;  
     }  
  // 遍歷鏈表並打印  
     public void diplay() {  
         Node<Object> current = head.next;  
         while (current != null) {  
             System.out.println(current.toString());  
             current = current.next;  
         }  
     } 
     public static void main(String[] args) {
      //測試鏈表的操作是否正確
   SingleList singleList=new SingleList();
   Node node1=new Node<Object>("testlist1");
   Node node2=new Node<Object>("testlist2");
   Node node3=new Node<Object>("testlist3");
   Node node4=new Node<Object>("testlist4");
   singleList.insertfirst(node1);
   singleList.insertfirst(node2);
   singleList.insertinto(node2, node3);
   singleList.insertlast(node4);
   singleList.diplay();
   //singleList.deletehead();
   //singleList.diplay();
   //singleList.deletetail();
   //singleList.diplay();
   //singleList.deltete(node1);
   //singleList.diplay();
   //singleList.deltete(node2);
   //singleList.diplay();
   singleList.deltete(node4);
   singleList.diplay();
  }

 }


 

 

發佈了6 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章