package 每日算法题; import java.awt.*; import java.util.Collection; public class 链表 <T>{ public Node head;//头结点 public Integer N=0;//结点个数 class Node{ public int date; public Node next; public Node(int date) { this.date = date; } } //判断链表是否为空 public boolean isEmpty(){ //return head==null; return N==0;//结点个数为0代表为空 } public int size(){ return N; } //增加节点 public void add(int date){ if (isEmpty()){//如果为空 直接赋给头 head=new Node(date); }else{ Node node =head; while (node.next!=null){ node=node.next;//找到尾结点 将值赋给尾结点 } node.next=new Node(date);//给尾结点挂链 } N++;//长度++ } //刪除尾结点 public void remove(){ if(isEmpty()){ throw new RuntimeException("结点为空");//如果链表长度为0,报异常
java遍历单链表(删除尾结点、添加)
最新推荐文章于 2023-06-14 02:03:18 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)