原创

列表

温馨提示:
本文最后更新于 2022年06月18日,已超过 8 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

链表

  • 链表是以节点的方式来存储,是链式存储
  • 每个节点包含 data 域, next 域:指向下一个节点.
  • 如图:发现链表的各个节点不一定是连续存储.
  • 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
package likedList;

import javax.xml.transform.Templates;

public class SingalList {
    //定义一个hearNode
    public static void main(String[] args) {
        HearNode hearNode1 = new HearNode(1, "djhf", ";fkj");
        HearNode hearNode2 = new HearNode(2, "djhf", ";fkj");
        HearNode hearNode3 = new HearNode(3, "3", ";fkj");
        HearNode hearNode4 = new HearNode(4, "djhf", ";fkj");
        HearNode hearNode5 = new HearNode(5, "5", ";fkj");
        HearNode hearNode6 = new HearNode(6, "djhf", ";fkj");
        HearNode hearNode7 = new HearNode(7, "djhf", ";fkj");
        HearNode updata02 = new HearNode(2, "是如同一个", "sudygh");
        SingalLinkedList singalLinkedList = new SingalLinkedList();
        singalLinkedList.addData2(hearNode1);
        singalLinkedList.addData2(hearNode2);
        singalLinkedList.seeLikedList();
        singalLinkedList.updata(updata02);
        singalLinkedList.seeLikedList();
    }

}
class HearNode{
    public int  no;
     public String name;
     public String nickName;
     public HearNode next;
     //构造器
public HearNode( int no, String name, String nickname) {
    // TODO Auto-generated constructor stub
    this.no = no;
    this.name = name;
    this.nickName = nickname;
}
//重写toString方法
@Override
public String toString() {
    return "HearNode [no=" + no + ", name=" + name + ", nickName=" + nickName + "]";
 }

}
class SingalLinkedList{
    //初始化一个头结点
    private HearNode head = new HearNode(0, null, null);
//    public void addData(HearNode node) {
//        //需要一个辅助节点
//        HearNode temp = head;
//        //找到最后
//        while(temp.next !=null) {
//            temp = temp.next;
//        }
//        temp.next = node;
//    }
    //第二种添加数据的方法,按照顺序
    public void addData2(HearNode node) {
        HearNode temp = head;
        boolean flag = true;
        while(flag) {
            if(temp.next == null) {
                temp.next = node;
                break;
            }else if(temp.next.no>=node.no) {
                node.next = temp.next;
                temp.next = node;
                break;
            }else if(temp.next.no == node.no){
                System.out.println("dlkufhg");
                break;
            }
            temp = temp.next;
        }
    }
    //遍历
    public void seeLikedList() {
        if(head.next == null) {
            System.out.println("链表为空");
        }
        HearNode temp = head.next;
        while(temp !=null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }
    //修改节点,根据no来修改
    public void updata(HearNode updata) {
        HearNode temp = head;
        boolean flag = true;
        while(true){
            if(temp.next == null) {
                break;
            }else if(temp.next.no == updata.no) {
                flag = false;
                break;
            }
            temp = temp.next;
        }
        if(flag == false) {
            temp.next.name = updata.name;
            temp.next.nickName = updata.nickName;
        }else {
            System.out.println("找不到节点");
        }
    }
    //删除节点
    public void delete(int flag) {
        HearNode temp = head;
        while(temp.next !=null || temp.next.no !=flag) {
            temp = temp.next;
        }
        if(temp.next !=null) {
            temp.next = temp.next.next;
        }else {
            System.out.println("找不到节点");
        }
    }
}
正文到此结束