单链表操作

package DataStructTest.练习;

import java.util.Scanner;

public class 单链表 {
    public static void main(String[] args) {
        Node a = new Node(1);
        Node a1 = new Node(88);
        Node a2 = new Node(66);
        Node a3 = new Node(68);
        LinkedList l = new LinkedList();
        l.add(a);
        l.add(a1);
        l.add(a2);
        l.add(a3);
        l.show();
        l.find(88);
        //l.update(88);
        l.show();
        System.out.println("============");
        LinkedList l1 = new LinkedList();
        l1.addByOrder(a);
        l1.addByOrder(a1);
        l1.addByOrder(a2);
        l1.addByOrder(a3);
        l1.show();
        l1.find(88);
    }
}

class LinkedList {
    private Node head = new Node(0);

    public LinkedList() {

    }

    //增加操作
    public void add(Node node) {
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
    }

    //删除操作
    public void del(int value) {
        Node temp = head.next;
        Node temp1 = null;
        //当temp.getValue()
        while (temp.getValue() != value) {
            temp1 = temp;
            temp = temp.next;
            if (temp == null) {
                System.out.println("没有找到");
                break;
            }
        }
        temp1.next = temp.next;
    }

    //查找操作
    public void find(int value) {
        Node temp = head.next;
        //当temp.getValue()
        while (temp.getValue() != value) {
            temp = temp.next;
            if (temp == null) {
                System.out.println("没有找到");
                return;
            }
        }
        System.out.println("找到了" + temp.getValue());
    }

    //修改操作
    public void update(int value) {
        Node temp = head.next;
        //当temp.getValue()
        while (temp.getValue() != value) {
            temp = temp.next;
            if (temp == null) {
                System.out.println("没有找到");
                return;
            }
        }
        System.out.println("找到了" + temp.getValue());
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要修改的值为:");
        int gai = sc.nextInt();
        temp.setValue(gai);
        System.out.println("修改完成");
    }

    //显示队列
    public void show() {
        Node temp = head;//这里可以是头节点赋值, 也可以是头节点的next赋值
        if (temp.next == null) {
            System.out.println("队列空~~~");
            return;
        }
        while (temp.next != null) {//下一个结点不为null
            System.out.println(temp.next);
            temp = temp.next;
        }
    }

    //顺序添加
    public void addByOrder(Node node) {
        Node temp = head;
        boolean flag = false; //表示是否找到该节点
        while (true) {
            if (temp.next == null) {//遍历完
                break;
            }
            if (temp.next.getValue() > node.getValue()) {
                break;
            } else if (temp.next.getValue() == node.getValue()) {
                flag = true;//value值已经存在
                break;
            }
            temp = temp.next;//后移
        }
        //根据flag判断是否找到要修改的节点
        if (flag) {//不能添加,说明编号存在
            System.out.printf("准备插入的英雄的编号%d 已经存在,不能加入\n", node.getValue());
        } else {
            //插入到了链表中, temp的后面
            node.next = temp.next;
            temp.next = node;
        }

    }
}

class Node {
    private int value;
    public Node next;

    public Node() {
    }

    public Node(int value) {
        this.value = value;
    }

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

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值