Jump to content

Is this neccessary?? a Java code


albertlee

Recommended Posts



import java.util.Enumeration;

public class LinkedList {

private static class Node {
	public Node prev;
	public Node next;
	public Object data;

	public Node(Node prev, Node next,
			Object data) {
		this.prev = prev;
		this.next = next;
		this.data = data;
	}
}


Node head;

public LinkedList() {
	head = null;
}

public void add(Object data) {
	Node n = new Node(null, head, data);

	[b]/*if(head != null)
		head.prev = n;  */[/b]

	head = n;
}

public Enumeration elements() {
	return new LLEnum();
}


private class LLEnum
		implements Enumeration {
	Node current;

	public LLEnum() {
		current = head;
	}

	public boolean hasMoreElements() {
		return current != null;
	}

	public Object nextElement() {
		Object data = current.data;
		current = current.next;
		return data;
	}
}
}

 

and

 

import java.util.Enumeration;

public class LinkedListTest {

public static void main(String[] args) {
	LinkedList ll = new LinkedList();

	for(int ii=0 ; ii<args.length ; ii++)
		ll.add(args[ii]);

	Enumeration enumu = ll.elements();
	while(enumu.hasMoreElements()) {
		System.out.println("Next linked-list node: " +
				enumu.nextElement());
	}
}

}

 

 

 

the 1st code behaves like a "growable" array, cappable of inserting different kind of object as its element.

 

 

the 2nd code is just given the main entry point to execute the program from another class.

 

Question, as the 1st code, LinkedList class apears, I dont know exactly what is the code within the comment mark does if I un-comment it. The codes are sample, and I was trying to understand its algorithm by drawing UML cases every time a Node object is reallocated as the parameter within another Node object's constructor.

 

Can any one tell me is there a reason for the code that's in comment mark in bold?? from the creator's point of view?? because with or without it, the code makes no difference. To my understanding, it just makes the Node head object bigger in size by storing a reference to an object that has a refernce to itself.

 

thanks

Link to comment
Share on other sites

>> Question, as the 1st code, LinkedList class apears, I dont know exactly what is the

>> code within the comment mark does if I un-comment it.

 

Your currently introduced linked list works the following way: There is a master node called head that the class knows itself. Each element (including head) then know the respectively next element in the list. This way, you can find each element by parsing the list. The "prev" entry of the list is obsolete as it isn´t used. If you un-comment the part you mentioned the "prev" property will be set. This is: Each item in the list will not only know the element that´s coming next, but also the element that comes before them.

 

 

>> Can any one tell me is there a reason for the code that's in comment mark in bold??

 

Perhaps because it´s a comment? For me, comments are much more important than actual code when I read other people´s code (so from this point of view, the code snippet you presented is crap).

 

 

>> To my understanding, it [the oucommented part, if it was in] just makes the Node

>> head object bigger in size by storing a reference to an object that has a refernce

>> to itself.

 

I don´t know Java so I might be wrong here. But what determines the size of an object (and all it´s nodes, then - not only the head) is it´s variables defined, not the variables used. So with or without the commented part, your nodes will have the same size. You´d have to remove the superfluous "prev" variable to reduce your list´s size.

Link to comment
Share on other sites

thanks Atheist

 

In fact, I did the comment, not the original creator.

 

What was the creator thinking about??

 

I could create a less complicated and cleaner code to do the same thing. :):)

 

In addition to that, every varriable of an object has constant size, because each one only holds an address, but the actual object can varry in size, situated in the heap, which could influence the system overall performance.

 

any way, thanks for the response, because all I want is to assure myself that the code line commented is vacuous.

Link to comment
Share on other sites

The code commented out could make sense if, for example, for some reason you are given a node and want to have the element that´s two levels above it in the list. It´s just a different type of list (doubly linked). It really depends on your application what kind of list you need/want.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.