Pointers/Doubly Link lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • valt
    New Member
    • Feb 2008
    • 3

    #1

    Pointers/Doubly Link lists

    Code:
    public class DLLNode
    {
    	String 		Item;
    	int 		Time;
    	DLLNode 	Next;
    	DLLNode 	Prev;
    
    	public DLLNode(String newItem,int newTime,DLLNode newNext,DLLNode newPrev)
    	{
    		Item 			= newItem;
    		Time 			= newTime;
    		Next 			= newNext;
    		Prev		 	= newPrev;
    	}
    }
    Code:
    public class DLLList
    {
    	DLLNode Head;
    
    	public DLLList()
    	{
    		Head 	  = null;
    		Head.Prev   = null;
    	}
    
    	public void add(String Item,int Time)
    	{
    
    		Head = new DLLNode(Item,Time,Head,Head.Prev);
    	}
    }
    Perhaps im just a newb, but I dont understand why I just cant do this when im creating the tail for my doubly linked list. I get a null pointer exception. I dont see a reason why I cant initialize it like this... Head.Prev iwont work because there are no values set? It seems like a waste to create another node for the tail because you will have double the overhead as a linked list.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by valt
    [Code=Java]
    public class DLLList {
    DLLNode Head;

    public DLLList() {
    Head = null;
    Head.Prev = null;
    }
    [/Code]
    You need to see why that constructor is doomed. You set Head to null so it does not refer to any object, and the one thing you really need to appreciate about a null reference (and this is something you need to learn and take to heart on day one) is that you can't dereference it -- you can't write Head.anything.

    In any case, doesn't a DLL need to maintain a pair of references -- one to the head of the list and one to the tail?

    Comment

    • valt
      New Member
      • Feb 2008
      • 3

      #3
      I dont see why you need to maintain the reference to the tail at the beginning... as the items grow in the list you should be able to get a reference to a tail later on. You know what im saying? I see these tutorials being made creating two nodes and that just seems foolish to me.

      Thanks for you feedback : ]

      Code:
      Head = new DLLNode(null,null,null,null);
      Also, why isnt this possible in the constructor?

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by valt
        I dont see why you need to maintain the reference to the tail at the beginning... as the items grow in the list you should be able to get a reference to a tail later on. You know what im saying? I see these tutorials being made creating two nodes and that just seems foolish to me.

        Thanks for you feedback : ]
        There are many slightly different ways to implement a doubly-linked list. I don't want to force you to do it one way, so I'll say no more.

        Comment

        • valt
          New Member
          • Feb 2008
          • 3

          #5
          Anyway... This is the way i decided to do it... hopefully this works

          Code:
          public class DLLNode
          {
          	String 		Item;
          	int 		Time;
          	DLLNode 	Next;
          	DLLNode 	Prev;
          
          	public DLLNode(String newItem,int newTime,DLLNode newPrev)
          	{
          		Item 			= newItem;
          		Time 			= newTime;
          		Prev		 	= newPrev;
          	}
          	public DLLNode(String newItem,int newTime)
          	{
          		Item 			= newItem;
          		Time 			= newTime;
                                          Prev                                          = null;
          	}
          }
          Code:
          public class DLLList
          {
          	DLLNode Head = null;
          
          	public DLLList()
          	{
          		Head = null;
          	}
          
          	public void add(String Item,int Time)
          	{
          		if(Head == null)
          			Head = new DLLNode(Item,Time);
          		else
          		{
          			Head = new DLLNode(Item,Time,Head);
          			Head.Prev.Next = Head;
          		}
          	}
          }
          Works like a charm... Thanks for your help
          Last edited by valt; Feb 25 '08, 10:34 PM. Reason: Worked

          Comment

          Working...