队列场景帮助入门



您好,我目前正在研究一个队列等待时间模拟,在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();
}
}

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

首先,从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
}}

相关内容

  • 没有找到相关文章

最新更新