如何使用Java中的对象创建链接列表。
这是课堂作业,我在使用链接列表中的汽车对象时遇到问题。 car类有两个实例变量(make、price)。 如何将变量放入node类中如何使用Java中的对象创建链接列表。,java,linked-list,Java,Linked List,这是课堂作业,我在使用链接列表中的汽车对象时遇到问题。 car类有两个实例变量(make、price)。 如何将变量放入node类中 public class CarList { private CarNode head = null; private CarNode tail = null; private int counter = 0; public CarList() { head = null; } public int getSize() { return cou
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;
}
}
public-class-CarList{
专用卡诺节点头=空;
私有CarNode tail=null;
专用整数计数器=0;
公职人员(){
head=null;
}
公共int getSize(){
返回计数器;
}
公共布尔值为空(){
如果(计数器<1){
返回true;
}
否则{
返回false;
}
}
/**公共CarNode firstNode(){
回流头;
}
公共CarNode lastNode(){
返回尾;
}
公共CarNode getNode(int目标){
卡诺前;
pre=头;
while(pre.next!=null){
pre=pre.next;
if(pre.data==目标){
返回预处理;
}
}
返回null;
}**/
公共void插入(字符串目标){
if(head==null | | target(pre.next).data){
pre=pre.next;
CarNode newNode=新CarNode(目标);
newNode.next=pre.next;
pre.next=newNode;
}
}
}
//卡诺德类
类卡诺管{
汽车数据;
卡诺下;
卡诺前;
公共卡诺德(汽车入口){
这个数据=输入;
next=null;
pre=null;
}
}
//汽车等级
公车{
国际价格;
串制;
公共汽车(整数便士,字符串m){
价格=便士;
这个Make=m;
}
public int getPrice(){
退货价格;
}
公共字符串getMake(){
退货;
}
公共无效定价(INTP){
价格=p;
}
公共void setMake(字符串m){
Make=m;
}
}
我无法理解您的插入方法
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插入(字符串目标){
if(head==null | | target(pre.next).data){
pre=pre.next;
CarNode newNode=新CarNode(目标);
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;
}
}
}
公共作废插入(车辆目标){
if(head==null | | target.compare(head.data)0){
pre=pre.next;
CarNode newNode=新CarNode(目标);
newNode.next=pre.next;
pre.next=newNode;
}
}
}
然后,您必须在Car类中创建一个int compare(Car-other)
方法。您不必将那些变量make
和price
放入CarNode类中,但您可以在Car类中使用getter和setter方法来在节点类中使用
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;
}
}
因此,如果您在已经创建(实例化)的节点类中使用Car
对象,那么您可以像这样使用getter和setter方法
所以在CarNode
类中,让我们创建一个car并希望获得该数据
this.data.getPrice();
及
由于Java是开源的,您有机会了解Java集合框架中的实现
LinkedList
。并以类似的方式实现它。您的insert
方法在我看来是错误的。我相信您正在尝试插入汽车
,以便按升序插入链接列表。您可以这样做:
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;
}
}
public void插入(字符串目标){
计数器++;
if(head==null | | target
这不适用于直接从您的代码中获取的我的insert_at_head方法public void insert_at_head(汽车数据){CarNode temp=new CarNode(数据);temp.next=head;head=temp;}。我唯一更改的部分是在else
中。