Click here to Skip to main content
14,331,896 members
Rate this:
Please Sign up or sign in to vote.
See more:
I new to data structures and on my own no one to teach me. I first started leaning Linked List but I came across multiple question. So my question are as follows please answer them in detail as you would to a 15 yr old boy.

1. Why do we provide the Node class inside the other class. Why cant we place it outside, if we can how should we place it.
2. How does the reference variable work in Java. Is it a implicit pointer built in java. can you please explain with a memory diagram.

Thank you in advance.

I have improved my question and the new version is below:

Hay thanks for your answer I have uploaded my code:

I re-implemented it with any help just my knowledge and I hasn't done it completely.
I mean the Node class inside the Linked list class. Why cant we just put the node class outside as separate class and a not a subclass. If we can do it how should we implement it. Please upload your version of Linked list at-least with basic functionality.
public class LinkedList {

	static class Node
	{
		int data;
		Node next;

		public Node(int data)
		{
			this.data = data;
			next = null;
		}

		public Node getNext()
		{
			return next;
		}

		public int getElement()
		{
			return data;
		}

		public void setNext(Node n)
		{
			next = n;
		}
	}

	Node head = null;
	Node tail = null;
	int size = 0;

	public boolean isEmpty(){ return size==0; }

	public int size(){ return size; }

	public int first()
	{
		if(isEmpty()){ return 0; }
		return head.getElement();
	}

	public int last()
	{
		if(isEmpty()) return 0;
		return tail.getElement();
	}

	public void addFirst(int data)
	{
		Node tmp = new Node(data);
		if(size == 0){head = tmp; tail = head; size++;}
	}

	public static void main(String args[])
	{


	}
}


What I have tried:

I have just memorized the code, But still understood the topics a little bit the question is just for some extra clearance. I have already implemented Linkedlist in java without generics and with features such as ADD, DELETE, REMOVEFIRST, REMOVELAST, ADDFIRST, ADDLAST.
Posted
Updated 11-May-16 11:26am
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is a technical forum, not a tutorial forum. You need to do your own research: see linked list java example - Google Search[^].
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
1. Why do we provide the Node class inside the other class. Why cant we place it outside, if we can how should we place it
This question makes poor sense if we cannot actually see the code.
As a wild guess, Node is not defined outside because it is an implementation detail.

Quote:
How does the reference variable work in Java. Is it a implicit pointer built in java. can you please explain with a memory diagram
Short answer: yes. You might find a more detailed one here: How is a Java reference different from a C pointer? - Programmers Stack Exchange[^]
   
Comments
   
5ed. Again, I provided some more detail in Solution 3.

By the way, do you know the joke I marked with first smile :-) in this solution? We often use it as a popular well-know meme (the meaning is as shown in my solution; it means "what the hell you are talking about?"), but only recently I leaned the source of it. I don't even know if it is international or not, but it sounds great in English.

—SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

I have no idea which implementation of linked list you mean. Actually, any developer is supposed to do such exercise from scratch, otherwise no one can say that such person has a clue on what software development is. With Java, this is much times easier than with lower-level languages, because of the garbage collector.

  1. I don't know why do "we" provide "the class inside class" (we? who are all those people? :-)), but I would really use two classes. One class would represent the references to next/previous list element, and another one would represent the "informational" part of the node, its content. I would use Java generics to implement the node.

    This way, it could isolate two aspects of the node: the functionality specific to the complete type of the node, and the universal part agnostic to the specific part; this universal part would implement pure list functionality: referenced to other nodes and list operations.

    By they way, it does not have to be "class in class". Don't mix inner classes with reference member of one class, of the type of another class. You can declare two classes separately and use one class in another one. That's all.
  2. It's too bothersome to draw a diagram (without graphics). Please draw the diagram in your head. Better yet, read about Java reference (or more generally, managed referenced (sometimes called managed pointers) in any of the tens of books on the topic. Basically, a Java reference object is really two objects: the object itself and the object referencing it. The application developer directly operates only by references. The object itself is on heap and is accessed through a reference when you use any of its instance members. When you assign one reference to another one, you obtain two reference variables, but the actual referenced object is only one. When you lose all referenced, the referenced object is considered unreachable and will be eventually destroyed; its memory will be reclaimed.

    So, you "diagram" is just two boxes: referenced object and the reference to it. Isn't it easy enough? :-)

    Now, references are like pointers, but they are not exactly pointers. They are managed. Say, the application developer is not guaranteed that the object keeps its physical memory location. The memory management is allowed to relocate it, but it's done transparently to the application developer; the integrity of the whole set of references and referenced all objects is atomically preserved.

    My description of garbage collection is greatly simplified. I did not provide some essential detail just because this is not a part of your question; so you only need the basic idea at this point.


—SA
   
v2
Comments
CPallini 11-May-16 4:38am
   
5.
   
Thank you, Carlo.
—SA
Member 12491145 11-May-16 9:27am
   
Hay thanks for your answer I have uploaded my code:

I re-implemented it with any help just my knowledge and I hasn't done it completely.
I mean the Node class inside the Linked list class. Why cant we just put the node class outside as separate class and a not a subclass. If we can do it how should we implement it. Please upload your version of Linked list at-least with basic functionality.

public class LinkedList {

static class Node
{
int data;
Node next;

public Node(int data)
{
this.data = data;
next = null;
}

public Node getNext()
{
return next;
}

public int getElement()
{
return data;
}

public void setNext(Node n)
{
next = n;
}
}

Node head = null;
Node tail = null;
int size = 0;

public boolean isEmpty(){ return size==0; }

public int size(){ return size; }

public int first()
{
if(isEmpty()){ return 0; }
return head.getElement();
}

public int last()
{
if(isEmpty()) return 0;
return tail.getElement();
}

public void addFirst(int data)
{
Node tmp = new Node(data);
if(size == 0){head = tmp; tail = head; size++;}
}

public static void main(String args[])
{


}
}
Sergey Alexandrovich Kryukov 11-May-16 10:41am
   
Please don't show any code in comments, it's unreadable; move it to the question, using "Improve question".
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100