简体   繁体   English

双链表

[英]Doubly linked lists

I have an assignment that I am terribly lost on involving doubly linked lists (note, we are supposed to create it from scratch, not using built-in API's). 我有一个涉及双重链接列表的作业,我为此而迷失了(请注意,我们应该从头开始创建它,而不是使用内置API)。 The program is supposed to keep track of credit cards basically. 该程序应该基本上可以跟踪信用卡。 My professor wants us to use doubly-linked lists to accomplish this. 我的教授希望我们使用双向链接列表来完成此任务。 The problem is, the book does not go into detail on the subject (doesn't even show pseudo code involving doubly linked lists), it merely describes what a doubly linked list is and then talks with pictures and no code in a small paragraph. 问题是,这本书没有详细介绍该主题(甚至没有显示涉及双向链表的伪代码),仅描述了双向链表是什么,然后在很小的一段文字中与图片进行了交谈,而没有代码。 But anyway, I'm done complaining. 但是无论如何,我已经抱怨完了。 I understand perfectly well how to create a node class and how it works. 我非常了解如何创建节点类以及它如何工作。 The problem is how do I use the nodes to create the list? 问题是如何使用节点创建列表? Here is what I have so far. 这是我到目前为止所拥有的。

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.
    }

}


}

Here is the assignment itself if it helps to clarify what is required and more importantly, the parts I'm not understanding. 这是作业本身,以帮助弄清需要什么,更重要的是,我不理解的部分。 Thanks https://docs.google.com/open?id=0B3vVwsO0eQRaQlRSZG95eXlPcVE 谢谢https://docs.google.com/open?id=0B3vVwsO0eQRaQlRSZG95eXlPcVE

I assume CardList is meant to encapsulate the actual doubly-linked-list implementation. 我假设CardList旨在封装实际的双链表实现。

Consider the base case of a DLL with only a single node: the node's prev and next references will be null (or itself). 考虑只有一个节点的DLL的基本情况:该节点的prevnext引用将为null(或本身为null)。 The list's encapsulation's head and tail references will both be the single node (as the node is both the start and end of the list). 列表的封装的headtail引用都将是单个节点(因为该节点既是列表的开始又是结尾)。 What's so difficult to understand about that? 那有什么很难理解的?

NB: Assuming that CardList is an encapsulation of the DLL structure (rather than an operation) there's no reason for it to have a CardInfoNode current field, as that kind of state information is only useful to algorithms that work on the structure, which would be maintaining that themselves (it also makes your class thread-unsafe). 注意:假设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.
    }

the above code is for when u not have any nodes in list, here ur going to add nodes to ur list.Ie ist node to list 上面的代码适用于列表中没有任何节点的情况,这里我们将节点添加到列表中。
here ur pointing head & tail to same node 这里我们将头和尾指向同一节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM