[英]Doubly linked lists
我有一个涉及双重链接列表的作业,我为此而迷失了(请注意,我们应该从头开始创建它,而不是使用内置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
我假设CardList
旨在封装实际的双链表实现。
考虑只有一个节点的DLL的基本情况:该节点的prev
和next
引用将为null(或本身为null)。 列表的封装的head
和tail
引用都将是单个节点(因为该节点既是列表的开始又是结尾)。 那有什么很难理解的?
注意:假设CardList
是DLL结构(而不是操作)的封装,则没有理由让它具有CardInfoNode current
字段,因为这种状态信息仅对在该结构上工作的算法有用。自己维护(这也会使您的类线程不安全)。
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.
}
上面的代码适用于列表中没有任何节点的情况,这里我们将节点添加到列表中。
这里我们将头和尾指向同一节点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.