public class Nodefang {
static class Node {
int val; // data | element 要插入的数据
Node next; // 如果 next == null 表示是最后一个结点
public Node(int val) { //构造方法
this.val = val;
this.next = null;
}
}
//打印链表
public static void sys (Node head){
for (Node node = head;node!=null;node=node.next){
System.out.println(node.val + "->");
}
}
//头插法
public static Node pushtou (Node head,int val){
Node node = new Node(val);
node.next = head;
return node;
}
//尾插法
public static Node pushwei (Node head,int val){
Node node = new Node(val);
if (head == null){
return node;
}
Node last = head;
while (last.next!=null){
last =last.next;
}
last.next =node;
return head;
}
//头删法
public static Node poptou (Node head){
if(head ==null){
return null;
}
return head.next;
}
//尾删法
public static Node popwei (Node head){
if (head ==null){
return null;
}
Node last =head;
while (last.next.next != null){
last = last.next;
}
last.next =null;
return head;
}
}
链表的简单概念
最新推荐文章于 2024-07-19 23:15:41 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)