推广网站的形式包括,营销战略有哪些内容,少儿编程是智商税吗,百度站长工具数据提交工作时间长了#xff0c;经常用框架#xff0c;感觉真的成了coding,建议有时间可以刷一下lettcode 时间一长就会忘#xff0c;写了大半天#xff0c;记录一下#xff0c;理解后再写特别简单#xff0c;链表逆序看了很多博客#xff0c;写法各式各样#xff0c;但是感觉…工作时间长了经常用框架感觉真的成了coding,建议有时间可以刷一下lettcode 时间一长就会忘写了大半天记录一下理解后再写特别简单链表逆序看了很多博客写法各式各样但是感觉解释的还是不清楚明了所以在这又总结一下
1.首先定义链表的结构
定义的结构NodeT 这样data 可以自定义的泛型增加了灵活性
public class NodeT {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 ,pq 执行流程如下 执行 tmp p;pq;qq-next 其中qq-next 是类C/C写法进行解释 这里容易忽视p.nextnull 容易形成回环 qq-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 MyListT {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.nextdata;
// before.nextnode; //指向新建的节点
// 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,qnull;p head.next; //指针 引用q head.next.next;//q 指针可以理解为探针在探是否到达链表末尾了Node tmpnull;p.setNext(null); //防止回环while (q ! null) {tmp p;pq;qq.next; //qq-next 起到探针的作用p.nexttmp;}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[]) {MyListObject 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);}
}
总结