/** * */ package cn.com.wwh; /** * @Description:TODO * @author:wwh * @time:2021-1-18 19:24:47 */ public class SingleLinkedList{ Node list;//头结点 int size;//节点个数 public class Node{ T data; Node next; public Node(T data,Node next) { this.data = data; this.next = next; } } /** * * @Description:头部增加节点 * @param data * @return * @author: wwh * @time:2021-1-18 19:27:24 */ public void add(T data) { Node node = new Node(data, list); list = node; size++; } /** * * @Description:指定位置增加结点 * @param data * @param index * @return * @author: wwh * @time:2021-1-18 19:28:51 */ public void add(T data,int index) { checkIndex(index); if (index == 0) { add(data); return; }else { Node preNode = list; Node curNode = list; for (int i = 0; i < index; i++) { preNode = curNode; curNode = curNode.next; } Node node = new Node(data, curNode); preNode.next = node; size++; } } /** * * @Description:删除头部结点 * @return * @author: wwh * @time:2021-1-18 19:33:51 */ public T remove() { if (list != null) { Node headNode = list; list = list.next; headNode.next = null;//GC size--; return headNode.data; } return null; } /** * * @Description:删除指定位置的结点 * @param index * @return * @return * @author: wwh * @time:2021-1-18 19:35:49 */ public T remove(int index) { checkIndex(index); if (list != null) { if (index == 0) { return remove(); } Node preNode = list; Node curNode = list; for (int i = 0; i < index; i++) { preNode = curNode; curNode = curNode.next; } preNode.next = curNode.next; curNode.next = null; size--; return curNode.data; } return null; } /** * * @Description:删除最后一个结点 * @return * @return * @author: wwh * @time:2021-1-18 19:38:42 */ public T removeLast() { if (list != null) { Node preNode = list; Node curNode = list; while (curNode.next != null) { preNode = curNode; curNode = curNode.next; } preNode.next = null; size--; return curNode.data; } return null; } /** * * @Description:修改指定位置的值 * @param index * @param data * @return * @author: wwh * @time:2021-1-18 19:41:23 */ public void set(int index,T data) { checkIndex(index); Node curNode = list; for (int i = 0; i < index; i++) { curNode = curNode.next; } curNode.data = data; } /** * * @Description:获取头结点 * @return * @return * @author: wwh * @time:2021-1-18 19:42:48 */ public T get() { if (list != null) { return list.data; } return null; } /** * * @Description:获取指定位置的结点 * @param index * @return * @return * @author: wwh * @time:2021-1-18 19:44:06 */ public T get(int index) { checkIndex(index); if (list != null) { Node curNode = list; for (int i = 0; i < index; i++) { curNode = curNode.next; } return curNode.data; } return null; } /** * * @Description:下标检查 * @param index * @return * @author: wwh * @time:2021-1-18 19:29:24 */ public void checkIndex(int index) { if (!(index >= 0 && index size)) { throw new IndexOutOfBoundsException(index +": out of " + size ); } } @Override public String toString() { Node node = list; for (int i = 0; i < size; i++) { System.err.print(node.data + " "); node = node.next; } System.err.println(); return super.toString(); } public static void main(String[] args) { SingleLinkedList list1 = new SingleLinkedList(); for (int i = 0; i < 5; i++) { list1.add(i); } list1.toString(); list1.add(3); list1.toString(); list1.add(34, 6); list1.toString(); list1.remove(); list1.toString(); list1.removeLast(); list1.toString(); list1.remove(0); list1.toString(); list1.set(0, 88); list1.toString(); System.err.println(list1.get()); System.err.println(list1.get(3)); } }
Original: https://www.cnblogs.com/wha6239/p/14295123.html
Author: 一只烤鸭朝北走
Title: java单链表基本操作
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611539/
转载文章受原作者版权保护。转载请注明原作者出处!