Hey
I am a student and i am new on the algorithm subject.
I have been given a compulsory assignment that is to set up a program that have Liked list with Nodes.
The problem is that i dont understand how to do this:
- remove the first node
- add an node to the end of the list
Here is my code so fare:
public class Node {
int data;
Node next;
Node(int data, Node neste){
this.data = data;
this.next = neste;
}
public int getData(){
return data;
}
public Node getNode(){
return next;
}
public void setData(int d){
data = d;
}
public void setNode(Node n){
next = n;
}
}
class Lenket{
private Node hode = null;
private int count = 0;
public int antElemeter(){
return count;
}
public Node finnHode(){
return hode;
}
public void settInnNode(int verdi){
hode = new Node(verdi, hode);
++count;
}
public Node removeFirst(int verdi) {
Node andre = hode.next;
hode = andre.next;
andre = new Node(verdi, null);
return hode;
}
public static void main(String []args){
Lenket l = new Lenket();
int t [] = {7, 1, 5, 4, 2};
for(int i = 0; i < t.length; ++i){
l.settInnNode(t[i]);
l.removeFirst(t[i]); // this is the method that shoud remove the first node, but it dosent work.
}
skrivTabell(t);
System.out.println(" ");
System.out.print("Antall Elementer: " + l.antElemeter());
System.out.print("\nVerdie i Index en: " + l.hode.getData());
}
public static void skrivTabell(int t[]){
System.out.print("Hele Noden: ");
for(int i= 0; i < t.length; ++i)
System.out.print( t[i] + " ");
}
}