[英]singly Linked List use setter & getter
我試圖運行一個單鏈列表,但是沒有給出任何輸出並且沒有錯誤。 我不知道問題出在哪里。 在類節點或測試類中這是實現...下面是節點類...
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
這是解決方案
希望這可以幫助
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.