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

4

2 回答 2

0

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

考虑仅具有单个节点的 DLL 的基本情况:节点prevnext引用将为空(或自身)。列表的封装headtail引用都将是单个节点(因为节点既是列表的开始又是列表的结尾)。这有什么难理解的?

注意:假设这CardList是对 DLL 结构(而不​​是操作)的封装,它没有理由拥有一个CardInfoNode current字段,因为这种状态信息仅对在结构上工作的算法有用,而这些算法将自己维护(它也使你的类线程不安全)。

于 2012-09-23T03:21:58.153 回答
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.
    }

上面的代码适用于当您在列表中没有任何节点时,您将在此处将节点添加到您的列表中。即此处列出的 ist 节点您
将头和尾指向同一个节点

于 2012-09-23T03:24:53.130 回答