Java實現雙向迴圈鏈表的基本操作

來源:互聯網
上載者:User

標籤:find   rip   插入   code   des   ack   基本   head   ros   

  1 package Eric.ADT;  2   3 /**  4  * <p>  5  * Title:doubleLinkedList  6  * </p>  7  * <p>  8  * Description:用java實現雙向迴圈鏈表的基本操作  9  * </p> 10  * <p> 11  * Location:Frostburg 12  * </p> 13  *  14  * @author: Eric.Chen 15  * @date:2017年9月23日下午12:54:05 16  */ 17 public class doubleLinkedList { 18     class Node {// 節點元素 19         private Node prev; 20         public Object value; 21         private Node next; 22     } 23  24     private Node header = null; 25  26     void initiate()// 初始化鏈表 27     { 28         header = new Node(); 29         header.prev = header; 30         header.value = null; 31         header.next = header; 32     } 33  34     public void insertList(int i, Object n)// 在鏈表的i位置插入元素n 35     { 36         if (i <= 0 || i > size()) { 37             System.out.println("插入位置不合法!!!" + "鏈表長度為:" + size()); 38         } else { 39             Node e = new Node(); 40             e.value = n; 41             if (header.prev == header)// 說明這是第一次插入元素,在頭結點後面插入元素 42             { 43                 e.prev = header; 44                 e.next = header; 45                 header.next = e; 46                 header.prev = e; 47  48             } else if (i == size())// 在最後插入元素 49             { 50                 e.next = header; 51                 e.prev = header.prev; 52                 header.prev.next = e; 53                 header.prev = e; 54             } else {// 在i位置插入元素 55                 Node temp = header; 56                 int count = 0; 57                 while (temp.next != header) { 58                     count++; 59                     if (count == i) { 60                         e.next = temp.next; 61                         e.prev = temp; 62                         temp.next.prev = e; 63                         temp.next = e; 64  65                     } 66                     temp = temp.next; 67                 } 68  69             } 70         } 71     } 72  73     public void deleteByIndex(int i)// 刪除指定位置上的元素 74     { 75         if (i <= 0 || i > size()) { 76             System.out.println("刪除的位置誤" + "鏈表長度為:" + size()); 77         } else { 78             Node temp = header; 79             int count = 0; 80             while (temp.next != header) { 81                 count++; 82                 if (count == i) { 83                     temp.next = temp.next.next; 84                     temp.next.prev = temp; 85  86                 } 87  88                 temp = temp.next; 89             } 90  91         } 92  93     } 94  95     public int size()// 返回鏈表的長度 96     { 97         int count = 1; 98         Node temp = header; 99         while (temp.next != header) {100             count++;101             temp = temp.next;102         }103 104         return count;105     }106 107     public void printList() {108         Node temp = header;109         while (temp.next != header) {110             System.out.print(temp.next.value + " ");111             temp = temp.next;112         }113     }114 115     public void deleteByEle(Object element)// 刪除鏈表中指定元素116     {117         Node temp = header;118         int count = 0;119         int num = 0;120         while (temp.next != header) {121             count++;122             if (temp.next.value == element) {123                 num = count;124             }125             temp = temp.next;126         }127         deleteByIndex(num);128     }129 130     public Object findEleByIndex(int i) {131         int count = 0;132         Object p = null;133         if (i <= 0 || i > size()) {134             System.out.println("輸入資料有錯!" + size());135             return -1;136         } else {137             Node temp = header;138             while (temp.next != header) {139                 count++;140                 if (count == i) {141                     p = temp.next.value;142                 }143                 temp = temp.next;144             }145         }146         return p;147     }148 149     public static void main(String[] args) {150         doubleLinkedList list = new doubleLinkedList();151         list.initiate();152         for (int i = 1; i <= 10; i++) {153             list.insertList(i, i);154         }155         System.out.println("原來的鏈表為:");156         list.printList();157         System.out.println();158         System.out.println("鏈表的長度為:" + list.size());159         /*list.deleteByEle(8);160         System.out.println("刪除元素後的鏈表為:");161         list.printList();162         System.out.println();163         System.out.println("刪除元素後的鏈表的長度為:" + list.size());*/164         int idem =(int) list.findEleByIndex(8);165         System.out.println(idem);166 167     }168 }

 

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.