Results 1 to 4 of 4

Thread: Linked List

  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Linked List

    When adding nodes to your linked list are you inserting everything to the tail are to the head? So traversing through the list is it LIFO or FIFO???? Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: Linked List

    It also depends from which side the nodes are deleted ...

    kind regards,

    Jos

    ps Why is this in the advanced section?
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Linked List

    Both... It depends on the observer's viewpoint...

  4. #4
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Re: Linked List

    Ok here it is, I have a node....

    Java Code:
    package listwnodes;
    
    public class Node<T> {
    
    	private Node<T> next;
    	private Node<T> prev;
    	private T element;
    	
    	public Node(){
    		element = null;
    		next = prev = null;
    	}
    	
    	public Node(T ele){
    		this.element = ele;
    		next = prev = null;
    		}
    	
    	public void setprev(Node<T> node){
    		this.prev = node;
    	}
    	
    	public void setnext(Node<T> next){
    		this.next = next;
    	}
    	
    	public Node<T> getNext(){
    		return this.next;
    	}
    	
    	public Node<T> getPrev(){
    		return this.prev;
    	}
    	
    	public String testString(){
    		String s = "Previous Element: "+getNext().element+"\nThis element: "+element+"\n";
    		return s;
    	}
    	
    }
    I have a list....

    Java Code:
    package listwnodes;
    
    public class SuperList<T> implements SeanList<T>{
    
    	private Node<T> head;
    	private Node<T> tail;
    	private int count;
    	
    		
    	@Override
    	public void addElement(T ele) {
    		// TODO Add element to end older elements go to the front
    		Node<T> tempNode;
    		tempNode = new Node<T>(ele);
    		if(count == 0){
    			
    			tempNode.setprev(tail);
    			tempNode.setnext(head);
    			head.setprev(tempNode);
    			tail.setnext(tempNode);
    			count++;
    		}	else{
    			tempNode.setprev(tail);
    			tempNode.setnext(tail.getNext());
    			tail.setnext(tempNode);
    			count++;
    			System.out.println(tempNode.testString());
    		}
    		
    		
    	}
    
    	@Override
    	public T getElement(int i) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public void removeElement(T ele) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public T getElementFromI(int index) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public void RemoveElement(T ele) {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	public T viewElement(){
    		Node<T> temp = new Node<T>();
    		return null ;
    	}
    	
    	public int compareTo(T ele){
    		return 0;
    	}
    
    	
    
    }
    and my main

    Java Code:
    package mainStuff;
    
    import java.util.Random;
    
    import listwnodes.SeanList;
    import listwnodes.SuperList;
    
    public class MainListTest {
    
    	public static void main(String[] args){
    	//	SeanList<Integer> myList = new SuperList<Integer>();
    		SuperList<Integer> myList = new SuperList<Integer>();
    		//SuperList<Comparable<T>>
    		Random rand = new Random();
    		
    		for (int i = 0; i < 10; i++) {
    			myList.addElement((Integer) rand.nextInt(50)+1);
    		}
    	}
    }
    For some reason when trying to add elements to my list I get a null pointer exception at the line in my SuperList class "head.setprev(tempNode);". Can anyone explain why please. Thank you guys very much.

Similar Threads

  1. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  4. Linked List
    By basma in forum JCreator
    Replies: 0
    Last Post: 02-03-2010, 08:34 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •