自定义链表增,删除,链表逆序
生活随笔
收集整理的這篇文章主要介紹了
自定义链表增,删除,链表逆序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工作時間長了,經常用框架,感覺真的成了coding,建議有時間可以刷一下lettcode 時間一長就會忘,寫了大半天,記錄一下,理解后再寫特別簡單,鏈表逆序看了很多博客,寫法各式各樣,但是感覺解釋的還是不清楚明了,所以在這又總結一下:
1.首先定義鏈表的結構?
定義的結構Node<T> 這樣data 可以自定義的泛型,增加了靈活性:
public class Node<T> {public T data;public Node next; //可以理解為C語言的指針public Node(){}public Node(T data){this.data = data;}public T getData() {return data;}public void setData(T data) {this.data = data;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;} }?這里設計的 head 頭指針數據默認為空也就是null;
?執行 temp =p ,p=q 執行流程如下:
?
?
執行 tmp =p;p=q;q=q->next 其中q=q->next 是類C/C++寫法進行解釋;?
?這里容易忽視:p.next=null 容易形成回環:
?q=q->next,在這里我理解為探針,查詢是否已經到鏈表結尾了:?
?增刪,逆序,具體代碼如下:
import java.lang.reflect.Array; import java.util.ArrayList;/*** @ClassName MyList* @Description* @Author qianxl* @Date 2019-09-07 17:34* @Version 1.0**/ public class MyList<T> {public Node head = new Node(-1);public Node current;/*** @param data* @description: 添加元素* @return: {@link Node}* @author qianxl* @date: 2019/9/7* @since 1.0*/public Node add(T data) {Node temp = head;Node before = new Node();do {before = temp;} while ((temp = temp.next) != null); //注釋部分是代碼重構使用do while // before = temp; // while (temp != null) { // before = temp; // temp = temp.next; // }temp = new Node(data);before.next = temp;return head;}public Node remove(T values) {Node data = this.head;boolean flag = false;Node before;Node temp = data.next;do {before = data;if (values.equals(temp.data)) {flag = true;break;}} while ((temp = temp.next) != null); //注釋部分是代碼重構使用do while // Node temp = data.next; // Node before =data; // while (temp != null) { // if (values.equals(temp.data)) { // flag = true; // break; // } // before = temp; // temp = temp.next; // }if (flag) {before.next = temp.next;}return head;}/*** @param* @description: 計算集合的長度* @return: {@link int}* @author qianxl* @date: 2019/9/7* @since 1.0*/public int size() {Node data = head;int count = 0;Node tmp;while (data != null) {data = data.next;count++;}return count - 1;//head 不存入數據}/*** @param index* @param value* @description: 指定位置插入值* @return: {@link Node}* @author qianxl* @date: 2019/9/8* @since 1.0*/public Node insert(int index, T value) {Node data = head.next; //去掉頭指針if (index > this.size() || index < 1) {return head;}if (index == 1) {Node node = new Node(value);node.next = head.next; //將新建的節點的指針指向,之前head 頭結點指向的指針head.next = node;return head;}int count = 1;Node before = data; //do {if (index == count) {Node node = new Node(value);node.next = data;before.next = node; //指向新建的節點break;}count++;before = data;data = data.next;} while (data != null); //注釋部分是代碼重構使用do while // Node before =data; // while (data != null) { // if (index == count) { // Node node = new Node(value); // node.next=data; // before.next=node; //指向新建的節點 // break; // } // count++; // before = data; // data = data.next; // }return head;}/*** @param* @description: 鏈表逆序* @return: {@link Node}* @author qianxl* @date: 2019/9/8* @since 1.0*/public Node reverse() {Node p,q=null;p = head.next; //指針 引用q = head.next.next;//q 指針可以理解為探針,在探是否到達鏈表末尾了Node tmp=null;p.setNext(null); //防止回環while (q != null) {tmp =p;p=q;q=q.next; //q=q->next 起到探針的作用p.next=tmp;}head.next =p;return head;}public void print(Node node) {if (node.next == null) {return;}Node temp = node.next;while (temp != null) {System.out.print(temp.data+" ");temp = temp.next;}System.out.println();}/*** @param array* @description: 將數組轉換為list head.next 理解為C語言指針,寫鏈表操作一定要畫圖!* @return: {@link Node}* @author qianxl* @date: 2019/9/7* @since 1.0*/public Node arrayToList(T[] array) {Node data = head;for (int i = 0; i < array.length; i++) {Node node = new Node(array[i]);data.next = node; //表情指向前驅data = node; //表示}return head;}public static void main(String arg[]) {MyList<Object> list = new MyList<>();Node ddd = list.add("ddd");list.add("this is a list");list.add("fffff");list.remove("ddd");list.print(ddd);Node node = list.arrayToList(new String[]{"2","3","4"});list.print(node);// list.insert(1, "8");list.add("8");list.print(node);System.out.println();list.reverse();list.print(node);} }總結
總結
以上是生活随笔為你收集整理的自定义链表增,删除,链表逆序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql及SQLyog安装教程
- 下一篇: C++的字符串分割函数