插入链接列表

时间:2016-03-03 01:29:23

标签: java arrays linked-list

    public void display(ListNode head)
{
   if(head == null)
       return;
   ListNode current = head;
   while (current != null){
       System.out.print(current.data + " -->");
       current = current.next;
   }
   System.out.print(current);

}

    public void insertAfter(ListNode previous, int data)
{
    if (previous == null){
        System.out.println("The given previous node cannot be null");
        return;}
    ListNode newNode = new ListNode(data);
    newNode.next = previous.next;
    previous.next = newNode;
    }
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);
    }
  

10 - > 8 - > 1 - > 11 - > null

     

您希望在数组中输入一个新数字的数字?

     

2

     

     

您想添加多少个号码?

     

1

     

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

     

10 - > 8 - > 1 - > 11 - > null

无法使开关正常工作,任何人都可以帮我解决这个问题吗?感谢。

2 个答案:

答案 0 :(得分:0)

要使swich工作,请从案例中删除单引号,例如:

switch(pos)
    {
    case 0: position = head;break;
    case 1: position = second;break;
    case 2: position = third; break;
    case 3: position = fourth; break;
    }

答案 1 :(得分:0)

我认为您不了解LinkedLists的工作原理。您正在尝试为每个节点初始化一个指针,这对于长列表是不可能的。使用switch case在LinkedList中插入数字是不可取的。

LinkedLists由如下所述的节点组成。

class ListNode {
    int val;
    ListNode next;

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

说,我们想要为LinkedList添加5个值。

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--;
}

说,列表看起来像这5 - > 3 - > 2 - > 1 - > 4 - > null

现在,用户想要在前2个节点之后输入一个数字。

int insertAfter = 2;
// I am assuming the list contains > `insertAfter` elements
ListNode cur = head;
while(insertAfter > 1) {
    cur = cur.next;
    insertAfter--;
}

cur现在指向示例链接列表中的3。

ListNode newNode = new ListNode(102234, cur.next);

创建了一个新节点,其下一个有2个。

cur.next = newNode;

cur现在指向newNode

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

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