单 Linklist 使用 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 以及节点实例方面遇到困难。 谢谢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();
}
}
}


主.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());
}
}


相关内容

  • 没有找到相关文章

最新更新