不将所有元素添加到链表中
Not adding all of the elements into the linkedlist
为什么我的 insertLast(T data)
方法没有将所有元素添加到列表中?
public class Tester {
public static void main(String[] args){
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.insertLast(1);
myList.insertLast(2);
myList.insertLast(3);
myList.insertLast(4);
myList.insertLast(5);
myList.insertLast(6);
myList.displayList();
}
}
它只增加了 6 个。代码可能有什么问题?
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data, Node<T> n){
this.data = data;
this.next = n;
}
public void display(){
System.out.print(this.data + " ");
}
}
class LinkedList<T> implements Iterable<T>{
private Node<T> head;
private int size;
public LinkedList(){
this.head = new Node<T>(null, null);
this.size = 0;
}
public boolean isEmpty(){
return (head.next == null);
}
public void displayList(){
Node<T> current = head;
while(current != null){
current.display();
current = current.next;
}
}
public void insert(T data){
head = new Node<T>(data, null);
size++;
}
public void insertLast(T data){
Node<T> newNode = new Node<T>(data, null);
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
else{
Node<T> current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
size++;
}
}
}
每次调用insertLast
,isEmpty
returns都是真的,因为head.next
是null
。 head.next
仅在 isEmpty
returns 为假时才设置为非空。
此项检查:
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
将 head 初始化为下一个 null
,因此 isEmpty() returns 每次调用时都为真。您需要在 isEmpty()
和构造函数中检查 head 本身是否为空,而不是:
this.head = new Node<T>(null, null);
您应该将其初始化为:
this.head = null;
Hai 对您的程序进行了细微更改。您不必将 head.next 初始化为 null
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data, Node<T> n){
this.data = data;
this.next = n;
}
public void display(){
System.out.print(this.data + " ");
}
}
class LinkedList<T> implements Iterable<T>{
private Node<T> head;
private int size;
public LinkedList(){
// this.head = new Node<T>(null, null);
this.size = 0;
}
public boolean isEmpty(){
return (head == null);
}
public void displayList(){
Node<T> current = head;
while(current != null){
current.display();
current = current.next;
}
}
public void insert(T data){
head = new Node<T>(data, null);
size++;
}
public void insertLast(T data){
Node<T> newNode = new Node<T>(data, null);
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
else{
Node<T> current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
size++;
}
}
public void forEach(Consumer<? super T> arg0) {
// TODO Auto-generated method stub
}
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
public Spliterator<T> spliterator() {
// TODO Auto-generated method stub
return null;
}
}
为什么我的 insertLast(T data)
方法没有将所有元素添加到列表中?
public class Tester {
public static void main(String[] args){
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.insertLast(1);
myList.insertLast(2);
myList.insertLast(3);
myList.insertLast(4);
myList.insertLast(5);
myList.insertLast(6);
myList.displayList();
}
}
它只增加了 6 个。代码可能有什么问题?
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data, Node<T> n){
this.data = data;
this.next = n;
}
public void display(){
System.out.print(this.data + " ");
}
}
class LinkedList<T> implements Iterable<T>{
private Node<T> head;
private int size;
public LinkedList(){
this.head = new Node<T>(null, null);
this.size = 0;
}
public boolean isEmpty(){
return (head.next == null);
}
public void displayList(){
Node<T> current = head;
while(current != null){
current.display();
current = current.next;
}
}
public void insert(T data){
head = new Node<T>(data, null);
size++;
}
public void insertLast(T data){
Node<T> newNode = new Node<T>(data, null);
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
else{
Node<T> current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
size++;
}
}
}
每次调用insertLast
,isEmpty
returns都是真的,因为head.next
是null
。 head.next
仅在 isEmpty
returns 为假时才设置为非空。
此项检查:
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
将 head 初始化为下一个 null
,因此 isEmpty() returns 每次调用时都为真。您需要在 isEmpty()
和构造函数中检查 head 本身是否为空,而不是:
this.head = new Node<T>(null, null);
您应该将其初始化为:
this.head = null;
Hai 对您的程序进行了细微更改。您不必将 head.next 初始化为 null
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data, Node<T> n){
this.data = data;
this.next = n;
}
public void display(){
System.out.print(this.data + " ");
}
}
class LinkedList<T> implements Iterable<T>{
private Node<T> head;
private int size;
public LinkedList(){
// this.head = new Node<T>(null, null);
this.size = 0;
}
public boolean isEmpty(){
return (head == null);
}
public void displayList(){
Node<T> current = head;
while(current != null){
current.display();
current = current.next;
}
}
public void insert(T data){
head = new Node<T>(data, null);
size++;
}
public void insertLast(T data){
Node<T> newNode = new Node<T>(data, null);
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
else{
Node<T> current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
size++;
}
}
public void forEach(Consumer<? super T> arg0) {
// TODO Auto-generated method stub
}
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
public Spliterator<T> spliterator() {
// TODO Auto-generated method stub
return null;
}
}