数据库中怎么合并两个有序链表
本篇内容介绍了“数据库中怎么合并两个有序链表”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
成都创新互联主营山西网站建设的网络公司,主营网站建设方案,手机APP定制开发,山西h5重庆小程序开发搭建,山西网站营销推广欢迎山西等地区企业咨询
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; } } }
“数据库中怎么合并两个有序链表”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
文章名称:数据库中怎么合并两个有序链表
网页网址:http://pwwzsj.com/article/gcoecc.html