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] + " ");
	}
	
	
}

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] + " ");
	}
	
	
}

Hmm check out here which might be of help:http://www.mycstutorials.com/articles/data_structures/linkedlists and also here:http://pages.cs.wisc.edu/~vernon/cs367/notes/4.LINKED-LIST.html

Edited 4 Years Ago by DavidKroukamp: n/a

Thank you for quick response. I have now study the links you gave me :D
But can you give me a quick example how i can remove the first node in my code ?
That would be a great advantage for me.

Thank you for quick response. I have now study the links you gave me :D
But can you give me a quick example how i can remove the first node in my code ?
That would be a great advantage for me.

Hmm yes i could do that or i could empower you with the knowledge to do it yourself:http://wiki.answers.com/Q/Delete_a_node_from_linked_list_algorithm and how about this too:http://stackoverflow.com/questions/270950/linkedlist-remove-method :)

Edited 4 Years Ago by DavidKroukamp: n/a

This article has been dead for over six months. Start a new discussion instead.