节点
public class HeroNode { public int no; public String name; public String nickname; public HeroNode next; public HeroNode(int hNo, String hName, String hNickname) { no = hNo; name = hName; nickname = hNickname; } @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nickname='" + nickname + '\'' + '}'; } }
package org.minos.linkedlist; import java.util.Stack; public class SingleLinkedList { //初始化一个头结点,头结点作为一个标志位 private HeroNode head = new HeroNode(0, "", ""); /** * 添加一个节点 * * @param heroNode */ public void add(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { temp.next = heroNode; break; } temp = temp.next; } } //按照排序添加 public void addByOrder(HeroNode heroNode) { HeroNode temp = head; //是否有重复 boolean flag = false; int no = heroNode.no; while (true) { // 链表末尾跳出 if (temp.next == null) { break; } if (no < temp.next.no) { break; } else if (no == temp.next.no) { flag = true; } temp = temp.next; } //没有重复的 if (!flag) { heroNode.next = temp.next; temp.next = heroNode; } else { System.out.println("添加元素重复"); } } //修改节点,编号不能修改 public void update(HeroNode newHeroNode) { HeroNode tem = head.next; while (true) { if (tem == null) { break; } if (tem.no == newHeroNode.no) { tem.nickname = newHeroNode.nickname; tem.name = newHeroNode.name; break; } tem = tem.next; } } /** * 删除节点 */ public void delete(int no) { HeroNode tem = head; while (true) { if (tem.next == null) { break; } if (tem.next.no == no) { tem.next = tem.next.next; break; } tem = tem.next; } } //获取节点的长度 public int length() { HeroNode tem = head; int length = 0; while (tem.next != null) { length++; tem = tem.next; } return length; } /** * 寻找倒数第N个节点 * * * @param index * @return */ public HeroNode findLastIndexNode(int index) { int length = length(); //空链表、输入不合理返回null if (head.next == null || index <= 0 || index > length) { return null; } HeroNode tem = head.next; for (int i = 0; i < length - index; i++) { tem = tem.next; } return tem; } /** * 使用栈的数据结构,先进后出, */ public void reversePrint() { if (head.next == null) { return; } HeroNode cur = head.next; //创建一个容器 Stack<HeroNode> stack = new Stack<HeroNode>(); //将链表数据压入栈中 while (cur != null) { stack.add(cur); cur = cur.next; } //将栈中的数据进行打印 while (stack.size() > 0) { System.out.println(stack.pop()); } } public void reverse() { //链表为空或者只有一个节点,无须翻转 if (head.next == null || head.next.next == null) { return; } //当前节点 HeroNode cur = head.next; //当前节点的下一个节点 HeroNode next = null; //翻转后的头结点 HeroNode reversHead = new HeroNode(0, "", ""); while (cur != null) { //记录原链表的后一个节点 next = cur.next; //将当前节点的后一个节点修改为反转后链表 cur.next = reversHead.next; //将反转后链表的头,指向当前链表 reversHead.next = cur; //修改当前链表 cur = next; } head = reversHead; } public void list() { if (head.next == null) { System.out.println("链表为空"); return; } HeroNode temp = head.next; while (true) { //判断是否到链表的最后 if (temp == null) { break; } System.out.println(temp); temp = temp.next; } } }
测试
package org.minos.linkedlist; public class SingleLinkedListDemo { public static void main(String[] args) { //SingleLinkedList linkedList=new SingleLinkedList(); //linkedList.add(new HeroNode(1,"宋江","")); //linkedList.add(new HeroNode(2,"卢俊义","")); //linkedList.add(new HeroNode(3,"吴用","")); //linkedList.add(new HeroNode(4,"林冲","")); //linkedList.list(); SingleLinkedList linkedList2=new SingleLinkedList(); System.out.println(linkedList2.length()); linkedList2.addByOrder(new HeroNode(2,"卢俊义","")); linkedList2.addByOrder(new HeroNode(1,"宋江","")); linkedList2.addByOrder(new HeroNode(9,"吴用","")); linkedList2.addByOrder(new HeroNode(4,"林冲","")); linkedList2.addByOrder(new HeroNode(3,"吴用","")); linkedList2.list(); System.out.println(); linkedList2.reversePrint(); //linkedList2.list(); //linkedList2.reverse(); //linkedList2.list(); } }