2012-09-23 188 views
1

我有一个任务,因为涉及双向链表(注意,我们应该从头开始创建它,而不是使用内置的API),所以我非常遗憾。该计划应该基本上追踪信用卡。我的教授希望我们使用双向链表来实现这一点。问题在于,这本书没有详细介绍这个主题(甚至不显示涉及双向链表的伪代码),它仅仅描述了双链表是什么,然后用图片和小段落中的代码进行会谈。但无论如何,我已经抱怨了。我完全理解如何创建节点类以及它如何工作。问题是我如何使用节点来创建列表?这是我到目前为止。双向链表

public class CardInfo 
{ 
private String name; 
private String cardVendor; 
private String dateOpened; 
private double lastBalance; 
private int accountStatus; 

private final int MAX_NAME_LENGTH = 25; 
private final int MAX_VENDOR_LENGTH = 15; 

CardInfo() 
{ 

} 

CardInfo(String n, String v, String d, double b, int s) 
{ 
    setName(n); 
    setCardVendor(v); 
    setDateOpened(d); 
    setLastBalance(b); 
    setAccountStatus(s); 
} 

public String getName() 
{ 
    return name; 
} 

public String getCardVendor() 
{ 
    return cardVendor; 
} 

public String getDateOpened() 
{ 
    return dateOpened; 
} 

public double getLastBalance() 
{ 
    return lastBalance; 
} 

public int getAccountStatus() 
{ 
    return accountStatus; 
} 

public void setName(String n) 
{ 
    if (n.length() > MAX_NAME_LENGTH) 
     throw new IllegalArgumentException("Too Many Characters"); 
    else 
     name = n;   
} 

public void setCardVendor(String v) 
{ 
    if (v.length() > MAX_VENDOR_LENGTH) 
     throw new IllegalArgumentException("Too Many Characters"); 
    else 
     cardVendor = v; 
} 

public void setDateOpened(String d) 
{ 
    dateOpened = d; 
} 

public void setLastBalance(double b) 
{ 
    lastBalance = b; 
} 

public void setAccountStatus(int s) 
{ 
    accountStatus = s; 
} 

public String toString() 
{ 
    return String.format("%-25s  %-15s  $%-s  %-s  %-s", 
      name, cardVendor, lastBalance, dateOpened, accountStatus); 
} 
} 

public class CardInfoNode 
{ 
CardInfo thisCard; 

CardInfoNode next; 
CardInfoNode prev; 

CardInfoNode() 
{ 

} 

public void setCardInfo(CardInfo info) 
{ 
    thisCard.setName(info.getName()); 
    thisCard.setCardVendor(info.getCardVendor()); 
    thisCard.setLastBalance(info.getLastBalance()); 
    thisCard.setDateOpened(info.getDateOpened()); 
    thisCard.setAccountStatus(info.getAccountStatus()); 
} 

public CardInfo getInfo() 
{ 
    return thisCard; 
} 


public void setNext(CardInfoNode node) 
{ 
    next = node; 
} 

public void setPrev(CardInfoNode node) 
{ 
    prev = node; 
} 

public CardInfoNode getNext() 
{ 
    return next; 
} 

public CardInfoNode getPrev() 
{ 
    return prev; 
} 
} 

public class CardList 
{ 
CardInfoNode head; 
CardInfoNode current; 
CardInfoNode tail; 

CardList() 
{ 
    head = current = tail = null; 
} 


public void insertCardInfo(CardInfo info) 
{ 
    if(head == null) 
    { 
     head = new CardInfoNode(); 
     head.setCardInfo(info); 
     head.setNext(tail); 
     tail.setPrev(node) // here lies the problem. tail must be set to something 
           // to make it doubly-linked. but tail is null since it's 
           // and end point of the list. 
    } 

} 


} 

这里是分配本身,如果它有助于澄清是必要的,更重要的是,部分我不理解。谢谢 https://docs.google.com/open?id=0B3vVwsO0eQRaQlRSZG95eXlPcVE

+0

请阅读此:http://en.wikipedia.org/wiki/Doubly_linked_list;然后获得一些想法并实施它们。然后寻求帮助) – Serge

+0

@RaymondChen:请注意,家庭作业标签现在[正式弃用](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially- depprecated)和不应再添加到问题中。 –

回答

0
if(head == null) 
    { 
     head = new CardInfoNode(); 
     head.setCardInfo(info); 
     head.setNext(tail); 
     tail.setPrev(node) // here lies the problem. tail must be set to something 
           // to make it doubly-linked. but tail is null since it's 
           // and end point of the list. 
    } 

上面的代码是当你没有任何列表中的节点,在这里你要添加节点到你的list.I.e. ist节点列表
这里你指向的头&尾部到同一节点

+0

确切地说,事情是,我将第一个元素插入到列表中。那么我应该让尾巴和头部都等于那个节点吗?还是我应该做点别的?这本书还提到使用“虚拟节点”,但同样,它也非常模糊。 – audiFanatic

+0

是的,你应该让尾巴和头都相等 –

+0

谢谢澄清! – audiFanatic

0

我假设CardList是为了封装实际的双向链表实现。

考虑只有单个节点的DLL的基本情况:节点的prevnext引用将为空(或其本身)。列表的封装的headtail引用都将是单个节点(因为该节点既是列表的开始也是结束)。有什么难以理解的呢?

注意:假设CardList是DLL结构(而不​​是操作)的封装,没有理由要有一个CardInfoNode current字段,因为这种状态信息只对运行在结构上的算法有用,这会自己维护它(这也会让你的类变得不安全)。

+0

谢谢戴,现在更有意义了。就目前的参考而言,我明白你在说什么,但是把它放在那里是它的一部分。如果它能帮助您更好地理解,我会在原始文章结尾处将作业发布到Google文档中。 – audiFanatic

+0

下面是再次的情况下的链接,你错过了:https://docs.google.com/open?id=0B3vVwsO0eQRaQlRSZG95eXlPcVE – audiFanatic

+0

确定,这里就是我把它改为:
如果(头== NULL)
\t {
\t \t head = new CardInfoNode();
\t \t head.setCardInfo(info);
\t \t head。setNext(尾部);
\t \t tail = head;
\t \t tail.setPrev(head);
\t}
audiFanatic