简体   繁体   中英

Queue not deleting elements (using linkedlist) [java]

I'm trying to get the hang of queues by making a very simple program that simulates a checkout line. For some reason my custom dequeue function (using a linkedlist's removeFirst()) wont actually delete stuff in the queue.

Heres the CustomerQueue Class

public class CustomerQueue<E> {   

private LinkedList<E> list = new LinkedList<>();  
public void enqueue(E e) { list.addLast(e); }   
public E dequeue() { return list.removeFirst(); }  
public int size() { return list.size(); } 
public E element() { return list.getFirst(); }

@Override    
public String toString() { return "Queue: " + list.toString(); }  

 }

Here is my main:

public static void main(String[] args) {

    CustomerQueue<Customer> queue = new CustomerQueue<>();

    final int totalTime = 720;
    int totalServiced = 0;
    int totalQueued = 0;
    int totalArrived = 1;
    int maxQueuedAtOnce = 0;
    Customer next = new Customer();

    queue.enqueue(next);
    System.out.println(next.getATime() + " <-- ATIME STIME --> " + next.getSTime());


    for (int minute = 0; minute < totalTime; minute++) {
        System.out.println("-- Minute " + (minute + 1) + " ---------");

        if (queue.element().getSTime() == 0) {
            totalServiced++;
            System.out.println("Current customer (" + queue.dequeue() + ") finished being serviced. (Removed)");
            //queue.dequeue();
        }

        if (next.getATime() == 0) {
            System.out.println("Customer ID=" + next.getID() + " has arrived.");
            queue.enqueue(next);
            System.out.println("Customer ID=" + next.getID() + " in queue.");
            next = new Customer();
            System.out.println("New Customer generated. ID=" + next.getID() + " ATIME: " + next.getATime() + " STIME: " + next.getSTime());
            totalArrived++;
            totalQueued++;
        }
        System.out.println("Customer ID=" + next.getID() + " arrival ticked down.");
        next.tickArrivalDown();
        queue.element().tickServiceDown();
        System.out.println("Current queue customer ID=" + next.getID() + " ticked down.");

        if (queue.size() > maxQueuedAtOnce)
            maxQueuedAtOnce = queue.size();
    }

    System.out.println("Total Customers Generated " + next.getNumCustomers());
    System.out.println("Total Customers Serviced: " + totalServiced);
    System.out.println("Total Customers Arrived: " + totalArrived);
    System.out.println("Maximum Customers Queued: " + maxQueuedAtOnce);
    System.out.println(queue.element());
}   

And of course the Customer Class:

public class Customer {
private int serviceTime;
private int arrivalTime;
private static int numCustomers = 0;
private int ID;

public int getSTime() { return serviceTime; }
public void setSTime(int t) { serviceTime = t; } 
public int getATime() { return arrivalTime; }
public void setATime(int t) { arrivalTime = t; } 
public int getID() { return ID; }
public static int getNumCustomers() { return numCustomers; }

Customer() {
    serviceTime = (int) (Math.random()*3 + 1);
    arrivalTime = (int) (Math.random()*3 + 1);
    ID = ++numCustomers;
}

public void tickServiceDown() { serviceTime--; }
public void tickServiceUp() { serviceTime++; }

public void tickArrivalDown() { arrivalTime--; }
public void tickArrivalUp() { arrivalTime++; }

@Override
public String toString() {
    return "ID: " + ID + " ArrivalTime: " + arrivalTime + " ServiceTime: " + serviceTime;

}
}

The Customer class I have setup generates its own arrival time and completion time when a Customer is instantiated. Basically at the end of the loop, the next customer's arrival time ticks down and the current customer's service time ticks down. When the current customer's service time hits 0, it should dequeue/remove the first element in the LinkedList. For some reason it isnt removing it. Here is the post-loop output:

Total Customers Generated 358
Total Customers Serviced: 1
Total Customers Arrived: 358
Maximum Customers Queued: 357
ID: 1 ArrivalTime: 0 ServiceTime: -717

I'm totally stumped, no amount of googling has helped.

Looking at your output

ID: 1 ArrivalTime: 0 ServiceTime: -717

and ServiceTime being what is being returned in getSTime()

then as 0 != -717 I think the below will be fine

How about if (queue.element().getSTime() <= 0) {

I figured out the answer. The initial if statement:

if (queue.element().getSTime() == 0) needed to become if (queue.size() > 0 && queue.element().getSTime() == 0)

Also needed to make the queue.element().tickServiceDown(); enclosed with the following if-statement if (queue.size() > 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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