gpt4 book ai didi

java - 单链表使用 setter 和 getter

转载 作者:行者123 更新时间:2023-12-02 10:43:28 25 4
gpt4 key购买 nike

我试图运行一个单链表,但没有给出输出并且没有错误。我不知道问题出在哪里。在类节点或测试类中这是实现...下面是节点类...

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


关于java - 单链表使用 setter 和 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52766720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com