简体   繁体   English

Output 不会打印链表中的所有列表。 使用 java

[英]Output does not print all of the list in linked list. Using java

I tried to make a list of student registration system using linked list.我尝试使用链表制作学生注册系统列表。 So you can add names to the list but the problem is the output only print the first name that user add instead of all of them.因此,您可以将名称添加到列表中,但问题是 output 仅打印用户添加的名字而不是全部。

I tried to add name of Michael, Kate, and Rose.我尝试添加迈克尔、凯特和罗斯的名字。 but the output only displaying Michael.但 output 只显示迈克尔。

output: output:

*****STUDENT COURSE REGISTRATION SYSTEM***** *****学生课程注册系统*****

-------------------- MENU ----------------- - - - - - - - - - - 菜单 - - - - - - - - -

  1. ADD NEW STUDENT TO COURSE将新学生添加到课程中
  2. REMOVE/WITHDRAW ENROLLMENT删除/撤销注册
  3. DISPLAY STUDENT INFORMATION显示学生信息
  4. EDIT STUDENT DATA编辑学生数据
  5. SEARCH STUDENT IN THE COURSE搜索课程中的学生
  6. EXIT出口

Your choice:3您的选择:3

Student Name: Michael学生姓名:迈克尔

please help me to display all the name that I've added to the list.请帮我显示我添加到列表中的所有名称。 Thank you in advance.先感谢您。 Your help will be really helpful for me!你的帮助对我真的很有帮助!


public class LinkedList2nd {
    Node head; 

    static class Node 
{   
   // Data fields for Node   
   Object info;  // data stored in the node
   Node link;         // link to next node

   // Methods
   // Constructors
   // postcondition: Creates a new empty node.
   public Node() {
     info = null;
     link = null;

   }

   // postcondition: Creates a new node storing obj.
   public Node(Object obj) {
     info = obj;
     link = null;
   }

   // postcondition: Creates a new node storing obj 
   //   and linked to node referenced by next.
   public Node(Object obj, Node next) {
     info = obj;
     link = next;
   } 
   // accessors

   public Object getInfo() 
   {
      return info;
   }

   public Node getLink() 
   {
      return link;
   }


   // mutators
   public void setInfo(Object newInfo) 
   {
      info = newInfo;
   }

   public void setLink(Node newLink) 
   {
      link = newLink;
   }
} 

    public static LinkedList2nd insert(LinkedList2nd list,Object info){
        Node newNode = new Node(info);
        if(list.head == null){
            list.head = newNode;
        }
        else {
          //inserting last node 
            Node current = list.head;
           while(current.getLink() != null){
            current = current.getLink();
            current.setLink(newNode); }//while 

            }//else

        return list;
    }

    public static void printList(LinkedList2nd list){
        Node current = list.head; 

        System.out.println("Student Name\n--------------");

        while(current != null){
          System.out.println(current.getInfo() + " ");
          current = current.link;//to next node 

        }//while

        System.out.println("\n");
    }

}



//`enter code here`MAIN CLASS : 


public class Main {
//STUDENT COURSE REGISTRATION SYSTEM 
    static LinkedList2nd name = new LinkedList2nd();
    //static LinkedList matric = new LinkedList();
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int choice;
        do {
            menu();
            System.out.print("Your choice:");
            choice=sc.nextInt();
            if (choice==0){
              System.out.println("Thank you and Good Bye.");
            break;
              }
            else 
            processChoice(choice);
            } while (choice !=0);
          }//end main



    static void menu(){
        System.out.println("*****STUDENT COURSE REGISTRATION SYSTEM*****");
        System.out.println("********************************************");
        System.out.println("-------------------- MENU ------------------\n"
                        +"1. ADD NEW STUDENT TO COURSE\n"
                        +"2. REMOVE/WITHDRAW ENROLLMENT\n"
                        +"3. DISPLAY STUDENT INFORMATION\n"
                        +"4. EDIT STUDENT DATA\n"
                        +"5. SEARCH STUDENT IN THE COURSE\n"
                        +"0. EXIT");
          }//menu

    static void processChoice(int choice){
        switch (choice) {
            case 1:
                addStudent();
                break;

            case 3:
                displayList();
                break; 

            default:
                break;
        }    
    }

    static void addStudent(){
        Scanner read = new Scanner(System.in);
        System.out.println("Enter Student Name : ");
        String inputName = read.nextLine();
        name.insert(name, inputName);


    }

    static void displayList(){
        name.printList(name);

    }

    }//class 

In your current insert method, you are setting the link of all nodes to newNode.在您当前的插入方法中,您将所有节点的链接设置为 newNode。 You should move the set call out of the loop您应该将 set 调用移出循环

       while(current.getLink() != null){
        current = current.getLink();
        current.setLink(newNode); }//while 

        }//else

Corrected已更正

       while(current.getLink() != null){
        current = current.getLink();

        }//else
        current.setLink(newNode);

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

相关问题 删除循环链表中的所有匹配项。 (爪哇) - Deleting all occurrences in a circular linked list. (java) 数组存储在链表中。 (爪哇) - Array stored in a linked list. (Java) 使用Java传递指向链表的指针。 但是,当我修改内存/阵列中的数据时,链表中的数据并未更新。 为什么? - passing pointers to linked list using java. But when I modify the data in the memory/Array it's not updated in the Linked List. Why? 使用链接列表作为输出? - Using Linked List as the output? 将一个列表的所有元素/对象 N 次复制到新列表。 使用 java 8 stream - Copy All the elements/object of one list N times to new list. Using java 8 stream 制作一个链表,其中每个节点都连接到另一个链表。(java) - Making a linked list where each node is connected to another linked list.(java) java链表。该程序不打印8.为什么? - java linked list .This program does not print 8.Why? 如何在不使用 Java 递归的情况下打印反向链表? - How to print a reverse linked list without using recursion in Java? 使用递归反向打印单链接列表-Java - Print Singly linked list in reverse using recursion - Java 如何使用带链表的 java 迭代器打印数据 - How to print out data using java iterator with a linked list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM