我正在尝试计算“餐厅等候名单”申请的总“等待时间”和平均等待时间。我无法想出那部分,我试图显示总数,但它会一直显示总数。我正在使用链表,并希望它在添加人员到候补名单时继续添加。我也不知道如何显示平均值,因为名称是一个字符串,如果我做总数/名称,它会给我一个错误。如果我将它解析为Int,它会给我一个错误。任何帮助,将不胜感激。当我单击以下按钮时,它应显示运行总计。
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
Random rand = new Random();
String name = txtName.getText();
int time = rand.nextInt(30) + 1;
int totaltime = time;
int partySize = Integer.parseInt(txtSize.getText());
txtTotal.setText(String.valueOf(totaltime) + " minutes until your table is ready.");
Highest = partySize;
String result = " Name: " + txtName.getText() + " Party Size: " + partySize + " Wait Time: " + time + " minutes.";
stack.enqueue(result);
Wait++;
}
private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {
String result;
int s;
int size = stack.size();
lstOutput.removeAll();
for (s = 0; s < size; s++) {
result = (String)stack.dequeue();
lstOutput.add(result);
}
}
stack是我的LinkedList。 LinkedListStack stack = new LinkedListStack();
这是LinkedListStack
public class LinkedListStack<E> implements QueueInterface<E> {
private LinkedList<E> list = new LinkedList<>(); // an empty list
public LinkedListStack() {
} // new queue relies on the initially empty list
public int size() {
return list.getSize();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void enqueue(E element) {
list.addLast(element);
}
public E first() {
return list.first();
}
public E dequeue() {
return list.removeFirst();
}
}
这是我的QueueInterface
public interface QueueInterface<E> {
int size();
boolean isEmpty();
// adds an item to the stack
void enqueue(E e);
// return but not remove the top item on the stack
E first();
// remove item at the top of the stack
E dequeue();
}
这是我正在使用的LinkedList。
public class LinkedList<E> {
private int size;
private Node<E> head;
private Node<E> tail;
// default constructor
public LinkedList() {
size = 0;
head = null;
tail = null;
}
// read-only property
public int getSize() {
return size;
}
public boolean isEmpty() {
// replaces an if/else
return (size == 0);
}
// return but not remove head of the list
public E first() {
if ( isEmpty() ) {
return null;
}
else {
return head.getElement();
}
}
public E last() {
if ( isEmpty() ) {
return null;
}
else {
return tail.getElement();
}
}
public void addFirst(E e) {
// create a new node and make it the new head of the list
head = new Node<>(e, head);
if (size == 0) {
tail = head; // special case first item in the list
}
size++;
}
public void addLast(E e) {
// create a new node and add to the tail of the list
Node<E> newest = new Node<>(e, null);
if (size == 0) { // special case for the first item
head = newest; // now head points to the new node
}
else {
tail.setNext(newest);
}
tail = newest;
size++;
}
public E removeFirst() {
if (isEmpty() ) {
return null;
}
else {
E tets = head.getElement();
head = head.getNext();
size--;
if (size == 0) {
tail = null; // list is now empty
}
return tets;
}
}
// nested class
public class Node<E> {
private E element;
private Node<E> next;
// custom constructor
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// get element
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> n) {
next = n;
}
} // end nested node class
} // end of LinkedList