java資料結構和演算法------線性表(鏈表結構)

來源:互聯網
上載者:User

標籤:

  1 package iYou.neugle.list;  2   3 public class MyChainList<T> {  4     // 向鏈表尾部添加元素  5     public Node<T> AddEnd(Node<T> head, T data) {  6         Node<T> node = new Node<T>();  7         node.data = data;  8         if (head == null) {  9             head = node; 10             return head; 11         } 12         SearchEnd(head).next = node; 13         return head; 14     } 15  16     private Node<T> SearchEnd(Node<T> head) { 17         if (head.next == null) { 18             return head; 19         } 20         return SearchEnd(head.next); 21     } 22  23     // 向鏈表頭部添加元素 24     public Node<T> AddHead(Node<T> head, T data) { 25         Node<T> node = new Node<T>(); 26         node.data = data; 27         Node<T> nodeCopy = DeepCopy(head, new Node<T>()); 28         node.next = nodeCopy; 29  30         head.data = node.data; 31         head.next = node.next; 32         return head; 33     } 34  35     private Node<T> DeepCopy(Node<T> head, Node<T> node) { 36         if (head != null) { 37             node.data = head.data; 38             node.next = head.next; 39         } else { 40             return null; 41         } 42         DeepCopy(head.next, node.next); 43         return node; 44     } 45  46     // 向鏈表中插入資料 47     public Node<T> Insert(Node<T> head, T key, T data) { 48         if (head.next == null) { 49             return null; 50         } 51  52         if (head.data.equals(key)) { 53             return AddHead(head, data); 54         } 55  56         if (head.next.data.equals(key)) { 57             Node<T> node = new Node<T>(); 58             node.data = data; 59             node.next = head.next; 60             head.next = node; 61             return head; 62         } 63         Insert(head.next, key, data); 64         return head; 65     } 66  67     // 向鏈表中刪除資料 68     public Node<T> Delete(Node<T> head, T key) { 69         if (head.next == null) { 70             return null; 71         } 72  73         if (head.data.equals(key)) { 74             head.data = head.next.data; 75             head.next = head.next.next; 76             return head; 77         } 78  79         if (head.next.data.equals(key)) { 80             head.next = head.next.next; 81             return head; 82         } 83         Delete(head.next, key); 84         return head; 85     } 86  87     // 按關鍵字尋找節點 88     public void Search(Node<T> head, T key) { 89         if (head.data.equals(key)) { 90             System.out.println("查詢節點為頭結點,節點值為:" + key); 91             return; 92         } 93         while (head.next != null) { 94             if (head.next.data.equals(key)) { 95                 System.out.print("查詢節點為:" + key + ","); 96                 System.out.print("查詢節點前置節點為:" + head.data + ","); 97                 if (head.next.next != null) { 98                     System.out.println("查詢節點後置節點為:" + head.next.next.data); 99                 } else {100                     System.out.println("查詢節點為尾節點!");101                 }102                 return;103             }104             head = head.next;105         }106         System.out.println("沒有該節點!");107     }108 109     // 擷取鏈表長度110     public int SizeOf(Node<T> head) {111         // 如果沒有節點,返回-1112         if (head == null) {113             return -1;114         }115         int n = 1;116         while (head.next != null) {117             n++;118             head = head.next;119         }120         return n;121     }122 123     public void Print(Node<T> head) {124         while (head != null) {125             System.out.println(head.data);126             head = head.next;127         }128     }129 }

 

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.