[英]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.