Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 34 of 34

Thread: LinkedList -- insert()

  1. #26
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: LinkedList -- insert()

    Hey Norm, walked away for a bit.

    The error after compiling.
    OrionException in thread "main"
    Pointer Out Of Bounds!
    PointerOutOfBoundsException
    at MySinglyLinkedList.getNext(MySinglyLinkedList.java :128)
    at MainClass.main(MainClass.java:11)
    Now, I made a quick change and I got the newMember to print..by some miracle. It is still throwing the error though. Line 128 is in post 24, the third snippet of code, and contains, " else throw new PointerOutOfBoundsException(); "
    Why did it work using firstNode.link = head; , but not firstNode.setLink(head); ? Don't they essentially accomplish the same task?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: LinkedList -- insert()

    Hard to say what the problem is without code that can be compiled and executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: LinkedList -- insert()

    main
    public class MainClass {
     
    	public static void main(String[] args) throws PointerOutOfBoundsException {
     
    	//	StringX star = new StringX("Betelgeuse");
    	//	System.out.println(star.toString());
     
    		MySinglyLinkedList<String> myStringList = new MySinglyLinkedList<String>();
    		myStringList.addToFront("Orion");
    		System.out.println(myStringList.getNext());
    	}
    }
    public interface SinglyLinkedList<T> {
     
    	public void traverse();
     
    	public void addToFront(T newMember);
     
    	public void addToRear(T newMember);
     
    	//public Node search(T key);
     
    	public void insert(T newMember, int index) throws IndexOutOfBoundsException;
     
    	//public boolean delete(T key);
    }
    public class MySinglyLinkedList<T> implements SinglyLinkedList<T> {
    	protected Node<T> head, tail, current;
    	int size = 0;
     
    	// /The node accessor method returns a reference to the node, this can be
    	// avoided by making the Node a private inner class.
    	private class Node<T> {
     
    		protected T element; // contents within node
    		private Node<T> link; //node pointer
     
    		public Node() {
    			element = null;
    			link = null;
    		}
     
    		public Node(T element) {
    			this.element = element;
    		}
     
    		public void setLink(Node<T> link) {
    			this.link = link;
    		}
     
    		public Node<T> getLink() {
    			return link;
    		}
     
    		public void setElement(T element) {
    			this.element = element;
    		}
    		public T getElement() {
    			return element;
    		}
     
    		public String toString(T element) {
    			return (element + " ");
    		}
    	}
     
    	// ///////////////END OF NODE CLASS////////////////////
    	public MySinglyLinkedList() {
    		head = null;
     
    		current = null;
    	}
     
    	public void traverse() {
    		current = new Node<T>();
    		reset();
    		while (current != null) {
    			System.out.println(current.getElement());
    			current = current.getLink();
    		}
    	}
    //add node to list - test for existing nodes
    	public void addToFront(T newMember) {
    		//initialize new node object
    		Node<T> firstNode = new Node<T>(newMember);
     
    		if (empty()) {
    			firstNode.link = head;
    			//firstNode.setLink(head);
    			firstNode.setElement(newMember);
    			firstNode = head;
    			current = head;
    			System.out.println(newMember);
    		} 
    		else {
    			Node<T> newNode = new Node<T>();
    			newNode.link = head;
    			head = newNode;
    		}
    	}
     
    	public void addToRear(T newMember) {
    		Node<T> lastNode = new Node<T>(newMember);
     
    		if (tail == null) {
    			head = tail = lastNode;
    		} else {
    			tail.link = lastNode;
    			lastNode = tail;
    		}
    	}
     
    	public void insert(T newMember, int index) throws IndexOutOfBoundsException {
     
    	}
     
    	/*
    	 * public Node search(T key) { reset(); T itemPosition;
    	 * 
    	 * while(current != null) { itemPosition = current.element;
    	 * 
    	 * if(itemPosition.equals(key)) { return current; } else return null; }
    	 * return current.getNext(); }
    	 */
    	/*public boolean delete(T key) {
    		reset();
    		Node<T> tempNode = new Node<T>(key);
     
    		if(head == current) {
    			head = tempNode.link;
    		}	else if(tail == current) {
    				tail = previous.link;
    			}
     
     
    		return false;
    	}
    */
    	private boolean empty() {
    		return (head == null);
    	}
     
    	public int size() {
    		Node<T> newNode = new Node<T>();
    		while (head != null) {
    			size++;
    			tail = newNode.link;
    		}
    		return size;
    	}
     
    	public void reset() {
    		current = head;
    	}
     
    	public Node<T> getNext() throws PointerOutOfBoundsException {
     
    		if (hasNext()) {
    			current = current.getLink();
    			return current;
    		} else 
    			throw new PointerOutOfBoundsException();
     
    	}
     
    	public boolean hasNext() {
    		if (current == null) {
    			return false;
    		}
    		return (current.getLink() != null);
    	}
     
    }

    public class PointerOutOfBoundsException extends Exception {
     
    	public PointerOutOfBoundsException() {
    		System.out.println("Pointer Out Of Bounds!");
    	}
    }
    Last edited by javaStooge; July 13th, 2014 at 07:33 PM. Reason: Updated code

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: LinkedList -- insert()

    The posted code does not compile because of missing class definitions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: LinkedList -- insert()

    *Updated* I forgot to add the exception class.

    I've outlined the linkedlist concept on pen and paper, and feel I have a fairly good grasp of it. (I'll be sure to use the "process" you mentioned earlier next time before getting neck deep into a program.)

    firstNode.setElement(newMember); should store the contents of the node object and allow the element to be retrieved....is this an unnecessary step?

    As far as the pointer exception being thrown...I'm assuming this is because current is retaining its null value within the next methods, instead of the current = head value within addToFirst(). Would that be correct?
    Last edited by javaStooge; July 13th, 2014 at 07:46 PM.

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: LinkedList -- insert()

    The lack of comments makes it hard to understand what the variables are for and what the logic is.
    For example: what is the value that is supposed to be in the current variable? Don't bother posting it here. It should be in the code.

    A suggestion for debugging:
    Add a toString() method to the Node class that returns a String with the value of the element and the value of the link. Also add a toString() method to the MSLL class that returns the value of head, current and tail. Then when the MSLL variable is printed all of the contents will be printed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    javaStooge (July 13th, 2014)

  8. #32
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: LinkedList -- insert()

    MySinglyLinkedList.Node is a raw type. References to generic type MySinglyLinkedList<T>.Node<T> should be parameterized
    I realized my Node type does not need a generic type (located in the Node class), but then I get the error above when I remove it. Is there a way to avoid this? I've also tried to define the type, Node, within the List class but I get the error below:

    MySinglyLinkedList.Node is a raw type. References to generic type MySinglyLinkedList<T>.Node<T> should be parameterized

    I didn't post the code again, since the content in question is already on this thread.

  9. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: LinkedList -- insert()

    I get the error above
    You left off the part of the error message that shows where the error is happening. What is the source line?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #34
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: LinkedList -- insert()

    LinkedList Definition:An ordered set of data elements, each containing a link to its successor (and sometimes its predecessor).
    Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, including lists (the abstract data type), stacks, queues, associative arrays, and S-expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation.

    Types of linked list:1)single linked list.2)double linked list.3)circular linked list.
    Example:Node node1 = new Node();
    Node node2 = new Node();
    LinkedList list = new LinkedList();
    list.add(node1);
    list.add(node2);

    //then my node1 will know who it's next is:
    assertEquals(node2, node1.next());
    Regards
    Srinath.M
    (Automation COE) **REMOVED /core-qa-offerings/test-automation
    Last edited by srinath12; July 24th, 2014 at 06:07 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. [SOLVED] linkedlist concurrentmodificationexception
    By dEvilKinG in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2013, 12:20 PM
  2. boolean linkedlist help
    By gspease839 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 11th, 2013, 04:32 PM
  3. Arraylist and LinkedList
    By vilkas123 in forum Collections and Generics
    Replies: 1
    Last Post: November 3rd, 2012, 02:20 AM
  4. LinkedList insert, remove and change
    By Alexie91 in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2011, 07:16 AM
  5. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM