LinkedList插入最后

时间:2016-12-29 01:07:52

标签: java data-structures linked-list insert

我正在尝试在链接列表insertAtEnd()的末尾插入元素。当我调试代码时,我看到在插入开始时默认插入node(0,null)。我认为这是在迭代列表时导致问题。关于如何解决这个问题的任何建议?

package com.ds.azim;

public class Node {
    //Node has 1. Data Element 2. Next pointer
    public int data;
    public Node next;
    //empty constructor 
    public Node(){
        //
    }
    public Node(int data){
        this.data= data;
        this.next = null;
    }
    public Node(int data, Node next){
        this.data = data;
        this.next = next;
    }
}

//*************************************//    

package com.ds.azim;

    public class SingleLinkedList {
        //Single Linked list has a head tail and has a length
        public Node head;
        public Node tail;
        public int length;
        //constructor
        public SingleLinkedList(){
            head = new Node();
            length = 0;
        }
        public void insertAtFirst(int data){
            head = new Node(data,head);
        }
        public void insertAtEnd(int data){
            Node curr = head;
            if(curr==null){
                insertAtFirst(data);
            }else{
                while(curr.next!=null){
                    curr = curr.next;
                }
                curr.next = new Node(data,null);
            }
        }
        public void show(){
            Node curr = head;
            while(curr.next!=null){
                //do something
                System.out.print(curr.data+",");
                curr = curr.next;
            }
        }
        public static void main(String[] args){
            SingleLinkedList sll = new SingleLinkedList();
            sll.insertAtFirst(12);
            sll.insertAtFirst(123);
            sll.insertAtFirst(890);
            sll.insertAtEnd(234);
            sll.show();


        }
    }

3 个答案:

答案 0 :(得分:0)

您的代码使用包含Node(0, null)的{​​{1}}初始化列表。要解决这个问题,请不要这样做。

head

同样在该代码中,您设置了public SingleLinkedList() { head = new Node(); length = 0; } ,但实际上长度为length = 0;。从构造函数中删除两个赋值。那么你将拥有一个零成员的结构,长度将是正确的。

答案 1 :(得分:0)

您有一个class SingleLinkedList { private Node head = null; private Node tail = null; public void addAtHead(int data) { if (head == null) { addFirst(data); } else { head.next = new Node(data, head.next); if (tail == head) tail = head.next; } } public void addAtTail(int data) { if (head == null) { addFirst(data); } else { assert tail != null; assert tail.next == null; tail.next = new Node(data); tail = tail.next; } } private void addFirst(int data) { assert head == null; assert tail == null; head = new Node(data); tail = head; } } 变量,该变量应指向列表中的最后一个节点。你应该保持最新:

tail

如果要删除class SingleLinkedList { private Node head = null; public void addAtHead(int data) { if (head == null) { head = new Node(data); } else { head.next = new Node(data, head.next); } } public void addAtTail(int data) { if (head == null) { head = new Node(data); } else { Node curr = head; while (curr.next != null) curr = curr.next; curr.next = new Node(data); } } } 变量,请:

;WITH cte
     AS (SELECT 1 AS fst_indicator, -- To identify the records from first query
                NULL as scd_indicator,
                TransactionNumber,..
         FROM   First_Query
         UNION ALL -- Change it to UNION if you really want to remove duplicates 
         SELECT NULL AS fst_indicator, 
                1 as  scd_indicator,-- To identify the records from second query
                TransactionNumber,..
         FROM   Second_Query),
     cte1
     AS (SELECT TransactionNumber
         FROM   cte
         GROUP  BY TransactionNumber
         HAVING Sum(fst_indicator) >= 1 -- to make sure TransactionNumber is present in first query
                AND Sum(scd_indicator) >= 1 -- to make sure TransactionNumber is present in second query
        )
SELECT *
FROM   cte c
WHERE  EXISTS (SELECT 1
               FROM   cte1 c1
               WHERE  c.TransactionNumber = c1.TransactionNumber) 

答案 2 :(得分:0)

除了删除这部分代码

public SingleLinkedList() {
  head = new Node();
  length = 0;
}

也改变你的节目功能,因为这不会打印最后一个元素

while(curr.next!=null){
                //do something
                System.out.print(curr.data+",");
                curr = curr.next;
            }

在此之后,再添加一个print语句来打印最后一个元素。

System.out.print(curr.data);

这将解决错误。