队列方案帮助入门

时间:2015-06-07 21:53:04

标签: java list linked-list queue simulation

您好我正在进行队列等待时间同步,在12个小时的过程中,每分钟每行添加一个随机数,同时每分钟从前面移除三个。在十二个小时结束后,我将平均他们进入和退出生产线的速度。我需要执行50次以获得更准确的模型模拟。我目前不知道如何正确实现这一点。如果我能得到一些关于从哪里开始的指示,那将是非常感激的。 链接列表类

public class LinkedListQueue<E>{

private Node<E> head;
private Node<E> tail;
private int size;

public LinkedListQueue() {

}

public void enqueue(E element) {
    Node newNode = new Node(element, null);

    if (size == 0) {
        head = newNode;
    } else {
        tail.setNextNode(newNode);
    }

    tail = newNode;
    size++;
}

public E dequeue() {
    if (head != null) {
        E element = head.getElement();
        head = head.getNextNode();
        size--;
        if (size == 0) {
            tail = null;
        }
        return element;
    }
    return null;
}

public E first() {
    if (head != null) {
        return head.getElement();
    }
    return null;
}

public int getSize() {
    return size;
}

public void print() {
    if (head != null) {
        Node currentNode = head;
        do {
            System.out.println(currentNode.toString());
            currentNode = currentNode.getNextNode();
        } while (currentNode != null);
    }
    System.out.println();
}
}

节点类

public class Node<E>{
private E element;
private Node<E> next;

public Node(E element, Node next) {
    this.element = element;
    this.next = next;
}

public void setNextNode(Node next) {
    this.next = next;
}

public Node<E> getNextNode() {
    return next;
}

public E getElement() {
    return element;
}

public String toString() {
    return element.toString();
}
}

模拟课程

import java.util.Random;

public class Simulation {

private int arrivalRate;
//you'll need other instance variables

public Simulation(int arrivalRate, int maxNumQueues) {
    this.arrivalRate = arrivalRate;
}

public void runSimulation() {
    //this is an example for using getRandomNumPeople
    //you are going to remove this whole loop.
    for (int i = 0; i < 10; i++) {
        int numPeople = getRandomNumPeople(arrivalRate);
        System.out.println("The number of people that arrived in minute " + i + " is: " + numPeople);
    }
}

//Don't change this method.
private static int getRandomNumPeople(double avg) {
    Random r = new Random();
    double L = Math.exp(-avg);
    int k = 0;
    double p = 1.0;
    do {
        p = p * r.nextDouble();
        k++;
    } while (p > L);
    return k - 1;
}

//Don't change the main method.
public static void main(String[] args) {
    Simulation s = new Simulation(18, 10);
    s.runSimulation();
}
}

1 个答案:

答案 0 :(得分:0)

看起来你根本没有开始这项任务。

首先,从main()方法开始。创建一个新的Simulation对象。按照构造函数调用new simulation(18,10)。对于初学者,您将看到构造函数不完整

public Simulation(int arrivalRate, int maxNumQueues) {
    this.arrivalRate = arrivalRate;
    // missing the handling of maxNumQueues
}

因此,对于初学者,您可能希望在Simulation类中定义一个整数类型的新变量(因为这是根据Simulation构造函数的maxNumQueues的类型)。从那里,你显然想要回到构造函数并设置你的新变量来引用构造函数调用。

示例:

public class Simulation {

private int arrivalRate;
private int maxNumQueues; // keep track of the maxNumQueues

public Simulation(int arrivalRate, int maxNumQueues) {
    this.arrivalRate = arrivalRate;
    this.maxNumQueues = maxNumQueues; // initialize our new local variable maxNumQueues
}}