5 Replies - 898 Views - Last Post: 19 February 2012 - 05:53 AM Rate Topic: -----

#1 raihan26   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Implement a doubly linked with null objects

Posted 19 February 2012 - 12:20 AM

What I want to do is- Implement a doubly linked list with null objects at the beginning and end of the list. So an
empty list will contain two null objects. So I was not able to understand what null objects means at the beginning and end of the list. And after creating null objects at the beginning and end of the list. I have to insert data in between the null objects everytime. So I have done this so far-

// Creating a doubly linked list.
    	doubleLinkedList = new DoubleLinkedList();

    class DoubleLinkedList {
    
    	private NewLink firstNode;
    	private NewLink lastNode;
    	private NewLink rootNode;
    

    	public DoubleLinkedList() {
    
//So this satisfies my question that I have asked meaning null objects at beginning and last node or something else I have to do.
    		firstNode = null;
    		lastNode  = null;
    
    	}
    
    }



    class NewLink {
    
    	public String  data;
    	public NewLink nextPointer;
    	public NewLink previousPointer;
    
    	public NewLink(String id) {
    
    		data = id;
    
    	}
    
    	// Overriding toString method to return the actual data of the node
    	public String toString() {
    
    		return "{" + data + "} ";
    
    	}
    }




Is This A Good Question/Topic? 0
  • +

Replies To: Implement a doubly linked with null objects

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Implement a doubly linked with null objects

Posted 19 February 2012 - 01:00 AM

The term 'null objects' means the data package in the node is null.

In addition to the data package, a node in a doubly linked list has two 'pointers', one to the previous node and another to the next node. The first and last node are typically called the head and tail node, respectively, but you can use first and last.

The two nodes in your list at creation will have the following characteristics:

first (or head) node:
previous = null;   // or prev, there is no node before the first node
next = last;       // next points to the tail
data = null;

last (or tail) node:
previous = first;  // or prev, points to first (or head) node
next = null;       // there is no node after the last or tail node
data = null;

Was This Post Helpful? 1
  • +
  • -

#3 raihan26   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Re: Implement a doubly linked with null objects

Posted 19 February 2012 - 01:38 AM

Thanks Greg for quick reply. So it doesn't means anything with Null Object Pattern as described here? http://cs.oberlin.ed...ullObjPattern/. I can simply declared those two as null as described you?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Implement a doubly linked with null objects

Posted 19 February 2012 - 02:11 AM

Implementing a Null Object pattern in a linked list is more involved than I understood your original post to be. Now I don't know what you're assignment is or if the link is relevant or just something you found by searching the net. Please clarify your assignment.
Was This Post Helpful? 0
  • +
  • -

#5 raihan26   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Re: Implement a doubly linked with null objects

Posted 19 February 2012 - 02:22 AM

What the actual question of Assignment was I have posted that above. But I think we have to use null object design pattern to implement those two null objects. But I am not sure now how can I use null object design pattern to use here in my case
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7205
  • View blog
  • Posts: 15,018
  • Joined: 16-October 07

Re: Implement a doubly linked with null objects

Posted 19 February 2012 - 05:53 AM

View Postraihan26, on 19 February 2012 - 05:22 AM, said:

But I think we have to use null object design pattern to implement those two null objects.


Unlikely.

The point of a "NullObject" is to allow there to always be some object stored in a variable rather than nothing at all, or null. There are two reasons to do this. One is that you always check for some quality of your returned object and dealing with the lack of that object becomes awkward.

The other reason is that you actually want to be able to return a null, but the return type doesn't allow it. In Java, you can't return a null int, because int is value type, not an object. You can, however, return a null Integer object. You'll see this done sometimes. It's one of the reasons Java generic collection classes don't handle primitives at all.

If this sounds like something you haven't covered and more than you should probably be worrying about, you're right.

In a linked list of any kind, becoming comfortable with null and it's usefulness is a fundamental part of the exercise. Forget NullObject, focus on null and links.

Btw, NewLink is a wonky name for a node. Is it always new? Is it even a node? Choose names that make sense to you.

I'd start out like this:
class DoubleLinkedList {
	private class Node {
		public String data;
		public Node previous, next;
		public Node(String data) { this(data, null, null); }
		public Node(String data, Node previous, Node next) { 
			this.data = data;
			this.previous = previous;
			this.next = next;
		}
		public String toString() { return "{" + data + "} "; }
	}
	
	
	private Node firstNode, lastNode;
	// you do not need this!
	// private NewLink rootNode;

	public DoubleLinkedList() { firstNode = lastNode = null; }
	
	public boolean isEmpty() { return firstNode == null; }
	
	public void add(String data) {
		if (isEmpty()) {
			firstNode = lastNode = new Node(data);
		} else {
			// the fun part, your code here
		}
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1