单链表---插入一个节点
发布日期:2021-05-12 19:32:43 浏览次数:12 分类:精选文章

本文共 1618 字,大约阅读时间需要 5 分钟。

包  单链表创建节点与操作</p>

//创建节点   Node n1=new Node(1); Node n2=new Node(2); Node n3=new Node(3); //追加节点 n1-oppend(n2); n2-oppend(n3); //返回自身的方法可实现链式追加 n1.oppend(n2).oppend(n3).oppend(new Node(4)); //输出节点数据 System.out.println(n1.next().next().next().date); //获取最后节点 System.out.println(n1.next().isLast()); //输出节点删除后的信息 //删除节点 n1.removeNext(); //显示删除后的节点信息 //节点插入操作示例 n3.AfterInsert(new Node(5)); //输出所有节点信息 n1.Show();

//节点结构定义 class Node { //节点内容 int date; //下一个节点 Node next; //创建新节点 Node(int date) { this.date = date; } } //判断是否为最后节点 public boolean isLast() { return next == null; } //获取下一个节点 public Node next() { return this.next; } //获取节点日期 public int getDate() { return this.date; } //显示节点信息 public void Show() { Node currentNode = this; while(true) { System.out.print(currentNode.date + " "); currentNode = currentNode.next; if(currentNode == null) { break; } } System.out.println(); } //追加节点方法 public Node oppend(Node node) { Node currentNode = this; while(true) { Node nextNode = currentNode.next; if(nextNode == null) { break; } currentNode = nextNode; } currentNode.next = node; return this; } //插入节点(示例) public void AfterInsert(Node node) { Node nextNext = next; this.next = node; node.next = nextNext; } //删除下一个节点 public void removeNext() { Node nextNext = next.next; this.next = nextNext; }

上一篇:循环链表
下一篇:单链表---删除单链表的节点

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月13日 15时28分08秒