双向链表

双向链表

插入

删除

代码

package com.linkedlist;


import java.util.Scanner;

/**
 * @program: DataStructures
 * @description: 双向链表
 * @author: XuDeming
 * @date: 2019-12-29 16:45:30
 **/
public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        System.out.println("***双向链表测试***");

        Scanner scanner = new Scanner(System.in);
        boolean flag = true;

        // 创建链表
        DoubleLinkedList list = new DoubleLinkedList();
        // 创建头节点
        Node2 head = new Node2(null);

        while (flag) {
            System.out.println("***双向链表操作菜单***");
            System.out.println("0: 退出程序");
            System.out.println("1: 正序打印链表");
            System.out.println("2: 显示链表长度");
            System.out.println("3: 添加数据");
            System.out.println("4: 插入数据");
            System.out.println("5: 查看数据(by位置)");
            System.out.println("6: 查看数据(by值)第一次出现");
            System.out.println("7: 查看数据(by值)最后一次出现");
            System.out.println("8: 删除数据(by位置)");
            System.out.println("9: 删除数据(by值)第一次出现");
            System.out.println("10: 删除数据(by值)最后一次出现");
            System.out.println("11: 更新数据");
            System.out.println("12: 清空链表");
            System.out.println("13:逆序打印链表");
            System.out.println("请输入操作数:");

            int i = scanner.nextInt();
            int index;
            String value;
            switch (i) {
                case 0:
                    flag = false;
                    break;
                case 1:
                    list.showList(head);
                    break;
                case 2:
                    System.out.println("链表长度:" + list.length(head));
                    break;
                case 3:
                    System.out.println("请输入添加的数据:");
                    value = scanner.next();
                    list.addNode(head, value);
                    break;
                case 4:
                    System.out.println("请输入插入的位置:");
                    index = scanner.nextInt();
                    System.out.println("请输入插入的数据:");
                    value = scanner.next();
                    list.addNodeByIndex(head, index, value);
                    break;
                case 5:
                    System.out.println("请输入查看的位置:");
                    index = scanner.nextInt();
                    Node2 node = list.findNodeByIndex(head, index);
                    if (node != null) {
                        System.out.println("该位置对应的值是" + node.value);
                    }
                    break;
                case 6:
                    System.out.println("请输入查看的值:");
                    value = scanner.next();
                    index = list.findFirstIndexByValue(head, value);
                    if (index > 0) {
                        System.out.println("该值第一次出现的位置是:" + index);
                    }
                    break;
                case 7:
                    System.out.println("请输入查看的值:");
                    value = scanner.next();
                    index = list.findLastIndexByValue(head, value);
                    if (index > 0) {
                        System.out.println("该值最后一次出现的位置是:" + index);
                    }
                    break;
                case 8:
                    System.out.println("请输入删除的位置:");
                    index = scanner.nextInt();
                    list.deleteNodeByIndex(head, index);
                    break;
                case 9:
                    System.out.println("请输入删除的值:");
                    value = scanner.next();
                    list.deleteFirstNodeByValue(head, value);
                    break;
                case 10:
                    System.out.println("请输入删除的值:");
                    value = scanner.next();
                    list.deleteLastNodeByValue(head, value);
                    break;
                case 11:
                    System.out.println("请输入更新的位置:");
                    index = scanner.nextInt();
                    System.out.println("请输入更新的值:");
                    value = scanner.next();
                    list.updateNodeByIndex(head, index, value);
                    break;
                case 12:
                    list.clean(head);
                    break;
                case 13:
                    list.resersetPrint(head);
                    break;
                default:
                    break;
            }
        }

    }

}
class Node2 {

    public String value;// 值
    public Node2 pre;// 指向上一个节点
    public Node2 next;// 指向下一个节点

    public Node2(String value) {
        this.value = value;
    }

}

// 双链表(带头节点)
class DoubleLinkedList {
    
    // 添加节点(最后一个节点的下一个位置)
    public void addNode(Node2 head, String value) {
        Node2 node = head;
        while (node.next != null) {
            node = node.next;
        }
        Node2 newNode = new Node2(value);
        node.next = newNode;
        newNode.pre = node;
    }

    // 插入节点(根据index)
    public void addNodeByIndex(Node2 head, int index, String value) {
        if (index == length(head) + 1) {// 最后一个节点的下一个位置
            addNode(head, value);
        } else {
            Node2 temp = findNodeByIndex(head, index);
            if (temp != null) {
                Node2 newNode = new Node2(value);
                newNode.next = temp;
                newNode.pre = temp.pre;
                temp.pre.next = newNode;
                temp.pre = newNode;
            }
        }
    }

    // 查找节点(根据index)
    public Node2 findNodeByIndex(Node2 head, int index) {
        if (head.next == null) {
            System.out.println("空链表");
            return null;
        }
        if (isNotIndex(head, index)) {
            System.out.println("查找位置有误");
            return null;
        }
        Node2 temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        return temp;
    }

    // 判断索引位置
    private boolean isNotIndex(Node2 head, int index) {
        return index < 1 || index > length(head);
    }

    // 查找节点(查找第一次出现value的位置)
    public int findFirstIndexByValue(Node2 head, String value) {
        if (value == null) throw new RuntimeException();
        if (head.next == null) {
            System.out.println("空链表");
            return 0;
        }
        int length = length(head);
        Node2 temp = head.next;
        int index = 1;
        for (int i = 0; i < length; i++) {
            if (value.equals(temp.value)) break;
            temp = temp.next;
            index++;

        }
        if (index > length) {
            System.out.println("查无此值");
            return -1;
        }
        return index;
    }

    // 查找节点(查找最后一次出现value的位置)
    public int findLastIndexByValue(Node2 head, String value) {
        if (value == null) throw new RuntimeException();
        if (head.next == null) {
            System.out.println("空链表");
            return 0;
        }
        int length = length(head);
        Node2 temp = findNodeByIndex(head, length);
        int index = length;
        for (int i = 0; i < length; i++) {
            if (value.equals(temp.value)) break;
            temp = temp.pre;
            index--;

        }
        if (index == 0) {
            System.out.println("查无此值");
            return -1;
        }
        return index;
    }

    // 删除节点(根据index)
    public void deleteNodeByIndex(Node2 head, int index) {
        Node2 delNode = findNodeByIndex(head, index);
        if (delNode != null) {
            if (index == length(head)) {// 最后一个节点
                delNode.pre.next = null;
            } else {
                delNode.next.pre = delNode.pre;
                delNode.pre.next = delNode.next;
            }
        }
    }

    // 删除节点(删除第一次出现value的节点)
    public void deleteFirstNodeByValue(Node2 head, String value) {
        int index = findFirstIndexByValue(head, value);
        if (index > 0) {
            deleteNodeByIndex(head, index);
        }
    }

    // 删除节点(删除第一次出现value的节点)
    public void deleteLastNodeByValue(Node2 head, String value) {
        int index = findLastIndexByValue(head, value);
        if (index > 0) {
            deleteNodeByIndex(head, index);
        }
    }

    // 修改节点(根据index)
    public void updateNodeByIndex(Node2 head, int index, String newValue) {
        Node2 node = findNodeByIndex(head, index);
        if (node != null) {
            node.value = newValue;
        }
    }

    // 正序打印链表
    public void showList(Node2 head) {
        if (head.next == null) {
            System.out.println("空链表");
            return;
        }
        Node2 node = head.next;
        while (node != null) {
            System.out.print(node.value + "\t");
            node = node.next;
        }
        System.out.println();
    }

    // 有效节点个数
    public int length(Node2 head) {
        if (head.next == null) return 0;
        Node2 temp = head.next;
        int size = 0;
        while (temp != null) {
            temp = temp.next;
            size++;
        }
        return size;
    }

    // 清空链表
    public void clean(Node2 head) {
        head.next = null;
    }

    // 逆序打印链表
    public void resersetPrint(Node2 head) {
        if (head.next == null) {
            System.out.println("空链表");
            return;
        }
        int length = length(head);
        Node2 node = findNodeByIndex(head, length);
        for (int i = 0; i < length; i++) {
            System.out.print(node.value + "\t");
            node = node.pre;
        }
        System.out.println();
    }
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值