/**
*
*/
package cn.com.wwh; /**
* @Description:TODO
* @author:wwh
* @time:2021-1-18 19:24:47
*/
public class SingleLinkedList <T>{ 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));
}
}

最新文章

  1. 移动端click事件延迟300ms的原因以及解决办法
  2. select元素javascript常用操作 转
  3. Population-based metagenomics analysis reveals markers for gut microbiome composition and diversity
  4. phpcms v9 源码解析(4)content模块下的index.php文件的init()方法解析
  5. 集中式(CVS、SVN)VS分布式(Git)
  6. 我的第一篇博客。(JavaScript的声明和数据类型的一些笔记)
  7. makefile笔记5 - makefile变量
  8. python 决策树
  9. [HihoCoder1369]网络流一&#183;Ford-Fulkerson算法
  10. JavaUtil smtp 邮件发送
  11. SD从零开始51-54 信用控制范围, 信用范围数据维护, 自动信用控制, 信用控制-阻止后续功能
  12. word绘图画布
  13. Android 避免APP启动闪黑屏(Theme和Style)
  14. 基于Spring Cloud的微服务落地
  15. 未能加载“xxx”程序集
  16. 润乾在jetty应用服务器下的JNDI配置一
  17. 炫酷的CSS3抖动样式:CSS Shake
  18. 使用Redis进行简单的数据缓存
  19. 自动出题判分——c#学习实践
  20. ftp,nfs和samba的区别

热门文章

  1. zabbix server&proxy部署操作过程
  2. SwitchHosts管理编辑hosts工具
  3. VSCode 前端常用插件集合
  4. 数据传输POST心法分享,做前端的你还解决不了这个bug?
  5. openlayers API实现鹰眼图OverviewMap时地图不断闪烁等问题的解决思路
  6. Go语言实践模式 - 函数选项模式(Functional Options Pattern)
  7. input输入框自动填充的问题
  8. input 标签的 pattern 属性
  9. ABP应用开发(Step by Step)-下篇
  10. 自学java的困难