链表是一种常见的基础数据结构,是一种有序的列表,但不会按照线性顺序存储数据,而是在每一个节点里存储下一个节点的指针(next)。链表适合插入、删除,不宜过长,否则会导致遍历性能下降。

  • 以节点方式存储;
  • 每个节点包含data域,next域:指向下一个节点;
  • 链表的各个节点不一定是连续存储的;

代码实现:

节点类

public class HeroNode {

    protected Integer no;
protected String name;
protected HeroNode next; public HeroNode(Integer no, String name) {
this.no = no;
this.name = name;
} @Override
public String toString() {
return "HeroNode[" +
"no=" + no +
", name='" + name + '\'' +
']';
}
}

SingleLinkedList

public class SingleLinkedList {

    private HeroNode root;
private Integer size = 0; /**
* 添加至链表头
* @param hero
*/
public void addFirst(HeroNode hero) {
if (root == null) {
root = hero;
} else {
hero.next = root;
root = hero;
}
size++;
} /**
* 添加至链表尾
* @param hero
*/
public void addLast(HeroNode hero) {
if (root == null) {
root = hero;
} else {
HeroNode temp = root;
while (temp.next != null) {
temp = temp.next;
}
temp.next = hero;
}
size++;
} /**
* 按照某属性的顺序添加
* @param hero
*/
public void addByOrder(HeroNode hero) {
if (root == null) {
root = hero; } else {
HeroNode tmp = root;
// 新节点比头节点小
if (hero.no < tmp.no) {
root = hero;
root.next = tmp;
return;
}
// 找到next节点编号大于等于新节点的编号的节点,该节点与它的next节点之间就是新节点所在位置,
// 等于时不添加,控制台进行提示
while (tmp.next != null && tmp.next.no < hero.no) {
tmp = tmp.next;
}
if (tmp.next == null) {
tmp.next = hero;
return;
} if (tmp.next.no.equals(hero.no)) {
System.out.println("编号为" + hero.no + "已存在");
return;
}
hero.next = tmp.next;
tmp.next = hero;
}
size++;
} /**
* 修改
* @param hero
*/
public void modify(HeroNode hero) {
HeroNode tmp = root;
while(tmp !=null) {
if (tmp.no.equals(hero.no)) {
tmp.name = hero.name;
break;
}
tmp = tmp.next;
}
} /**
* 根据编号获取节点
* @param no
* @return
*/
public HeroNode query(int no) {
HeroNode tmp = root;
while (tmp != null) {
if (tmp.no.equals(no)) {
return tmp;
}
tmp = tmp.next;
}
return null;
} /**
* 根据编号删除节点
* @param no
*/
public void remove(int no) {
if (root == null) {
return;
}
//根节点
if (no == root.no) {
if (null != root.next) {
root = root.next;
} else {
root = null;
}
size--;
return;
} // 非根节点
HeroNode temp = root;
while (temp.next != null) {
if (no == temp.next.no) {
break;
}
temp = temp.next;
}
// 节点不存在
if (temp.next == null) {
return;
}
temp.next = temp.next.next;
size--;
} /**
* 打印
*/
public void display() {
if (root == null) {
System.out.println("This SingleLinkedList is null.");
return;
}
HeroNode temp = root;
while(temp != null) {
System.out.println(temp.toString());
temp = temp.next;
}
} /**
* 获取链表长度
* @return
*/
public Integer getSize() {
return size;
}
}

最新文章

  1. 网络存储(四)之ISCSI的进阶
  2. css3 transition effect(其它效果)
  3. LOVE代码收集
  4. allVncClients
  5. 启动Activity,传递参数最佳实践
  6. Oracle EBS 如何月结[Z]
  7. [Erlang危机](4.5)第四章练习
  8. MIME协议在邮件中的应用详解
  9. 一个简单的java贷款程序
  10. 云服务器 远程mysql 无法连接
  11. mfc100u.dll下载和使用方法
  12. 6.使用桌面版AI伴侣或手机版AI伴侣实时预览编程效果
  13. Consul1-window安装consul
  14. scroll滚动条样式修改
  15. android studio设置窗口颜色和字体
  16. VSCode搭建Java开发运行环境
  17. DNF NPK包名对照一览表
  18. 简单的socket_server 和 socket_client(实现文件的上传功能)
  19. Hadoop 系列(一)基本概念
  20. 三,mysql优化--sql语句优化之索引一

热门文章

  1. 始终让footer在底部
  2. javaScript基础题
  3. hourglassnet网络解析
  4. Tomcat面试题汇总
  5. django-spirt 论坛主题
  6. 一、传统MVC token验证方式
  7. C#在Oralce环境执行查询时报&quot;Arithmetic operation resulted in an overflow&quot;
  8. 数据库——Oracle(6)
  9. 【未知来源】Randomized Binary Search Tree
  10. Mybatis config.xml 配置