2013-02-18 93 views
-1

這是在課堂上的作業,我在我的鏈接列表中使用我的車對象時遇到了問題。 汽車類有兩個實例變量(make,price)。 我將如何將變量放入節點類中。如何使用Java中的對象創建鏈接列表。

public class CarList { 
private CarNode head = null; 
private CarNode tail = null; 
private int counter = 0; 

public CarList() { 
    head = null; 
} 

public int getSize() { 
    return counter; 
} 
public boolean isEmpty() { 
    if(counter < 1) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 
/**public CarNode firstNode() { 
    return head; 
} 
public CarNode lastNode() { 
    return tail; 
} 
public CarNode getNode(int target) { 
    CarNode pre; 
    pre = head; 

    while(pre.next != null) { 
     pre = pre.next; 

     if(pre.data == target) { 
      return pre; 
     } 

    } 
    return null; 
}**/ 
public void insert (String target) { 
    if(head==null || target < head.data) { 
     insert_at_head(target); 
    return; 
    } 
    CarNode pre = head; 

      while(pre.next != null && target > (pre.next).data) { 
       pre = pre.next; 

       CarNode newNode = new CarNode(target); 
       newNode.next = pre.next; 
       pre.next = newNode; 
    } 
} 
} 
//The CarNode Class 
class CarNode { 
Car data; 
CarNode next; 
CarNode pre; 

public CarNode(Car entry) { 
    this.data = entry; 
    next = null; 
    pre = null; 
} 

} 

//Car Class 

public class Car { 
int Price; 
String Make; 

public Car(int pennies, String m) { 
    this.Price = pennies; 
    this.Make = m; 
} 

public int getPrice() { 
    return Price; 
} 

public String getMake() { 
    return Make; 
} 

public void setPrice(int p) { 
    Price = p; 
} 
public void setMake(String m) { 
    Make = m; 
} 
} 

回答

2

我不明白您的插入方法

public void insert (String target) { 
    if(head==null || target < head.data) { 
    insert_at_head(target); 
    return; 
    } 
    CarNode pre = head; 

    while(pre.next != null && target > (pre.next).data) { 
     pre = pre.next; 
     CarNode newNode = new CarNode(target); 
     newNode.next = pre.next; 
     pre.next = newNode; 
    } 
} 

我認爲這將是:

public void insert (Car target) { 
    if(head==null || target.compare(head.data)<0) { 
    insert_at_head(target); 
    }else{ 
    CarNode pre = head; 
    while(pre.next != null && target.compare((pre.next).data)>0) { 
     pre = pre.next; 
     CarNode newNode = new CarNode(target); 
     newNode.next = pre.next; 
     pre.next = newNode; 
    } 
    } 
} 

比你必須創建在汽車類int compare(Car other)方法。

0

您不必將變量makeprice「放置」到CarNode類中,但是您可以在您的汽車類中使用getter和setter方法在您的節點類中使用。

因此,如果您在已經創建(實例化)的Node類中使用了Car對象,那麼您可以使用像這樣的getter和setter方法。

因此,在CarNode類中,讓我們創建一輛汽車並獲取該數據。

this.data.getPrice(); 

this.data.getMake(); 
0

由於Java是開源的,你有一個爐排機會看一看實現LinkedList自帶的Java集合框架的一部分。並以類似的方式實施它。

0

insert方法看起來不對我。我相信你試圖插入Car這樣的鏈表按升序排列。你可以這樣做:

public void insert (String target) { 
    counter++; 
    if(head==null || target < head.data) { 
    insert_at_head(target); 
    }else{ 
    /* trying to find position such that cur is greater than target */ 
    CarNode pre = head, cur = head.next; 
    while(cur != null && cur.data < target) { 
     pre = cur; 
     cur = cur.next; 
    } 

    /* now insert element between pre and cur */ 
    /* if cur is end then you might reach the end of list */ 
    CarNode c = new CarNode(target); 
    pre.next = c; 
    c.next = cur; 
    if(c.next == null) 
     tail = c; 
    } 
} 
+0

這會不會與我insert_at_head方法工作 公共無效insert_at_head(汽車數據){ \t \t CarNode臨時=新CarNode(數據); \t \t temp.next = head; \t \t head = temp; \t} – user2084799 2013-02-18 22:19:26

+0

這是直接從您的代碼。只有我改變的部分在'else'中。 – Shivam 2013-02-18 22:33:02

相關問題