While循环

时间:2017-03-27 20:26:23

标签: java linked-list

我的Iterator和Deque方法有问题,而且这是我的代码:

import java.util.*;
import java.util.Iterator;





class Customer {
       public String lastName;
       public String firstName;
       public Customer() {
       }
       public Customer(String last, String first) {
          this.lastName = last;
          this.firstName = first;
       }
       public String toString() {
          return firstName + " " + lastName;
       }
    }
    class HourlyCustomer extends Customer {
       public double hourlyRate;
       public HourlyCustomer(String last, String first) {
          super(last, first);
       }
    }

class GenQueue<E> {
   private LinkedList<E> list = new LinkedList<E>();
   public ListIterator<E> iterator = list.listIterator();
   public void enqueue(E item) {
      list.addLast(item);
   }
   public E dequeue() {
      return list.poll();
   }
   public E show(){
       return list.peek();
   }
   public void printQueueElements(){

   }
   public E isNotEnd(){
       return list.getLast();
   }
   public boolean hasItems() {
      return !list.isEmpty();
   }
   public boolean isEmpty()
   {
       return list.isEmpty();
   }
   public Iterator<E>   iterator()
   {
    return iterator;       
   }


   public E removeFirst(){
       return list.removeFirst();
   }
   public E getFirst(){
       return list.getFirst();
   }
   public int size() {
      return list.size();
   }
   public boolean hasNext()
   {
    return false;

   }



   public void addItems(GenQueue<? extends E> q) {

      while (q.hasNext()) list.addLast(q.dequeue());
   }


}





public class Jerald {

    public static void main(String[] args){



        Scanner sc = new Scanner(System.in);
        String input1;
        String input2;
        int choice = 1000;




        GenQueue<Customer> empList;
        empList = new GenQueue<Customer>();
        GenQueue<HourlyCustomer> hList;
        hList = new GenQueue<HourlyCustomer>(); 


        while(true){

        do{

            System.out.println("================");
            System.out.println("Queue Operations Menu");
            System.out.println("================");
            System.out.println("1,Enquene");
            System.out.println("2,Dequeue");
            System.out.println("0, Quit\n");
            System.out.println("Enter Choice:");
            try{

                choice = sc.nextInt();



                switch(choice){
                case 1:

                    do{


                    System.out.println("\nPlease enter last name:  ");
                    input1 = sc.next();
                    System.out.println("\nPlease enter first name:  ");
                    input2 = sc.next();
                    hList.enqueue(new HourlyCustomer(input1, input2));
                    empList.addItems(hList);

                    System.out.println("\n"+(input2 + " " +  input1) + " is successful queued");

                    System.out.println("\nDo you still want to enqueue?<1> or do you want to view all in queue?<0> or \nBack to main menu for dequeueing?<menu>: ");
                    choice = sc.nextInt();

                    }while (choice != 0);



                     System.out.println("\nThe customers' names are: \n");

                     int numberOfElements = empList.size();
                     for (int i = 0; i < numberOfElements; i++) {
                       Customer emp = empList.dequeue();
                       System.out.println(emp.firstName + " " + emp.lastName + "\n");
                       empList.enqueue(emp);
                     }

                     break;






                case 2:


                    if (empList.isEmpty()) {
                        System.out.println("The queue is empty!");
                    }
                    else
                    {
                    System.out.println("\nDequeued customer: "  +empList.getFirst());
                    empList.removeFirst();
                    }

                    if (empList.isEmpty()) {
                        System.out.println("The queue is empty!");
                    }
                    else
                    {
                    System.out.println("\nNext customer in queue: "  +empList.getFirst()+"\n");
                }

                    break;

                case 0:

                    System.exit(0);



                default:
                        System.out.println("Invalid choice");
                }

            }

            catch(InputMismatchException e){
                System.out.println("Please enter 1-5, 0 to quit");
                sc.nextLine();
            }

        }while(choice != 0);
        }
    }
}

在第一种情况下,我试图让它检索队列中的所有元素并将其打印出来。没有从那里删除它们。所以基本上,不是使用我完成的while(hasItems)poll(),而是显示了我想要的输出,但它删除了列表中的所有内容,所以我提出了另一种方法并使用了hasNext方法,所以我将while(empLst.hasNext())element()方法一起使用,该方法仅检索但不删除。不幸的是,我在这方面失败了并且在许多输入之后得到了一个空结果,或者在给出一个输入之后得到了一个无限循环。我该如何解决?需要帮忙。我认为这是我的实施,但我想我已经检查过了。无论如何,我需要你的意见。

顺便提一下,在案例2中,我删除了链表的第一个元素,并显示了删除的链表的第一个元素。

2 个答案:

答案 0 :(得分:0)

在案例1中,在do-while循环之后,您想要打印队列中的所有元素并保留所有元素。

我看到了两种可能性:

1)在GenQueue中实现一种迭代内部列表并打印每个元素而不改变任何内容的方法:public void printQueueElements()。 这是推荐的解决方案。

2)而不是:

while (empList.hasNext()) {
  Customer emp = empList.dequeue();
  System.out.println(emp.firstName + " " + emp.lastName + "\n" );
}

使用:

int numberOfElements = empList.size();
for (int i = 0; i < numberOfElements; i++) {
  Customer emp = empList.dequeue();
  System.out.println(emp.firstName + " " + emp.lastName + "\n");
  empList.enqueue(emp);
}

这样队列重新获得了它的元素。

案例3的代码应为:

case 3:
  System.out.println("\nThe customers' names are: \n");
  Iterator<Customer> it = empList.iterator();

注意变量的类型&#34;它&#34;。此外,惯例是调用迭代器变量&#34; itr&#34;或&#34;迭代器&#34;。

答案 1 :(得分:0)

要摆脱无限循环问题,请删除外部while(true)循环。导致无限循环正是这些循环的作用,你不需要它来获得你想要的那种流。

我还在你的菜单中引入了第三种选择,它可以清除一些东西,但我想这取决于你。

就你的列表问题而言,修复GenQueue中的“dequeue”方法,使它实际出一个dequeue(删除列表中的第一个元素),然后你的案例2变得容易了。

要打印列表而不删除其元素,请使用迭代器。我删除了队列类中的ListIterator字段,因为您不需要它。实际上,无论如何,您已经为该类实现了大多数方法。看看吧 List API

class GenQueue<E> {

    private LinkedList<E> list = new LinkedList<E>();

    public void enqueue(E item) {
        list.addLast(item);
    }

    public E dequeue() {

        // return a customer with null values if empty? (up to you)
        if (list.isEmpty())
            return new Customer("","");
        else
            return list.remove(0);
    }

    public E isNotEnd(){

        return list.getLast();
    }

    public boolean hasItems() {

        return !list.isEmpty();
    }

    public boolean isEmpty() {

        return list.isEmpty();
    }

    public Iterator<E> iterator() {

        return list.iterator();
    }

    public E removeFirst() {

        return list.removeFirst();
    }

    public E getFirst() {

        return list.getFirst();
    }

    public int size() {

        return list.size();
    }

    public boolean hasNext() {

        return false;
    }

    public void addItems(GenQueue<? extends E> q) {

        while (q.hasNext()) list.addLast(q.dequeue());
    }
}


public class something {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String input1;
        String input2;
        int choice = 1000;

        GenQueue<Customer> empList;
        empList = new GenQueue<Customer>();
        GenQueue<HourlyCustomer> hList;
        hList = new GenQueue<HourlyCustomer>();

        do {

            System.out.println("================");
            System.out.println("Queue Operations Menu");
            System.out.println("================");
            System.out.println("1,Enquene");
            System.out.println("2,Dequeue");
            System.out.println("3,View queue");
            System.out.println("0, Quit\n");
            System.out.println("Enter Choice:");

            try {

                choice = sc.nextInt();

                switch(choice) {

                    case 1:

                        System.out.println("\nPlease enter last name:  ");
                        input1 = sc.next();
                        System.out.println("\nPlease enter first name:  ");
                        input2 = sc.next();
                        hList.enqueue(new HourlyCustomer(input1, input2));
                        empList.addItems(hList);

                        System.out.println("\n"+(input2 + " " +  input1) + " is successful queued");

                        break;

                    case 2:

                        System.out.println("Dequeued customer: " + empList.dequeue().toString());

                        break;

                    case 3:

                        System.out.println("\nThe customers' names are: \n");

                        Iterator<Genqueue<Customer>> it = empList.iterator();

                        while (it.hasNext()) {

                            Customer emp = it.next();
                            System.out.println(emp.firstName + " " + emp.lastName + "\n" );
                        }

                        break;

                    case 0:

                        System.exit(0);

                    default:

                        System.out.println("Invalid choice");
                }
            }
            catch(InputMismatchException e) {

                System.out.println("Please enter 1-5, 0 to quit");
                sc.nextLine();
            }

        } while(choice != 0);
    }
}