简体   繁体   English

单链表使用setter和getter

[英]singly Linked List use setter & getter

I was trying to run a singly linked list, but no output is given and without error. 我试图运行一个单链列表,但是没有给出任何输出并且没有错误。 I don't know where is the problem. 我不知道问题出在哪里。 in the class node or the test class Here is the implementation... Below is the node class... 在类节点或测试类中这是实现...下面是节点类...

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

and here my code in list methods 这是列表方法中的代码

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

finally the test class: 最后是测试课:

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

I'm looking at many programs to find the problem but I didn't get any results. 我正在寻找许多程序来查找问题,但没有得到任何结果。 but I think the problem is in pass data to the method (ListNode node) 但我认为问题在于将数据传递给方法(ListNode节点)

the proble is I don't know how to work with ListNode instance, I used to write code for the class Node Like this then pass data. 问题是我不知道如何使用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;
}

but I face difficulty with getter and setter and node instance. 但是我在使用getter和setter和node实例时遇到困难。 thank you StackOverflow members. 谢谢StackOverflow的成员。

error with printlist() output appears like this: WITH -2147483648 not include in list element printlist()输出错误显示如下:WITH -2147483648不包括在列表元素中

 -2147483648
 60
 50
 40
 30
 20
 10
 -2147483648

Here's the solution 这是解决方案
Hope this helps 希望这可以帮助

ListNode.java 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 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 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM