栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单链表的增删改查操作(含头插尾插)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单链表的增删改查操作(含头插尾插)

package datastructure;


public class SingleList {
    private Node head;
    public void headInsert(int value){
        Node node = new Node();
        node.data = value;
        if (head==null){
            head = node;
        }else {
            node.next = head;
            head = node;
        }
    }
    public void TailInsert(int value) {
        Node node = new Node();
        node.data = value;
        if (head == null) {
            head = node;
        } else {
            Node tailNode =  head;
            while (tailNode.next != null){
                tailNode = tailNode.next;
            }
            tailNode.next = node;
        }
    }
    public void delete(int index) {
        if (index == 1) {
            head = head.next;
        } else {
            Node tempNode = head;
            Node front = null;
            for(int position = 1; tempNode != null; ++position) {
                if (position == index) {
                    front.next = tempNode.next;
                    tempNode.next = null;
                    return;
                }
                front = tempNode;
                tempNode = tempNode.next;
            }
        }
    }
    public boolean deleteNode(int value){
        boolean flag = false;
        while (head.data==value){
            head = head.next;
            flag = true;
        }
        Node temp = head;
        while (temp != null&&temp.next!=null){
            if (temp.next.data == value){
                temp.next = temp.next.next;
                flag = true;
            }else {
                temp = temp.next;
            }
        }
        return flag;
    }
    public void updateNode(int target, int value) {
        Node node = new Node();
        node.data = target;
        Node temp = head;
        if (head.next == null || head == null) {
            System.out.println("链表为空");
        }
        if (head.data == node.data){
            head.data = value;
        }
        while (temp.next!=null) {
            if (temp == null) {
                System.out.println("未找到改节点,无法修改");
                break;
            }
            if (temp.next.data == node.data) {
                temp.next.data = value;
            }
            temp = temp.next;
        }
    }
    public int getLength() {
        Node tempNode = head;
        int length = 0;
        if (head == null) {
            return 0;
        } else {
            while(tempNode.next != null) {
                length++;
                tempNode = tempNode.next;
            }

            return length;
        }
    }
    public void searchNode(SingleList singleList,int k) {
        if(k<1 || k>singleList.getLength()) {
            System.out.println("查找失败,节点不存在");
            return ;
        }
        Node temp=head;
        int i = k-1;
        while(i>0) {
            temp=temp.next;
            i--;
        }
        System.out.println("查找成功,节点是"+temp);
    }

    public static void main(String[] args) {
        SingleList singleList = new SingleList();
//        singleList.headInsert(1);
//        singleList.headInsert(5);
//        singleList.headInsert(4);
//        singleList.headInsert(3);
        singleList.TailInsert(5);
        singleList.TailInsert(2);
        singleList.TailInsert(3);
        singleList.TailInsert(5);
        singleList.TailInsert(5);
        singleList.TailInsert(5);
        singleList.TailInsert(5);
        System.out.println(singleList);
        singleList.updateNode(5,1);
//        singleList.deleteNode(5);
//        singleList.searchNode(singleList,2);
        System.out.println(singleList);

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

    public static class Node{
        int data;
        Node next;


        @Override
        public String toString() {
            return data +"->"+ next;
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/959417.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号