Click here to Skip to main content
15,896,338 members

How does a linkedlist work and why do we provide a node class inside the main class?

Member 12491145 asked:

Open original thread
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.
C#
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.
Tags: Java

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900