java 用鏈表實現線性表

來源:互聯網
上載者:User
 
 
package com.jzm;public class MyLList <T> {  private Node firstNode;        private int length;           //線性表的長度   private class Node{  //私人內部類         private  T  data ;   private  Node next;      private Node(T dataPortion){   data =  dataPortion;   next = null;    }//end constructor        /*private  Node (T dataPortion,Node nextNode){    data = dataPortion;   next = nextNode;  }  //end constructor   */           }//end-Node        public MyLList() { clear();}   public boolean isEmpty(){   return length < 1?true:false;   }  public final void clear(){ firstNode =  null; length = 0;  } private Node getNodeAt(int givenPosition){    assert (!isEmpty())&&(1<= givenPosition) &&(givenPosition <=length);     Node currentNode = firstNode;      for(int i=1;i< givenPosition;i++){        currentNode = currentNode.next;   }    assert  currentNode != null;     return  currentNode;    } public boolean add(T newEntry){//在末端加入元素   Node  newNode = new Node(newEntry);    if(isEmpty()){    firstNode = newNode;    }else {   Node lastNode = getNodeAt(length);    lastNode.next = newNode; //使最後一個結點引用指向新結點      }//end-if   length++;  return true;   }//end add  public boolean add(int newPosition,T newEntry){  if((newPosition>=1)&& (newPosition<=length+1)){  Node newNode= new Node(newEntry);  if(isEmpty() || newPosition==1){  //情況一 newNode.next = firstNode; firstNode = newNode;  }else{  Node nodeBefore = getNodeAt(newPosition-1); Node nodeafter = nodeBefore.next;    newNode.next = nodeafter; nodeBefore.next = newNode;  }//end else  length++; return true;  }else return false;  }//end add(,,)  public T getEntry(int givenPosition){  T result = null;   if(!isEmpty()&&(givenPosition >=1) && givenPosition<= length){      result = getNodeAt(givenPosition).data;   }   return result;  }  public Object remove(int givenPosition){  Object result = null;  if(givenPosition <1 ||givenPosition>length ||isEmpty()){  System.out.println("刪除的位置不存在或者鏈表為空白");  return result;  }else if(givenPosition != 1){    Node deleteNode = getNodeAt(givenPosition);   Node Nodebefore = getNodeAt(givenPosition-1);  Nodebefore.next = deleteNode.next;  length--;  return deleteNode;  }else{    result = firstNode.data;    firstNode = firstNode.next;    length--;    return result;   }  }      public void display(){     if(length <1){ System.out.println("鏈表沒資料!"); }else{     Node currentNode = firstNode;          for (int i = 1; i <= length; i++) {     System.out.println("第"+i+"個資料:"+currentNode.data);      currentNode = currentNode.next;     }  } //for }//end display   }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.