简体   繁体   中英

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

I assume CardList is meant to encapsulate the actual doubly-linked-list implementation.

Consider the base case of a DLL with only a single node: the node's prev and next references will be null (or itself). 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). 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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