一直在爲分配的鏈接列表隊列和堆棧類實現。我已經測試了這兩種方法,並且他們看起來還在工作,但我擔心執行的某些部分可能比我目前設置的更好,並且我不想爲低效的代碼取消積分。實現鏈接列表堆棧和隊列,我不知道我設置的代碼是否足夠有效
這是我已經設置了類:
節點
public class Node {
Node next;
Car car;
/**
* A node object, used for the creation of LinkedLists.
* @param car
*/
public Node(Car car)
{
next = null;
this.car = car;
}
}
堆棧
public class LStack {
Node head = null;
/**
* Adds a car object to the list
* @param car = the car object to be added
*/
public void push(Car car)
{
Node oldHead = head;
head = new Node(car);
head.next = oldHead;
}
/**
* Removes the top car from the list
* @return the car at the top of the list
*/
public Car pop()
{
Car headCar = head.car;
head = head.next;
return headCar;
}
/**
* Checks if the list is empty
* @return whether or not the list is empty
*/
public boolean isEmpty()
{
return (head == null);
}
/**
*
* @return the size of the list
*/
public int size()
{
Node nextNode = head;
int count = 0;
while (nextNode != null)
{
count++;
nextNode = nextNode.next;
}
return count;
}
/**
* Displays the list of car license plate information
*/
public void display()
{
Node nextNode = head;
while (nextNode != null)
{
System.out.print(nextNode.car.getLicense() + "|");
nextNode = nextNode.next;
}
System.out.println();
}
/**
*
*/
public void reverseStack()
{
// not implemented yet
}
}
隊列
public class LQueue {
Node head = null;
/**
* Adds a car object to the list
*
* @param car
* = the car object to be added
*/
public void insert(Car car) {
Node current = head;
if (current != null) {
while (current.next != null) {
current = current.next;
}
current.next = new Node(car);
}
else
{
head = new Node(car);
}
}
/**
* Removes the top car from the list
*
* @return the car at the top of the list
*/
public Car remove() {
Car headCar = head.car;
head = head.next;
return headCar;
}
/**
* Checks if the list is empty
*
* @return whether or not the list is empty
*/
public boolean isEmpty() {
return (head == null);
}
/**
*
* @return the size of the list
*/
public int size() {
Node nextNode = head;
int count = 0;
while (nextNode != null) {
count++;
nextNode = nextNode.next;
}
return count;
}
/**
* Displays the list of car license plate information
*/
public void display() {
Node nextNode = head;
while (nextNode != null) {
System.out.print(nextNode.car.getLicense() + "|");
nextNode = nextNode.next;
}
System.out.println();
}
/**
*
*/
public void reverseQueue() {
}
}
和汽車類是不是真的很重要,它只是st礦石牌照信息中的字符串。
大多數情況下,我很擔心我用While循環設置的那些,我不確定是否有更高效的內存執行方式。有沒有一種更加標準化的方式來設置我可能錯過的?
考慮到這個[codereview.stackexchange.com](http://codereview.stackexchange.com/) – finnw