【问题标题】:How to display a string in linked list using nodes如何使用节点在链表中显示字符串
【发布时间】:2011-10-06 22:24:09
【问题描述】:

我创建了一个基本节点链表,以数字显示列表的大小(即:0 - 9) 现在我正在尝试更改我必须显示名称列表的内容。我对我需要改变什么以及将要改变什么感到困惑。名称将采用字符串格式。最终,我将从 txt 文件中读取名称列表。现在我只使用 3 个名称和测试数据。

       import java.util.*;

  public class Node {
public int dataitems; 
public Node next; 
Node front;

public void initList(){
    front = null;
}

public Node makeNode(int number){
    Node newNode;
    newNode = new Node();
    newNode.dataitems = number;
    newNode.next = null;
    return newNode;
}

public boolean isListEmpty(Node front){
    boolean balance;
    if (front == null){
        balance = true;
    }
    else {
        balance = false;
    }
    return balance;

}

public Node findTail(Node front) {
    Node current;
    current = front;
    while(current.next != null){
        //System.out.print(current.dataitems);
        current = current.next;

    } //System.out.println(current.dataitems);
    return current;
}

public void addNode(Node front ,int number){
    Node tail;
    if(isListEmpty(front)){
        this.front = makeNode(number);
    } 
    else {
        tail = findTail(front);
        tail.next = makeNode(number);
    }
}

public void printNodes(int len){

    int j;
    for (j = 0; j < len; j++){
        addNode(front, j);
    }  showList(front);
}

public void showList(Node front){
    Node current;
    current = front;
    while ( current.next != null){
        System.out.print(current.dataitems + " ");
        current = current.next;
    }
    System.out.println(current.dataitems);
}


public static void main(String[] args) {
     String[] names = {"Billy Joe", "Sally Hill", "Mike Tolly"}; // Trying to print theses names..Possibly in alphabetical order

    Node x = new Node();
     Scanner in = new Scanner(System.in);
        System.out.println("What size list? Enter Number: ");
    int number = in.nextInt();
     x.printNodes(number);
    } 

}

【问题讨论】:

    标签: java linked-list nodes


    【解决方案1】:

    在我看来,有几件事必须改变

     public void printNodes(String[] nameList){
    
        int j;
        for (j = 0; j < nameList.length; j++){
            addNode(front, nameList[j]);
        }  showList(front);
    }
    

    您必须传递包含名称的数组

    x.printNodes(names);
    

    也改变:

    public void addNode(Node front ,String name){
        Node tail;
        if(isListEmpty(front)){
            this.front = makeNode(name);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(name);
        }
    }
    

    和:

    public Node makeNode(String name){
        Node newNode;
        newNode = new Node();
        newNode.dataitems = name;
        newNode.next = null;
        return newNode;
    }
    

    别忘了把 dateitem 的类型改成字符串:

          import java.util.*;
    
      public class Node {
    public String dataitems;
    

    【讨论】:

    • 是的,这有效..感谢您的帮助。我已经做了这件事,但我将所有内容都更改为 string[],其中只有 printnodes 需要是 string[]。谢谢
    猜你喜欢
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多