Java 插入链表

Java 插入链表,java,arrays,linked-list,Java,Arrays,Linked List,10-->8-->1-->11-->空 您希望在数组中的哪个数字之后添加新数字 二, 空的 你想加什么号码 一, 给定的上一个节点不能为null 10-->8-->1-->11-->空 无法使开关正常工作,有人能帮我解决这个问题吗?谢谢。要使切换正常工作,请从案例中删除单引号,例如: public static void main (String[] args) { Scanner scan = new Scanner(System.in); ListNode hea

10-->8-->1-->11-->空

您希望在数组中的哪个数字之后添加新数字

二,

空的

你想加什么号码

一,

给定的上一个节点不能为null

10-->8-->1-->11-->空



无法使开关正常工作,有人能帮我解决这个问题吗?谢谢。

要使切换正常工作,请从案例中删除单引号,例如:

public static void main (String[] args)
{
    Scanner scan = new Scanner(System.in);
    
    ListNode head = new ListNode(10);
    ListNode second = new ListNode(8);
    ListNode third = new ListNode(1);
    ListNode fourth = new ListNode(11);

    head.next = second;
    second.next = third;
    third.next = fourth;

    System.out.println("\nAfter what digit of number in the array would you " 
            + "like to add a new number?");
    int pos = scan.nextInt();
    ListNode position = null;
    switch(pos)
    {
    case '0': position = head;break;
    case '1': position = second;break;
    case '2': position = third; break;
    case '3': position = fourth; break;
    }
    System.out.println(position);
    
    System.out.println("\nWhat number would you like to add?");
    int num3 = scan.nextInt();
    
    LL.insertAfter(position, num3);
    System.out.println();
    LL.display(head);
    }

我认为你不了解LinkedList是如何工作的。您正在尝试为每个节点初始化指针,这对于长列表是不可能的。使用开关盒在LinkedList中插入数字绝非易事

LinkedList由如下所述的节点组成

switch(pos)
    {
    case 0: position = head;break;
    case 1: position = second;break;
    case 2: position = third; break;
    case 3: position = fourth; break;
    }
比如说,我们想向LinkedList添加5个值

class ListNode {
    int val;
    ListNode next;

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
比如说,这个列表看起来像5->3->2->1->4->null

现在,用户希望在前两个节点之后输入一个数字

int numItems = 5;
ListNode head = null;

Scanner sc = new Scanner(System.in);

while(numItems != 0) {

    // take input from user
    int val = sc.nextInt();

    // list is currently empty
    if(head == null) 
        head = new ListNode(val, null);
    else {
        // go the last node in the list
        ListNode cur = head;
        while(cur.next != null)
            cur = cur.next;

        // cur now points to the last node in the list
        // can add new ListNode here
        cur.next = new ListNode(val, null);
    }
    numItems--;
}
cur
现在指向示例链接列表中的3

int insertAfter = 2;
// I am assuming the list contains > `insertAfter` elements
ListNode cur = head;
while(insertAfter > 1) {
    cur = cur.next;
    insertAfter--;
}
已创建一个新节点,该节点的下一个节点为2

ListNode newNode = new ListNode(102234, cur.next);
cur
现在指向
newNode

列表现在看起来像5->3->102234->2->1->4->null


如果您有任何澄清,请告诉我。

@DarshanMehta要插入的硬编码索引?!当我尝试在索引4之后添加时会发生什么?还有其他变量吗?索引6呢?另一个变量,我想?@DebosmitRay说得很好。然而,我并不完全了解这个计划的目的。OP问了一个关于开关不工作的具体问题,因此,我建议只解决这个问题:)@DarshanMehta,请在你的帖子中提及。这篇文章的标题是“在链表的指定位置后添加一个数字”。如果一个初学者读到这篇文章,他/她可能会认为为每个节点设置指针只是为了在某个索引之后进行插入是可以接受的!这不是我的帖子。@DebosmitRay这意味着我需要抛出一个异常来限制索引号的范围吗?使用开关盒在LinkedList中插入数字不是一个好办法。在下面寻找正确的方法。
ListNode newNode = new ListNode(102234, cur.next);
cur.next = newNode;