Java雙向鏈表倒置功能實現過程解析
題目要求:Java實現一個雙向鏈表的倒置功能(1->2->3 變成 3->2->1)
提交:代碼、測試用例,希望可以寫成一個Java小項目,可以看到單元測試部分
該題目的代碼,已經放到了我的github上,地址為:https://github.com/jiashubing/alibaba-linkedlist-reversed.git
最關鍵的是自定義節點Node 和自定義雙向鏈表MyLinkedList 兩個類,倒置的方法放在自定義鏈表類里reversed() ,具體的說明都在代碼中
自定義節點類Node.java,有三個參數 :T data(當前值)、Node<T> left(左節點)、Node<T> right(右節點)
自定義雙向鏈表類MyLinkedList.java,有兩個參數:Node<T> head(頭結點)、Node<T> current(當前節點,也是最后一個節點)
添加節點的方法void add(T data):添加的第一個節點Node,它的左節點為null,最后一個節點的右節點也為null,中間的每個節點的左右節點都有值
倒置鏈表的方法reversed():把每個節點的左右節點交換,并且把鏈表的首尾節點也交換,就可以了。這里需要考慮的是循環的終止條件。我的實現如下:
public void reversed() {if (head == null || head.getRight() == null) {return;}current = head;while(true) {//交換左右節點Node<T> tmp = head.getLeft();head.setLeft(head.getRight());head.setRight(tmp);//如果左節點為空,則終止,否則循環執行if (head.getLeft() == null) {return;} else {head = head.getLeft();}}}
剩下的測試用例,就簡單了。下面是我的github上的代碼,記錄下:
pom.xml
<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>cn.jiashubing</groupId> <artifactId>alitest</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies></project>
Node.java
package cn.jiashubing;/** * 自定義節點 * * @author jiashubing * @since 2018/3/30 */class Node<T> { /** * 當前值 */ private T data; /** * 左節點 */ private Node<T> left; /** * 右節點 */ private Node<T> right; Node(T data) { this.data = data; this.left = null; this.right = null; } T getData() { return data; } void setData(T data) { this.data = data; } Node<T> getLeft() { return left; } void setLeft(Node<T> left) { this.left = left; } Node<T> getRight() { return right; } void setRight(Node<T> right) { this.right = right; }}
MyLinkedList.java
package cn.jiashubing;/** * 自定義雙向鏈表 * * @author jiashubing * @since 2018/3/30 */public class MyLinkedList<T> { /** * 頭結點 */ private Node<T> head; /** * 當前節點 */ private Node<T> current; /** * 添加節點 * 如果頭節點為空,則賦值為當前節點 * 否則,要雙向設置,當前節點向后移動一位 * * @param data 當前節點的值 */ public void add(T data) { if (head == null) { head = new Node<T>(data); current = head; } else { Node<T> tmp = new Node<T>(data); current.setRight(tmp); tmp.setLeft(current); current = current.getRight(); } } /** * 正向打印鏈表 * * @param node 當前節點 */ public void print(Node<T> node) { if (node == null) { return; } Node<T> tmp = node; while (tmp != null) { System.out.print(tmp.getData() + ' '); tmp = tmp.getRight(); } System.out.println(''); } /** * 反向打印鏈表 * * @param node 當前節點 */ public void printRev(Node<T> node) { if (node == null) { return; } Node<T> tmp = node; while (tmp != null) { System.out.print(tmp.getData() + ' '); tmp = tmp.getLeft(); } System.out.println(''); } /** * 鏈表倒置 */ public void reversed() { if (head == null || head.getRight() == null) { return; } current = head; while(true) { //交換左右節點 Node<T> tmp = head.getLeft(); head.setLeft(head.getRight()); head.setRight(tmp); //如果左節點為空,則終止,否則循環執行 if (head.getLeft() == null) {return; } else {head = head.getLeft(); } } } public Node<T> getHead() { return head; } public Node<T> getCurrent() { return current; }}
JunitTest.java
import cn.jiashubing.MyLinkedList;import org.junit.Before;import org.junit.Test;/** * @author jiashubing * @since 2018/3/30 */public class JunitTest { private MyLinkedList<Integer> list; @Before public void setNum() { list = new MyLinkedList<Integer>(); for (int i = 1; i < 4; i++) { list.add(i); } System.out.println('正向打印: '); list.print(list.getHead()); } @Test public void test1() { System.out.println('鏈表倒置后正向打印 '); list.reversed(); list.print(list.getHead()); } @Test public void test2() { System.out.println('逆向打印 '); list.printRev(list.getCurrent()); }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: