package demo1; import java.util.ArrayList; import java.util.List; import java.util.Stack; import static demo1.SingleLinkdeList.*; public class linkedlist { public static void main(String[] args) { //测试,先创建节点 HeroNode hero1 = new HeroNode(1, "aa", "aaa"); HeroNode hero2 = new HeroNode(2, "bb", "aaa"); HeroNode hero3 = new HeroNode(3, "cc", "aaa"); HeroNode hero4 = new HeroNode(4, "dd", "aaa"); HeroNode hero9 = new HeroNode(5, "dd", "aaa"); HeroNode hero6 = new HeroNode(41, "dd", "aaa"); HeroNode hero7 = new HeroNode(23, "dd", "aaa"); HeroNode hero8 = new HeroNode(55, "dd", "aaa"); HeroNode hero11 = new HeroNode(11, "aa", "aaa"); HeroNode hero21 = new HeroNode(21, "bb", "aaa"); HeroNode hero31 = new HeroNode(13, "cc", "aaa"); HeroNode hero41 = new HeroNode(411, "dd", "aaa"); HeroNode hero91 = new HeroNode(51, "dd", "aaa"); HeroNode hero61 = new HeroNode(411, "dd", "aaa"); HeroNode hero71 = new HeroNode(213, "dd", "aaa"); HeroNode hero81 = new HeroNode(515, "dd", "aaa"); //创建链表 SingleLinkdeList singlelinkdelist1 = new SingleLinkdeList(); SingleLinkdeList singlelinkdelist2 = new SingleLinkdeList(); /*singleLinkdeList.add(hero1); singleLinkdeList.add(hero2); singleLinkdeList.add(hero3); singleLinkdeList.add(hero4);*/ singlelinkdelist1.addByOrder(hero1); singlelinkdelist1.addByOrder(hero4); singlelinkdelist1.addByOrder(hero2); singlelinkdelist1.addByOrder(hero3); singlelinkdelist1.addByOrder(hero9); singlelinkdelist1.addByOrder(hero6); singlelinkdelist2.addByOrder(hero11); singlelinkdelist2.addByOrder(hero41); singlelinkdelist2.addByOrder(hero21); singlelinkdelist2.addByOrder(hero31); singlelinkdelist2.addByOrder(hero91); singlelinkdelist2.addByOrder(hero61); System.out.println(singlelinkdelist1.getnum()); singlelinkdelist1.daoshuget(singlelinkdelist1, 1); singlelinkdelist1.addByOrder(hero7); singlelinkdelist1.addByOrder(hero8); singlelinkdelist2.addByOrder(hero71); singlelinkdelist2.addByOrder(hero81); System.out.println(singlelinkdelist1.getnum()); //singleLinkdeList.addByOrder(hero3); singlelinkdelist1.list(); singlelinkdelist1.daoshuget(singlelinkdelist1, 7); singlelinkdelist1.daoshuget(singlelinkdelist1, 8); //修改 HeroNode hero5 = new HeroNode(4, "ddd", "bbb"); singlelinkdelist1.update(hero5); singlelinkdelist1.list(); singlelinkdelist1.delete(1); singlelinkdelist1.delete(2); singlelinkdelist1.delete(4); singlelinkdelist1.delete(1); singlelinkdelist1.list(); System.out.println(singlelinkdelist1.getnum()); //reversetList(singlelinkdelist1.getHead()); singlelinkdelist1.list(); System.out.println("逆序打印"); int k = singlelinkdelist1.getnum(); reverselist(k, singlelinkdelist1.getHead()); reverseStack(k, singlelinkdelist1.getHead()); System.out.println("合并前l1:"); singlelinkdelist1.list(); System.out.println("合并前l2:"); singlelinkdelist2.list(); System.out.println("合并后:"); ArrayList<HeroNode> a=hebin(singlelinkdelist1, singlelinkdelist2); for (int i = 0; i < a.size(); i++) { System.out.println(a.get(i)); } } //定义Singlelinkedlist } class SingleLinkdeList { //初始化一个头节点,不要动,不存放具体数据 private HeroNode head = new HeroNode(0, "", ""); public HeroNode getHead() {//重要 return head; } //添加节点到单向链表 //不考虑编号顺序时,找到最后节点再将这个next指向新的节点 public void add(HeroNode heroNode) { //因为head节点不能动,因此需要一个辅助变量temp HeroNode temp2 = head; //遍历链表,找到最后 while (true) { if (temp2.next == null) { break; } //没找到就将temp后移 temp2 = temp2.next; } //当循环结束时,temp指向链表最后 temp2.next = heroNode; } //第二种添加方式 public void addByOrder(HeroNode heroNode) { HeroNode temp = head; boolean flag = false;//标志添加的编号是否存在,默认为false while (true) { if (temp.next == null) {//说明temp已经在链表最后 break; } if (temp.next.no > heroNode.no) {//位置找到,就在temp后面添加 break; } else if (temp.next.no == heroNode.no) {//说明已存在 flag = true; System.out.println(temp.next.no); System.out.println(heroNode.no); break; } temp = temp.next;//遍历 } //判断flag if (flag) { System.out.println("已存在"); } else { heroNode.next = temp.next; temp.next = heroNode; } } //修改节点信息 public void update(HeroNode newHeroNode) { //判断为空 if (head.next == null) { System.out.println("空"); return; } HeroNode temp = head.next;//定义一个辅助变量 boolean flag = false; while (true) { if (temp == null) { break;//遍历完 } if (temp.next.no == newHeroNode.no) { flag = true;//找到 break; } temp = temp.next; } if (flag) { temp.name = newHeroNode.name; temp.nickname = newHeroNode.nickname; } else { System.out.println("无此"); } } //删除节点 public void delete(int i) { HeroNode temp = head;//定义一个辅助变量(和上面修改方法不一样!!!) boolean flag = false; while (true) { if (temp.next == null) { //System.out.println("无此"); break;//遍历完 } if (temp.next.no == i) { flag = true;//找到 break; } temp = temp.next; } if (flag) { temp.next = temp.next.next;//被删除的节点将不会有其他引用指向,被垃圾回收机制回收 } else { System.out.println("无此"); } } //求单链表有效节点个数 public int getnum() { HeroNode temp = head.next; int k = 0; while (true) { if (temp == null) { break; } k++; temp = temp.next; } return k; } //倒数节点 public void daoshuget(SingleLinkdeList singleLinkdeList, int k) { HeroNode temp = head; int a = singleLinkdeList.getnum(); if (k > a) { System.out.println("不存在"); return; } int b = a - k + 1; for (int i = 0; i < b; i++) { temp = temp.next; } System.out.println("先打印一遍"); singleLinkdeList.list(); System.out.println("倒数第" + k + "个节点内容是:" + temp.toString()); return; } //单链表反转 public static void reversetList(HeroNode head) { //HeroNode head=singleLinkdeList.getHead(); if (head.next == null || head.next.next == null) {//若为空或者只有1个,无需反转,直接返回 return; } //定义一个辅助指针(变量)帮助遍历原链表 HeroNode cur = head.next; HeroNode next = null;//指向当前节点(cur)的下一个节点 HeroNode reverseHead = new HeroNode(0, "", ""); //遍历原链表 while (cur != null) { next = cur.next;//先暂时保存当前节点的下一个节点 cur.next = reverseHead.next;//将cur的下一个节点指向新的链表的最前端 reverseHead.next = cur;//将cur连接到新的链表上 cur = next;//让cur后移 } //将head.next指向reverseHead.next head.next = reverseHead.next; return; } //逆序打印 public static void reverselist(int k, HeroNode head) { if (head.next == null) { return; } for (int i1 = 0; i1 < k; i1++) { HeroNode temp = head; for (int i = k; i > i1; i--) { temp = temp.next; } System.out.println(temp); } return; } //栈逆序打印 public static void reverseStack(int k, HeroNode head) { if (head.next == null) { return; } Stack<HeroNode> stack = new Stack(); HeroNode temp = head; for (int i = 0; i < k; i++) { stack.add(temp.next);//入栈 temp = temp.next; } while (stack.size() > 0) { System.out.println(stack.pop());//出栈 } return; } //合并两个有序的单链表,合并后依然有序 public static ArrayList<HeroNode> hebin(SingleLinkdeList s1, SingleLinkdeList s2) { ArrayList<HeroNode>a=new ArrayList<>(); HeroNode temp1 = s1.getHead(); HeroNode temp2 = s2.getHead(); HeroNode next = null; HeroNode temp3 = new HeroNode(0, "", ""); while ((temp1.next != null) || (temp2.next != null)) { if (temp1.next == null) { next = temp2.next.next; temp3.next = temp2.next; temp2.next = next; //temp3 = temp3.next; } else if (temp2.next == null) { next = temp1.next.next; temp3.next = temp1.next; temp1.next = next; //temp3 = temp3.next; } else { if (temp1.next.no < temp2.next.no) { next = temp1.next.next; temp3.next = temp1.next; temp1.next = next; //temp3 = temp3.next; } else { next = temp2.next.next; temp3.next = temp2.next; temp2.next = next; //temp3 = temp3.next; } } temp3 = temp3.next; System.out.println("temp3的值是:" + temp3); a.add(temp3); //s3.add(temp3); } return a; } //显示链表,遍历 //通过一个辅助变量 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 = temp.next; } } } class HeroNode { public int no; public String name; public String nickname; public HeroNode next;//指向下一个节点 public HeroNode() { } public HeroNode(int no, String name, String nickname) { this.no = no; this.name = name; this.nickname = nickname; //this.next = next; } /** * 获取 * * @return no */ public int getNo() { return no; } /** * 设置 * * @param no */ public void setNo(int no) { this.no = no; } /** * 获取 * * @return name */ public String getName() { return name; } /** * 设置 * * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * * @return nickname */ public String getNickname() { return nickname; } /** * 设置 * * @param nickname */ public void setNickname(String nickname) { this.nickname = nickname; } /** * 获取 * * @return next */ public HeroNode getNext() { return next; } /** * 设置 * * @param next */ public void setNext(HeroNode next) { this.next = next; } public String toString() { return "HeroNode{no = " + no + ", name = " + name + ", nickname = " + nickname + "}"; } //构造器 }
单链表求节点个数,反转,逆序打印,合并两个有序的单链表
最新推荐文章于 2024-09-05 11:00:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)