我试图运行一个单链表,但是没有给出输出,并且没有错误。我不知道问题出在哪里。在类节点或测试类中这是实现...下面是节点类...
public class ListNode {
private int data;
private ListNode next;
public ListNode(int data)
{
this.data=data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
这里是列表方法中的代码
public class Linkedklist {
ListNode head;
private int length ;
public Linkedklist()
{
length=0;
}
public synchronized ListNode getHead()
{
return head;
}
public synchronized void insertAtBegin(ListNode node)
{
node.setNext(head);
head=node;
length++;
}
public int ListLength(ListNode headNode)
{
ListNode currentNode = headNode;
while(currentNode != null)
{
length++;
currentNode = currentNode.getNext();
}
return length;
}
最后是测试类:
Linkedklist t=new Linkedklist();
Scanner s=new Scanner(System.in);
t.insertAtBegin(2);
t.insertAtBegin(3);
t.insertAtBegin(10);
System.out.println("the head of the list: ");
t.getHead();
System.out.println("Enter the Element: ");
int e1=s.nextInt();
t.insertAtEnd(e1);
t.getHead();
System.out.println("=====length of the list : =====");
t.length();
我正在寻找许多程序来查找问题,但没有得到任何结果。 但我认为问题在于将数据传递给方法(ListNode节点)
问题是我不知道如何使用ListNode实例,我曾经为Node类编写代码,然后传递数据。
public Node(String a,double b)
{
ename=a;
salary=b;
next=null;
}
public Node(String a,double b,Node n)
{
ename=a;
salary=b;
next=n;
}
但是我在使用getter和setter和node实例时遇到困难。 谢谢StackOverflow的成员。
printlist()输出错误显示如下:WITH -2147483648不包括在列表元素中
-2147483648
60
50
40
30
20
10
-2147483648
答案 0 :(得分:1)
这是解决方案
希望这会有所帮助
ListNode.java
public class ListNode {
private int data;
private ListNode next;
public ListNode(int data) {
this.data = data;
next = null;
}
public int getData() { return data; }
public void setData(int data) { this.data = data; }
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
Linkedklist.java
public class Linkedklist {
ListNode head;
private int length;
public Linkedklist() {
head = null;
length = 0;
}
public synchronized ListNode getHead() { return head; }
public synchronized void insertAtBegin(ListNode node) {
node.setNext(head);
head = node;
length++;
}
public int getLength() { return length; }
public void insertAtEnd(ListNode node) {
ListNode curr, prev;
curr = head;
prev = null;
while (curr != null) {
prev = curr;
curr = curr.getNext();
}
if (prev == null) {
head = node;
head.setNext(null);
} else {
prev.setNext(node);
node.setNext(null);
}
length++;
}
public int computeLength() {
ListNode currNode = head;
int len = 0;
while (currNode != null) {
len++;
currNode = currNode.getNext();
}
return len;
}
public void printList() {
ListNode curr = head;
while (curr != null) {
System.out.println(curr.getData());
curr = curr.getNext();
}
}
}
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Linkedklist t = new Linkedklist();
Scanner s = new Scanner(System.in);
t.insertAtBegin(new ListNode(2));
t.insertAtBegin(new ListNode(3));
t.insertAtBegin(new ListNode(10));
t.insertAtEnd(new ListNode(4));
System.out.print("Enter a number: ");
int num = s.nextInt();
t.insertAtEnd(new ListNode(num));
System.out.println("List Entries: ");
t.printList();
System.out.println("Length of the list = " + t.getLength());
System.out.println("Computed length of the list = " + t.computeLength());
}
}