首页 > 编程语言 >java实现单链表源码

java实现单链表源码

时间:2022-09-03 13:45:26浏览次数:64  
标签:Node node head 单链 java 源码 date return public

package MyLink.MySingleLink;
import java.util.Objects;
/**
*单链表结点类
* */
public class Node {
/**
* 数据域
* */
private Object date;
/**
* 指针域
* */
private Node next;


public Node(Object date){
this.date = date;
}
public Node(Object date,Node next){
this.date = date;
this.next = next;
}

public Object getDate(){
return this.date;
}
public Node getNext(){
return this.next;
}

public void setDate(Object date){
this.date = date;
}

public void setNext(Node next){
this.next = next;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return Objects.equals(date, node.date) &&
Objects.equals(next, node.next);
}

@Override
public int hashCode() {
return Objects.hash(date, next);
}

@Override
public String toString() {
return "Node{" +
"date=" + date +
", next=" + next +
'}';
}
}

----------------
package MyLink.MySingleLink;

import java.util.Objects;

/**
* 带有头结点的单链表
* */
public class Link {
/**
* 头节点
* */
private Node head;

//构造此链表须初始化头节点
public Link(Node node){
head = node;
}

public Node getHead() {
return head;
}

public void setHead(Node head) {
this.head = head;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Link link = (Link) o;
return Objects.equals(head, link.head);
}

@Override
public int hashCode() {
return Objects.hash(head);
}

@Override
public String toString() {
return "Link{" +
"head=" + head +
'}';
}


/**
* 打印单链表
* */
public void printSingleLink(){
Node pMove = head;
while(pMove != null) {
System.out.print(pMove.getDate() + "-->");
pMove = pMove.getNext();
}
}

/**
* 单链表增加结点 尾插法
* */
public void insertNodeByTail(Node node){
Node pMove = head;
while(pMove.getNext() != null){
pMove = pMove.getNext();
}
pMove.setNext(node);
System.out.println("insert\t"+ node.getDate() +"\t successful!");
}


/**
* 单链表删除结点
* */
public boolean deleteNode(Node node){
//如果没用这个结点 删除失败
if(!findNode(node)){
System.out.println("delete"+ node.getDate() +"fail!");
return findNode(node);
}
//否则执行删除
Node pMove1 = head;
Node pMove2 = head.getNext();
while(pMove1.getNext() != null){
if(pMove2.equals(node)){
pMove1.setNext(pMove2.getNext());
}
pMove1 = pMove2;
pMove2 = pMove1.getNext();
}
System.out.println("delete\t"+ node.getDate() +"\tsuccessful!");
return true;
}


/**
* 单链表查找结点 找到返回true否则false
* */
public boolean findNode(Node node){
Node pMove = head;
while(pMove != null){
if(pMove.equals(node)){
return true;
}
pMove = pMove.getNext();
}
System.out.println("not found "+node.getDate());
return false;
}
}
----------------------
package MyLink.MySingleLink;
/**
* 单链表测试类 程序入口
* */
public class MainT {
public static void main(String[] args){

//创建头结点的单链表
Node head = new Node("head");
Link link = new Link(head);

Node node1 = new Node("first");
Node node2 = new Node("second");
Node node3 = new Node("third");
Node node4 = new Node("four");
Node node5 = new Node("fifth");
Node node6 = new Node("sixth");

link.insertNodeByTail(node1);
link.insertNodeByTail(node2);
link.insertNodeByTail(node3);
link.insertNodeByTail(node4);
link.insertNodeByTail(node5);

link.printSingleLink();
System.out.println();
System.out.println(link.findNode(node6));

link.deleteNode(node5);
}
}

----测试结果---

 

标签:Node,node,head,单链,java,源码,date,return,public
From: https://www.cnblogs.com/pinglu/p/16652458.html

相关文章

  • java实现双向链表
    package数据结构.链表.双链表;publicclassDoubleNode{privateDoubleNodeprecursor;privateintdate;privateDoubleNodenext;publicDoubleNode(i......
  • java.lang.UnsupportedOperationException解决方法
    开始的代码:item.setAnswerpics(Arrays.asList(pointscheckentryList.get(i).getProblems_pics().split(","))) 正确的代码:List<String>picsList=Arrays.asList(......
  • java复习随笔 (二)
    注明:随笔只是作者复习所用,没有实际参考价值面向对象向上转型向上转型即父类引用子类实例,例如:Fatherfather=newSon();特点:丢失子类特有方法向下转型向下转型......
  • HTML、CSS 和 JavaScript 中的所有唯一字符挑战
    HTML、CSS和JavaScript中的所有唯一字符挑战HTML:<divclass="wrapper"><h1>所有独特的字符</h1><br><p>来自<ahref="https://buttondown.email/cassidoo/ar......
  • 前端面试题 JavaScript 基础 —— 2022-09-03
    每日3题13以下代码执行后,控制台中的输出内容为?Object.prototype.a=1;Function.prototype.b=2;functionF(){}varf=newF();console.log(F.a);console.lo......
  • 第一次java测试
    二、数据结构要求:(5分) 1、定义公寓类,类名:RoomInformation,其中包括五个私有变量(roomno,stunno1,stunno2,stunno3,stunno4,hygienescore)。各成员的含义如下:变量roomno为字......
  • java一对一面试指导
    Java一对一面试指导大纲第一章:面试“潜规则” "潜规则"1:面试不是点对点回答问题,而是告诉面试官你会的技术面试的本质不是考试,不是点对点回答问题;而是自己出题,自己......
  • java mysql截取所需数据
    mysql截取数据:例:截取门铺名称,门铺名称长度不确定{"进店日期":"2022-09-01","电话":"1********25","姓名":"张三","单号":"90817","门铺":"吴滨路店","消费金额":"......
  • java mysql删除表中多余的重复记录(多个字段),只留有id最小的记录
    mysql删除表中多余的重复记录(多个字段),只留有id最小的记录DELETEFROM表1fWHERE(f.字段1,f.字段2)IN(SELECT字段1,字段2FROM表1GROUPBY字段1,字段2HAVING......
  • 九月二号——API、java String类的常用方法
    API应用软件编程接口。是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组里程能力,而无需访问源代码,或理解内部工作机制的细节。String类的......