0

这是情况。用户将通过用户输入在 team1 和 team2 中插入名称,然后程序将显示 TEAM1 和 TEAM2 的所有名称。我的显示方法有问题,因为它们只显示 Team1 中的名称。我想要做的是显示 team1 和 team2 中的所有名称。我是编程新手,我真的很难学习。到目前为止,这是我的代码。

     class Node
     {
       protected String info;
       protected Node next;
         public Node(String value)
         {
           info = value;
           next = null;
         }
      }

     class LinkedList
     {
        private Node head;
        private int count;

        public LinkedList()
        {
          head = null;
          count = 0;
        }

       public void insertteam1(String name)
       {
           Node b = new Node(name);
           b.next = null;
           count++;
           if (head == null)
           {
            head = b;
            return;
           }
           for(Node cur = head; cur != null; cur = cur.next)
           {
               if (cur.next == null)
               {
                 cur.next = b;
                 return;
               }

           }
       }

       public void insertteam2(String name)
       {
            Node c = new Node(name);
            c.next = null;
            count++;
            if (head == null)
            {
               head = c;
               return;
            }
            for(Node cur = head; cur != null; cur = cur.next)
            {
                if (cur.next == null)
                {
                    cur.next = c;
                    return;
                }

            }
         }
         public void displayTeamone()
         {
                for(Node cur = head; cur != null; cur = cur.next)
                    System.out.print(cur.info + " ");
                System.out.println();
         }
         public void displayTeamtwo()
         {
                for(Node cur = head; cur != null; cur = cur.next)
                    System.out.println(cur.info + " ");
                System.out.println();
         }
       }
4

1 回答 1

0

You are inserting the two teams into the same list. Use two head nodes,one for team1 and one for team2.

private Node head1;
private Node head2;

and reuse the insert function like:

public void insertteam(String name,String team)
{
  if(team.equals("team1")
   head = head1;
  else
   head = head2;
   //logic for insert
}
于 2014-03-12T05:39:45.500 回答