2011-10-18 17 views
1

一直在爲分配的鏈接列表隊列和堆棧類實現。我已經測試了這兩種方法,並且他們看起來還在工作,但我擔心執行的某些部分可能比我目前設置的更好,並且我不想爲低效的代碼取消積分。實現鏈接列表堆棧和隊列,我不知道我設置的代碼是否足夠有效

這是我已經設置了類:

節點

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循環設置的那些,我不確定是否有更高效的內存執行方式。有沒有一種更加標準化的方式來設置我可能錯過的?

+1

考慮到這個[codereview.stackexchange.com](http://codereview.stackexchange.com/) – finnw

回答

3

我會改變的一件事是我會讓LQueue保持對隊列頭部和尾部的引用。通過這種方式,您可以在不變的情況下持續使用insert(),而無需遍歷所有現有元素。

此外,如果您願意,可以使兩個size()方法在恆定時間內運行:跟蹤成員變量中的當前大小,在insert()上增加並在remove()上遞減。

4

的只有兩件事我能想到的是:

  1. 跟蹤列表中的元素的數量與上增加和遞減上刪除遞增一個int。這將使size()方法即時。你所要做的就是返回那個int值。

  2. 對於隊列,使用Node引用跟蹤尾部,就像您做頭部一樣。這樣你就不必遍歷列表來找到列表的末尾。尾巴將始終是最後添加的節點。這將允許您不必在每次添加內容時遍歷列表;相反,您可以將其添加到該尾部的末尾。

相關問題