-1

This is for a assignment in class and I am having trouble with using My car objects in my link List. The car class has two instance variables (make, price). How would I place the variables into the node class.

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;
}
}
4

4 回答 4

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)在 Car 类中创建一个方法。

于 2013-02-18T21:32:35.270 回答
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;
  }
}
于 2013-02-18T21:47:21.883 回答
0

您不必将这些变量“放置”makepriceCarNode 类中,但您可以在 car 类中使用 getter 和 setter 方法在节点类中使用。

因此,如果您Car在 Node 类中使用已经创建(实例化)的对象,那么您可以像这样使用那些 getter 和 setter 方法。

所以在CarNode类中,让我们创建一辆汽车并想要获取该数据。

this.data.getPrice();

this.data.getMake();
于 2013-02-18T21:36:20.290 回答
0

由于 Java 是开源的,您有机会了解LinkedList作为 Java Collection 框架一部分的实现。并以类似的方式实现它。

于 2013-02-18T21:38:40.377 回答