[英]Java driver class is not working with linkedList
My line 10 in my driver and line 19 in my linkedList class are not working together. 我的驱动程序中的第10行和我的linkedList类中的第19行不能一起工作。 I can't find the problem but I think it has something to do with the declaration.
我找不到问题,但我认为这与声明有关。 Any help?
有什么帮助吗?
public class Driver
{
public static void main(String[] args)
{
LinkedList linkedList = new LinkedList();
linkedList.add(3);
linkedList.add(7);
linkedList.add(12);
linkedList.printList();
linkedList.add(2);
linkedList.add(6);
linkedList.add(10);
linkedList.add(13);
linkedList.printList();
}
}
and the linkedList class 和linkedList类
package part2;
public class LinkedList
{
Node head;
int size;
public LinkedList()
{
head = null;
System.out.println("LinkedList created...");
}
public void add (int data)
{
Node current = head;
Node newNode = new Node (data);
while(current.next != null && current.next.data < newNode.data)
{
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
System.out.println("LinkedList.add(), element: " +data+ "added. Element # "+size+ "...");
}
public void printList()
{
Node current = head;
while(current.next != null)
{
System.out.println("LinkedList.printList(), element: " +current.data + "...");
current = current.next;
}
System.out.println("LinkedList.printList(), element: " +current.data + "...");
}
public void delete( int dataToRemove)
{
for(Node current = head; current.next != null; current = current.next)
{
if (current.next.equals(dataToRemove))
{
current.next = current.next.next;
}
}
}
private class Node
{
public Node(int data)
{
this.data= data;
this.next = null;
}
Node next;
int data;
}
}
You need to initialize the head node when the first node is added. 添加第一个节点时,需要初始化头节点。 Otherwise the head node is always null.
否则,头节点始终为null。 For example:
例如:
public class LinkedList {
Node head;
int size;
public LinkedList() {
head = null;
System.out.println("LinkedList created...");
}
public void add(int data) {
Node current = head;
Node newNode = new Node(data);
size++;
if (head == null) {
// Init the head node first
head = newNode;
System.out.println("LinkedList.add(), element: " + data + "added. Element # " + size + "...");
return;
}
while (current.next != null && current.next.data < newNode.data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
System.out.println("LinkedList.add(), element: " + data + "added. Element # " + size + "...");
}
public void printList() {
Node current = head;
while (current.next != null) {
System.out.println("LinkedList.printList(), element: " + current.data + "...");
current = current.next;
}
System.out.println("LinkedList.printList(), element: " + current.data + "...");
}
public void delete(int dataToRemove) {
for (Node current = head; current.next != null; current = current.next) {
if (current.next.data == dataToRemove) {
current.next = current.next.next;
size--;
break;
}
}
}
And your delete method is also not correct. 而且您的删除方法也不正确。 You need to compare the data value with given data instead of comparing the node with given data.
您需要将数据值与给定数据进行比较,而不是将节点与给定数据进行比较。 I also made a change for this.
我对此也进行了更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.