package cn.chmcyz.linkedlist;
/**
* @author 谌涣谋
* @date 2020/5/14 - 17:01
*/
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList list=new SingleLinkedList();
HeroNode node1=new HeroNode(1,"宋江","及时雨");
HeroNode node2=new HeroNode(2,"卢俊义","玉麒麟");
list.show();
list.add(node1);
list.add(node2);
list.show();
list.delete(1);
list.show();
list.update(new HeroNode(2,"卢俊义2","玉麒麟"));
list.show();
}
}
class SingleLinkedList{
private HeroNode head=new HeroNode(0,"","");//定义一个头结点,不存放任何数据
/**
* 在链表中按排名添加元素思路
* 找到链表中第一个比该节点的no值大的节点x
* 将节点x-1的next设置成插入的节点,将插入的节点的next设置成X
* @param heroNode
*/
public void addByOrder(HeroNode heroNode){
boolean flag=false;//是否已经存在该排名结点的标志
HeroNode temp=head;
while (true){
//如果是链表最后一个结点,Break
if(temp.getNext()==null){
break;
}
if(temp.getNo()<heroNode.getNo()){
break;
}
if(temp.getNo()==heroNode.getNo()){
flag=true;
break;
}
temp=temp.getNext();
}
heroNode.setNext(temp.getNext());
temp.setNext(heroNode);
}
/**
* 在链表最后添加元素思路
* 找到链表的最后一个元素
* 将链表的最后一个元素的next设置成当前元素
* @param heroNode
*/
public void add(HeroNode heroNode){
HeroNode temp=head;
while (temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(heroNode);
}
/**
* 获取链表长度
*/
public int getSize(){
int count=0;
HeroNode temp=head.getNext();
while (temp!=null){
count+=1;
System.out.println(temp);
temp=temp.getNext();
}
return count;
}
/**
* 删除结点
*
*/
public void delete(int no){
HeroNode temp=head.getNext();
HeroNode prevous=head;//定义一个先节点
if(temp==null){
throw new RuntimeException("不存在该节点");
}
while (temp!=null){
if(temp.getNo()==no){
//如果找到了目标结点,则删除该节点,并且终止该方法
prevous.setNext(temp.getNext());
return;
}
prevous=temp;
temp=temp.getNext();
}
//如果没有找到,抛异常
throw new RuntimeException("不存在该节点");
}
/**
* 修改节点的值
*/
public void update(HeroNode newNode){
HeroNode temp=head.getNext();
if(temp==null){
throw new RuntimeException("不存在该节点");
}
while(temp!=null){
if(temp.getNo()==newNode.getNo()){
temp.setName(newNode.getName());
temp.setNickName(newNode.getNickName());
return;
}
temp=temp.getNext();
}
//如果没有找到,抛异常
throw new RuntimeException("不存在该节点");
}
/**
* 遍历单链表的方法
*/
public void show(){
HeroNode temp=head.getNext();
if(temp==null){
System.out.println("该链表为空");
return;
}
while (temp!=null){
System.out.println(temp);
temp=temp.getNext();
}
System.out.println("=====================");
}
}
//定义一个HeroNode类,每一个HeroNode对象就是一个节点
class HeroNode{
private int no;
private String name;
private String nickName;
private HeroNode next;
public HeroNode(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public HeroNode getNext() {
return next;
}
public void setNext(HeroNode next) {
this.next = next;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
单链表Java实现
最新推荐文章于 2025-03-18 22:20:32 发布