单链表
接口 LinkList.java
package Struct;
public interface LinkList {
//判断链表为空
public boolean linkListIsEmpty();
//头插
public void linkListPushFront(int value);
//头删
public SNode linkListPopFront();
//打印链表
public void disPlayList();
//查找元素在链表中的位置
public int linkListFind(int to_find);
//删除指定值的元素
public SNode linkListRemove(int to_delete);
//求链表长度
public int linkListSize();
}
实现类LinkList1.java
package Struct;
//单链表节点
class SNode{
int data;//定义数据类型为int型的数据
SNode next;//定义下一个节点
public SNode(int data) {
super();
this.data = data;
}
public void disPlay() {
System.out.print(this.data+"->");
}
}
public class LinkList1 implements LinkList {
private SNode first;
public LinkList1(){
first = null;
}
//判断链表是否为空
public boolean linkListIsEmpty(){
return (first == null);
}
//头插
public void linkListPushFront(int value) {
SNode newNode = new SNode(value);
newNode.next = first;
first = newNode;
}
//头删
public SNode linkListPopFront() {
SNode temp = first;
first = first.next;
return temp;
}
//查找指定元素在链表中的位置
public int linkListFind(int to_find) {
if(first == null){
return 0;//空链表无需查找,直接返回无效值-1
}
SNode current = first;
while(current.data != to_find){
if(current.next == null){
return -1;//表示未找到要查找的元素
}
current = current.next;//移动current
}
return 1;//找到了
}
//打印链表
public void disPlayList(){
SNode current = first;
while(current!=null){
current.disPlay();
current = current.next;
}
System.out.println("");
}
//删除指定值的元素
public SNode linkListRemove(int to_delete) {
if(first== null){
System.out.println("空链表无需此操作");//空链表无需此操作
}
SNode current = first;
SNode pre = first;
while(current.data!=to_delete){
if(current.next == null){
System.out.println("该链表中不存在要指定删除的元素");
}
pre = current;
current = current.next;
}
if(current==first){
//要删除的元素为首元素
first = first.next;
}
else{
//要删除的元素为除首元素外的元素
pre.next = current.next;
}
return current;//找到了要删除元素
}
public int linkListSize(){
int size = 1;
SNode current = first;
while(current.next!=null){
size++;
}
return size;
}
}
测试类TestLinkList.java
package Struct;
public class TestLinkList {
public static void main(String[] args) {
LinkList1 theLink = new LinkList1();
System.out.println(theLink.linkListIsEmpty());
//在空链表中查找指定元素
System.out.println(theLink.linkListFind(5));
theLink.linkListPushFront(1);
theLink.linkListPushFront(2);
theLink.linkListPushFront(3);
theLink.linkListPushFront(4);
System.out.println("头插四个元素后链表为:");
theLink.disPlayList();
System.out.println("链表的长度为:");
System.out.println(theLink.linkListSize());
System.out.println("查找指定元素");
//在链表中查找不存在的元素
System.out.println(theLink.linkListFind(5));
//在链表中查找存在的元素
System.out.println(theLink.linkListFind(2));
theLink.disPlayList();
theLink.linkListPopFront();
theLink.linkListPopFront();
theLink.linkListPopFront();
theLink.linkListPopFront();
System.out.println("头删四个元素后链表为:");
theLink.disPlayList();
theLink.linkListPushFront(1);
theLink.linkListPushFront(2);
theLink.linkListPushFront(3);
theLink.linkListPushFront(4);
theLink.disPlayList();
//要删除的元素为首元素
theLink.linkListRemove(4);
System.out.println("删除元素4后链表为:");
theLink.disPlayList();
//要删除元素为非首元素
theLink.linkListRemove(2);
System.out.println("删除元素2后链表为:");
theLink.disPlayList();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了