简体   繁体   English

Java中的单链列表存在一些问题

[英]Having some issues with singly linked list in java

I am attempting to create a singly linked list where I can add nodes that will have an integer value and a next value. 我试图创建一个单链列表,可以在其中添加将具有整数值和下一个值的节点。 I need to be able to traverse this list so I can add the elements of the list. 我需要能够遍历此列表,以便可以添加列表的元素。 Everything needs to be within one file. 一切都必须放在一个文件中。 I am new to java and could use some assistance. 我是Java新手,可以使用一些帮助。 Here is my code: 这是我的代码:

package LargestSum;
import java.io.*;
import java.util.Scanner;

public class LargestSum {
   public static class LinkedList {

private int num;
private LinkedList node;    
private LinkedList head;
private LinkedList tail;
private int listSize;
public LinkedList next;

public LinkedList(){

    node = null;
    num = 0;
    head = null;
    tail = null;
    listSize = 0;
}

public void setLink(LinkedList l){
    node = l;
}

public void setNum(int n){
    num = n;
}

public LinkedList getNode(){
    return node;
}
public int getNum(){
    return num;
} 

public boolean empty(){
    return head == null;
}

public int getListSize(){
    return listSize;
}

public void insert(int set){
    LinkedList list = new LinkedList();
    listSize++;
    if(head == null){
        head = list;
        tail = head;

    }
    else {
        tail.setLink(list);
        tail = list;
    }
 }
 }

public static void main(String[] args)  throws IOException {
     String fileName = "in.txt";
     LinkedList list = new LinkedList();
     Scanner numbers = new Scanner(new File(fileName));
     while(numbers.hasNext()){
        int num = numbers.nextInt();
        list.insert(num);
        System.out.println(num);
     } 
     int listSize = list.getListSize();
} 
}

I'd appreciate any type of help and pointers, please. 请您提供任何类型的帮助和指示。 Thank you. 谢谢。

Problem in your code is you are initializing linkedlist every time you add an element. 代码中的问题是每次添加元素时都要初始化链表。 so every node are not linked with each other. 因此,每个节点都不相互链接。 what you can do is make two classes: 1) node and 2) LinkedList 您可以做的是创建两个类:1)节点和2)LinkedList

node will act has container element which denote element in LinkedList. 节点将具有表示LinkedList中的元素的容器元素。 LinkedList class will have all the functionality which you want to do. LinkedList类将具有您要执行的所有功能。

I have made changes in your code instead pasting whole new code. 我已经对您的代码进行了更改,而不是粘贴了整个新代码。 so that you can understand difference between them by comparing your code with it. 这样您就可以通过将代码与其进行比较来了解它们之间的区别。

//package LargestSum;
import java.io.*;
import java.util.Scanner;

public class Test {

    public static class node{
        int num;
        node next;

        public node(int num){
            this.num = num;
            next = null;
        }
        public node getNext(){
            return next;
        }
        public void setNext(node t){
            this.next = t;
        }
        public int getNum(){
            return this.num;
        }
    }


   public static class LinkedList {

//private int num;
//private LinkedList node;    
private node head;
private node tail;
private int listSize;
//public LinkedList next;

public LinkedList(){

//    node = null;
//    num = 0;
    head = null;
    tail = null;
    listSize = 0;
}

//public void setLink(LinkedList l){
//    node = l;
//}

//public void setNum(int n){
//    num = n;
//}

//public LinkedList getNode(){
//    return node;
//}
//public int getNum(){
//    return num;
//} 

public boolean empty(){
    return head == null;
}

public int getListSize(){
    return listSize;
}

public void print(){
    node traverse = head; 
    while(traverse!=null){
        System.out.println(traverse.getNum());
        traverse = traverse.getNext();
    }

}

public void insert(int set){
  //  LinkedList list = new LinkedList();
  node temp = new node(set);

    listSize++;
    if(head == null){       
        head = temp;
        tail = temp;
    }
    else {
        tail.setNext(temp);
        tail = tail.getNext();
    }
 }
 }

public static void main(String[] args)  throws IOException {
    // String fileName = "in.txt";
     LinkedList list = new LinkedList();
     Scanner numbers = new Scanner(System.in);
     int x=10;
     while(x >0){
        int num = numbers.nextInt();
        list.insert(num);
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
        list.print();
        x--;
     } 
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
        list.print();
     int listSize = list.getListSize();
} 
}

Here, I have implemented very basic one. 在这里,我实现了非常基本的一种。 you can't add other functionality in this code directly. 您不能直接在此代码中添加其他功能。 if you want to add other one just use this type of two classes as I used here and add functions in LinkedList class. 如果要添加其他类,只需使用我在此使用的两种此类,然后在LinkedList类中添加函数。

If you have any further query comment me. 如果您还有其他疑问,请评论我。

Thanks, 谢谢,

Bhavik Bhavik

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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