(69条消息) 单链表(ListNode)及操作其基础方法

(69条消息) 单链表(ListNode)及操作其基础方法,第1张

在做算法题的过程中,总会遇见单链表相关的题,在Java中用需要自己定义一个ListNode类来生成链表对象,闲来无事就写了一个ListNode,对链表的操作直接在ListNode里进行了,实际用处不大,仅供参考。如有问题烦请大家指正。下边上代码:

/** * 链表 * */public class ListNode T { /** * 数据域 */ private T val; /** * 指针域 */ private ListNode T next; /** * 头结点 */ private ListNode T head; public ListNode() { } public ListNode(T a) { this(); head = new ListNode T val = a; head.next = this; } /** * 在链表尾端插入节点 * */ public void add(T a) { ListNode T node = new ListNode T if (head == null) { head = new ListNode T head.next = node.head.next; return; } else { ListNode T temp = head; while (temp.next != null) { temp = temp.next; } temp.next = node.head.next; } } /** * 在链表指定位置插入节点 * */ public void add(int index, T a) { if (!isPositionIndex(index)) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ListNode T node = new ListNode T ListNode T temp = head; while (index 0) { temp = temp.next; index--; } ListNode T t = new ListNode (); t = temp.next; temp.next = node; node.next = t; } /** * 在链表尾端添加链表 * */ public void add(ListNode T node) { if (node.head.next != null) { if (head == null) { head = new ListNode T head.next = node.head.next; } else { ListNode T temp = head; while (temp.next != null) { temp = temp.next; } temp.next = node.head.next; } } } /** * 通过下标移除元素 * */ public T remove(int index) { if (!isPositionIndex(index)) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ListNode T temp = head; while (index = 0) { temp = temp.next; index--; } temp.next = temp.next.next; return temp.val; } /** * 移除链表中第一次出现的o * */ public boolean remove(T o) { if (head == null) { return false; } ListNode T temp = head; while (temp.next != null) { if (o == null) { if (o == temp.next.val) { temp.next = temp.next.next; return true; } } else { if (o.equals(temp.next.val)) { temp.next = temp.next.next; return true; } } temp = temp.next; } return false; } /** * 返回下标的元素 * * @param index * @return */ public T get(int index) { return this.getListNode(index).val; } /** * 修改下标的元素 * */ public T set(int index, T o) { ListNode T node = this.getListNode(index); T oldVal = node.val; node.val = o; return oldVal; } /** * 返回下标的节点 * */ private ListNode T getListNode(int index) { if (!isPositionIndex(index)) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ListNode T temp = head; while (index = 0) { temp = temp.next; index--; } return temp; } /** * 返回元素第一次出现的下标 * */ public int get(T o) { if (head == null) { return -1; } int index = 0; ListNode T temp = head; while (temp.next != null) { if (o == null) { if (o == temp.next.val) { return index; } } else { if (o.equals(temp.next.val)) { return index; } } temp = temp.next; index++; } return -1; } /** * 打印当前链表 */ public void printListNode() { if (head == null) { return; } ListNode T temp = head; System.out.print("打印链表:"); while (temp.next != null) { System.out.print(temp.next.val + " "); temp = temp.next; } } /** * 链表长度 * */ public int length() { int count = 0; if (head == null) { return count; } ListNode T temp = head; while (temp.next != null) { temp = temp.next; count++; } return count; } /** * 清空链表 */ public void clear() { if (head == null) { return; } ListNode T temp = head; while (temp.next != null) { ListNode T node = temp.next; temp.head = null; temp.val = null; temp.next = null; temp = node; } } /** * 判断输入索引是否是有效的 * * */ private boolean isPositionIndex(int index) { if (index 0 || index this.length()) { return false; } return true; } /** * 是一个静态util方法,用来打印非当前链表,由于目前方法较少未单独成包 */ public static void printListNode(ListNode ? node) { if (node.head == null) { return; } ListNode ? temp = node.head; System.out.print("打印链表:"); while (temp.next != null) { System.out.print(temp.next.val + " "); temp = temp.next; } } public ListNode T getNext() { return next; } public void setNext(ListNode T next) { this.next = next; } public T val() { return val; }}

 


本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » (69条消息) 单链表(ListNode)及操作其基础方法

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情