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.