我需要编写一个Java程序(类LinkedList-Queue),它应该在不使用导入列表的情况下执行所有操作,我已经尝试但是我无法创建推送方法而且需要帮助。
有人可以帮帮我吗? 你可以在我的github帐户中找到我的所有课程: https://github.com/mertowski/Fortgeschrittene-Programmierung/tree/master/012.%20Aufgabe%204%20Generische%20Queue
这是我的班级:
package main.java.a4;
//First In First Out
public class Queue<K> {
QueueNode first;
QueueNode last;
private int size = 0;
//-------------------Innere Klasse------------------------------
private class QueueNode {
K data;
QueueNode next;
public QueueNode(K data, QueueNode next) {
this.data = data;
this.next = next;
}
public void setData() {
this.data = data;
}
public void setNext() {
this.next = next;
}
}
//-------------------Innere Klasse------------------------------
//(Methode zum Einfügen eines Elements in die Queue)
public void push(K element) {
if(element == null) {
throw new NullPointerException("Element was null!");
}
if(first == null) {
//here
} else {
//and here
}
}
//(Methode zum herausnehmen eines Elements aus der Warteschlange)
public K pull() {
if (first == null) {
throw new NullPointerException("Queue was empty!");
} else {
QueueNode temp = first;
first = first.next;
if(size == 1) {
last = null;
}
size--;
return temp.data;
}
}
//(Gibt die Anzahl der in der Liste gespeicherten Elemente zurück)
public int size() {
return size;
}
public String toString() {
QueueNode temp = first;
StringBuilder sb = new StringBuilder();
while (temp != null) {
sb.append("<" + temp.data + ">");
temp = temp.next;
}
return sb.toString();
}
public boolean isEmpty() {
if(first == null) {
return true;
} else {
return false;
}
}
}
这是我的测试类J-Unit:
package test.java.a4;
import main.java.a4.Account;
import main.java.a4.AccountDetails;
import main.java.a4.Euro;
import main.java.a4.Queue;
import main.java.a4.Transaction;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class QueueTest {
private Queue<Transaction> transactions;
private Transaction transaction1;
private Transaction transaction2;
private Transaction transaction3;
//Method is called before every test method.
@Before
public void setUp() {
this.transactions = new Queue<>();
Account<Euro> acc1 = new Account<>(new AccountDetails("10001", "Bank1"), 20000d, 50000d, new Euro(1, "EUR"), 123);
this.transaction1 = new Transaction(acc1, "10002", 20000);
Account<Euro> acc2 = new Account<>(new AccountDetails("10002", "Bank1"), 20000d, 50000d, new Euro(1, "EUR"), 123);
this.transaction2 = new Transaction(acc2, "10001", 30000);
Account<Euro> acc3 = new Account<>(new AccountDetails("10004", "Bank1"), 20000d, 50000d, new Euro(1, "EUR"), 123);
this.transaction3 = new Transaction(acc3, "10006", 50000);
}
//try to push null
@Test(expected = NullPointerException.class)
public void testPushNullElement() {
this.transactions.push(null);
}
//test if the queue is empty
@Test
public void testQueueIsEmpty() {
assertEquals(true, transactions.isEmpty());
}
//test if the size is zero when the queue is empty
@Test
public void testQueueSizeZero() {
assertEquals(0, transactions.size());
}
//try to push one element
@Test
public void testPushFirstElement() {
transactions.push(transaction1);
assertEquals("<10002>", transactions.toString());
}
//test if the size is one after an element was pushed
@Test
public void testQueueSizeOne() {
transactions.push(transaction1);
assertEquals(1, transactions.size());
}
//try to push two elements
@Test
public void testPushSecondElement() {
transactions.push(transaction1);
this.transactions.push(transaction2);
assertEquals("<10002><10001>", this.transactions.toString());
}
//test if the size is two after two elements were pushed
@Test
public void testQueueSizeTwo() {
transactions.push(transaction1);
transactions.push(transaction2);
assertEquals(2, transactions.size());
}
//try to push three elements
@Test
public void testPushThirdElement() {
transactions.push(transaction1);
transactions.push(transaction2);
transactions.push(transaction3);
assertEquals("<10002><10001><10006>", this.transactions.toString());
}
//test if the size is three after three elements were pushed
@Test
public void testQueueSizeThree() {
transactions.push(transaction1);
transactions.push(transaction2);
transactions.push(transaction3);
assertEquals(3, transactions.size());
}
//add three elements and try to pull the first one
@Test
public void testQueuePullFirstTransaction() {
transactions.push(transaction1);
transactions.push(transaction2);
transactions.push(transaction3);
assertEquals(transaction1.getTargetIban(), transactions.pull().getTargetIban());
}
//add two elements and try to pull the first one
@Test
public void testQueuePullSecondTransaction() {
transactions.push(transaction2);
transactions.push(transaction3);
assertEquals(transaction2.getTargetIban(), transactions.pull().getTargetIban());
}
//add one element and try to pull it
@Test
public void testQueuePullThirdTransaction() {
transactions.push(transaction3);
assertEquals(transaction3.getTargetIban(), transactions.pull().getTargetIban());
}
}
答案 0 :(得分:2)
此代码应该这样做
public void push(K element) {
if (element == null) {
throw new NullPointerException("Element was null!");
}
QueueNode newNode = new QueueNode(element, first);
if (first == null) {
last = newNode;
}
first = newNode;
}