java實現迴圈鏈表

來源:互聯網
上載者:User

標籤:

前面已經介紹了java實現單鏈表:http://www.cnblogs.com/lixiaolun/p/4643886.html

其實兩者的主要差別就在於如何判斷是否到了鏈表的結尾:

在單鏈表中

while(temp.next!=null){temp=temp.next;}

在迴圈鏈表中

while(temp.next!=header){temp=temp.next;}

 

下面是迴圈鏈表的代碼和測試代碼:

迴圈鏈表的代碼:

package circularlinkedlist;public class CircularLinkedList {class Element{public Object value=null;private Element next=null;}private Element header = null;//頭結點/** * 初始化鏈表 * */void initList(){header = new Element();header.value=null;header.next=header;}/** * 插入鏈表 * */void insertList(Object o){Element e=new Element();e.value=o;if(header.next==header)//第一次插入元素{header.next=e;e.next=header;}else//不是第一次插入元素{//temp引用在棧中,temp和header引用都指向堆中的initList()中new的Element對象Element temp = header;while(temp.next!=header)//尋找最後一個元素{temp=temp.next;}temp.next=e;e.next=header;//新插入的最後一個節點指向頭結點}}/** * 刪除鏈表中第i個元素 * */void deletelist(Object o){Element temp =header;while(temp.next!=header){//判斷temp當前指向的結點的下一個結點是否是要刪除的結點if(temp.next.value.equals(o)){temp.next=temp.next.next;//刪除結點}else{temp=temp.next;//temp“指標”後移}}}/** * 擷取鏈表的第i個位置的元素 * */Element getElement(int i){if(i<=0 || i>size()){System.out.println("擷取鏈表的位置有誤!返回null");return null;}else{int count =0;Element element = new Element();Element temp = header;while(temp.next!=header){count++;if(count==i){element.value=temp.next.value;}temp=temp.next;}return element;}}/** * 鏈表長度 * */int size(){Element temp = header;int size=0;while(temp.next!=header){size++;temp=temp.next;}return size;}/** * 判斷鏈表中是否存在某元素 * */Boolean isContain(Object o){Element temp =header;while(temp.next!=header){if(temp.next.value.equals(o)){return true;}temp=temp.next;}return false;}/** * 列印鏈表 * */void print(){System.out.print("列印鏈表:");Element temp =header;while(temp.next!=header){temp=temp.next;System.out.print(temp.value+"\t");}System.out.println();}}

測試代碼:

package circularlinkedlist;public class CircularLinkedListMain {public static void main(String[] args) {CircularLinkedList clList = new CircularLinkedList();clList.initList();clList.insertList(1);clList.insertList(2);clList.insertList(3);clList.insertList(4);clList.insertList(5);clList.print();System.out.println("鏈表長度:"+clList.size());clList.deletelist(1);clList.deletelist(5);clList.print();System.out.println("第1個元素值為:"+clList.getElement(1).value);System.out.println("第2個元素值為:"+clList.getElement(2).value);System.out.println("第3個元素值為:"+clList.getElement(3).value);System.out.println(clList.isContain(2));System.out.println(clList.isContain(6));//System.out.println(clList.isContain(5));}}

  

java實現迴圈鏈表

聯繫我們

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