您好,登錄后才能下訂單哦!
本篇內容介紹了“數據庫中怎么合并兩個有序鏈表”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
package com.clean.notes; public class Node { String data; Node next; public Node() { } public Node(String data) { this.data = data; } public Node(String data, Node next) { this.data = data; this.next = next; } public String getData() { return data; } public void setData(String data) { this.data = data; } }
package com.clean.notes; public class MyNode { //private static Node head = new Node("0", null); private Node head = null; // public static void main(String[] args) { // addNode("1"); // addNode("2"); // insertNode(head,2,"3"); // deleteNode(head,2); // traverse(head); // linkListLength(head); // System.out.println("------------"); // printNodes(head); // System.out.println("--------------"); // Node prev = reverseLinkList(head); // printNodes(prev); // } public MyNode(String x) { head = new Node(x,null); } /** * 添加數據作為節點 * * @param data */ public void addNode(String data) { Node newNode = new Node(data); Node temp = this.getHead(); while (temp.next != null) { temp = temp.next; } temp.next = newNode; } /** * 從head 遍歷鏈表中的數據 * @param head */ public void traverse(Node head) { Node temp = head; while (temp != null) { System.out.println(temp.data); temp = temp.next; } } /** * 獲取現在鏈表的長度 * @param head * @return */ public int linkListLength(Node head) { Node temp = head; int i = 0; while (temp != null) { i ++; temp = temp.next; } int len = i; System.out.println("length = " + len); return len; } /** * 添加節點 * @param head * @param index * @param data */ public void insertNode(Node head, int index, String data) { int len = linkListLength(head); if(index < 1 || index > len) { System.out.println("輸入index不合法!"); return; } int currentPos = 0; Node temp = head; Node newNode = new Node(data); while (temp != null) { if((index - 1) == currentPos) { newNode.next = temp.next; temp.next = newNode; return; } temp = temp.next; currentPos ++; } } /** * 刪除節點 * @param head * @param index */ public void deleteNode(Node head, int index) { int len = linkListLength(head); if(index < 1 || index > len) { System.out.println("輸入index不合法!"); return; } int currentPos = 0; Node temp = head; while (temp.next != null) { if((index - 1) == currentPos) { Node deteteNode = temp.next; temp.next = deteteNode.next; return; } temp = temp.next; currentPos ++; } } public static void printNodes(Node head) { if(head != null) { System.out.println(head.data); printNodes(head.next); } } public Node getHead() { return this.head; } public void setHead(Node head) { this.head = head; } /** * 從head 遍歷鏈表中的數據 * @param head */ public static void traverseString(Node head) { StringBuffer stringBuffer = new StringBuffer(); Node temp = head; while (temp != null) { //System.out.println(temp.data); stringBuffer.append(temp.data).append("->"); temp = temp.next; } String resStr = stringBuffer.toString(); System.out.println(resStr.substring(0,resStr.lastIndexOf("->"))); } /** * 實現鏈表的反轉 * * @param node 鏈表的頭節點 */ public static Node reverseLinkList(Node node) { Node prev ; if (node == null || node.next == null) { prev = node; } else { Node tmp = reverseLinkList(node.next); node.next.next = node; node.next = null; prev = tmp; } return prev; } }
package com.clean.notes; public class App { public static void main(String[] args) { MyNode L1 = new MyNode("x"); MyNode L2 = new MyNode("y"); MyNode L3 = new MyNode("z"); L1.addNode(7 +""); L1.addNode(10 +""); L1.addNode(24 +""); L1.addNode(68 +""); L2.addNode(9 +""); L2.addNode(17 +""); L2.addNode(23 +""); L2.addNode(50 +""); L2.addNode(100 +""); L2.addNode(200 +""); int sizeA = L1.linkListLength(L1.getHead()); int sizeB = L2.linkListLength(L2.getHead()); System.out.println("sizeA = " + sizeA); System.out.println("sizeB = " + sizeB); L1.traverseString(L1.getHead()); L2.traverseString(L2.getHead()); Node temp = L1.getHead(); Node temp2 = L2.getHead(); while (temp.next!= null && temp2.next != null) { temp = temp.next; temp2 = temp2.next; if(temp.data.compareTo(temp2.data) < 0) { L3.addNode(temp.data); L3.addNode(temp2.data); } else { L3.addNode(temp2.data); L3.addNode(temp.data); } } while (temp.next != null) { temp = temp.next; L3.addNode(temp.data); } while (temp2.next != null) { temp2 = temp2.next; L3.addNode(temp2.data); } int z = L3.linkListLength(L3.getHead()); System.out.println("length L3 = " + z); L3.traverseString(L3.getHead()); } }
package com.clean.notes; public class Client { public static void main(String[] args) { Client client = new Client(); MyNode L1 = new MyNode("x"); MyNode L2 = new MyNode("y"); L1.addNode(5 +""); L1.addNode(4 +""); L1.addNode(8 +""); L1.addNode(3 +""); L1.addNode(2 +""); L1.addNode(1 +""); L1.addNode(7 +""); L2.addNode(9 +""); L2.addNode(1 +""); L2.addNode(3 +""); L2.addNode(2 +""); L2.addNode(1 +""); L2.addNode(7 +""); int sizeA = L1.linkListLength(L1.getHead()); int sizeB = L2.linkListLength(L2.getHead()); System.out.println("sizeA = " + sizeA); System.out.println("sizeB = " + sizeB); L1.traverseString(L1.getHead()); L2.traverseString(L2.getHead()); int len = client.getLength(L1,L2); System.out.println("len = " + len); Node temp = L1.getHead(); System.out.println(temp.next.data); int j = 0; while (j < len) { temp = temp.next; j ++; } Node temp2 = L2.getHead(); while (!temp.data.equalsIgnoreCase(temp2.data)) { temp = temp.next; temp2 = temp2.next; } System.out.println("the same = " + temp2.data); } public int getLength(MyNode h2, MyNode h3) { int len1 = h2.linkListLength(h2.getHead()); int len2 = h3.linkListLength(h3.getHead()); if(len1 > len2) { return len1 - len2; } else { return len2 - len1; } } }
“數據庫中怎么合并兩個有序鏈表”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。