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.

Results 1 to 3 of 3

Thread: Needing a little advice on a method in my circular linked list.

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Needing a little advice on a method in my circular linked list.

    So, the method that I'm having problems with at the moment is my insertAt method, which takes an integer and an item as parameters and then adds the item to the circular linked list at the index of the list that the integer specifies. If the index passed through is less than 0 or greater than the number of elements in the list then it throws a new Index Out of Bounds Exception. I can't seem to figure out what the problem is with this method. Any advice would be greatly appreciated. The error I'm getting is this:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: -1
    at clinkedlist.CLinkedList.insertAt(CLinkedList.java: 65)
    at clinkedlist.CLinkedListTest.test10(CLinkedListTest .java:114)
    at clinkedlist.CLinkedListTest.run(CLinkedListTest.ja va:27)
    at clinkedlist.CLinkedListTest.main(CLinkedListTest.j ava:14)

    import java.util.*;
    class Node<T> {
        T data;
        Node<T> next;
     
        Node(T dat){
            data = dat;
            next = null;
        }
     
        Node(T dat, Node<T> nxt){
            data = dat;
            next = nxt;
        }
    }
     
    public class CLinkedList<T>{
        private Node<T> tail;
        private int count;
        //private CLinkedList list;
     
        public CLinkedList(){
            tail = null;
        }
     
        public void add(T item){
            if (tail != null){
                Node<T> newNode = new Node<T>(item, tail.next);
                tail.next = newNode;
            }
            else{
                tail = new Node<T>(item, null);
                tail.next = tail;
            }
     
            count++;
        }
     
        public void append(T item){
            if (tail != null){
                Node<T> newNode = new Node<T>(item, tail.next);
                tail.next = newNode;
                tail = newNode;
            }
            else{
                tail = new Node<T>(item, null);
                tail.next = tail;
            }
     
            count++;
        }
     
        public void insertAt(int index, T item){
            if (index < 0 || index > count){
                throw new IndexOutOfBoundsException(Integer.toString(index));       //****This is where the problems start.****//
            }
            else if (index == 0){
                add(item);
            }
            else{
                Node<T> node = getNode(index - 1);
                node.next = new Node<T>(item, node.next);
                count++;
            }
        }
     
        public boolean remove(T item){
            if (tail != null){
                Node<T> remNode = tail;
     
                do{
                    if (remNode.next.data.equals(item)){
                        if (remNode.next == tail && tail != tail.next){
                            remNode.next = remNode.next.next;
                            tail = tail.next;
                        }
                        else if (remNode.next == remNode.next.next){
                            tail = null;
                        }
                        else{
                            remNode.next = remNode.next.next;
                        }
                        return true;
                    }
                    remNode = remNode.next;
                }while (remNode != tail);
            }
            return false;
            /*Node<T> temp = new Node<T>(item, tail);
            if (tail != null){
                tail = tail.next;
            }
            if (temp != null){
                count--;
                remove(temp.data);
                return true;
            }
            return false;*/
        }
     
        public T removeAt(int index){
            if (index < 0 || index > count){
                throw new IndexOutOfBoundsException(Integer.toString(index));
            }
            if (index >= 0){
                Node<T> node = getNode(index);
                remove(node.data);
                count--;
            }
            return null;
     
        }
     
        public void clear(){
     
        }
     
        public void setAt(T item, int index){
            if (index < 0 || index >= count ){
                throw new IndexOutOfBoundsException(Integer.toString(index));
            }
            Node<T> node = getNode(index);
            //T result = node.data;
            node.data = item;
        }
     
        public T getAt(int index){
            if (index < 0 || index > count){
                throw new IndexOutOfBoundsException(Integer.toString(index));
            }
     
            Node<T> node = getNode(index);
            return node.data;
        }
     
        public int indexOf(T item){
            Node<T> temp = new Node<T>(item, tail);
            //Node<T> temp = tail;
            int i;
     
            for (i = 0; !(temp.data).equals(getNode(i)) && temp != null; i++){
                temp = temp.next;
            }
            if (i == count){
                return -1;
            }
            return i;
        }
     
        public int size(){
            return count;
        }
     
        public boolean isEmpty(){
            if (count == 0){
                return true;
            }
            else{
                return false;
            }
        }
     
        private Node<T> getNode(int index){
            Node<T> node = tail;
            int i;
     
            for (i = 0; i < index && node != null; i++){
                node = node.next;
            }
            return node;
        }
    }
    public class CLinkedListTest {
        public static void main(String[] args){
            CLinkedListTest me = new CLinkedListTest();
            me.run();
        }
     
        public void run(){
            test1();
            test2();
            test3();
            test4();
            test5();
            test6();
            test7();
            test8();
            test9();
            test10();
            test11();
            test12();
            test13();
            test14();
            test15();
            test16();
            test17();
            test18();
            test19();
            test20();
            test21();
        }
     
        //Test to see that the initial list is empty.
        public void test1(){
            CLinkedList test = new CLinkedList();
            assert 0 == test.size();
        }
     
        //Test to see if append works on an empty CLL.
        public void test2(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.append("A");
            assert 1 == test.size();
        }
     
        //Test to see if append is working on a non-empty CLL.
        public void test3(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.append("A");
            test.append("B");
            assert 2 == test.size();
        }
     
        //Test to see if add is working on an empty CLL.
        public void test4(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            assert 1 == test.size();
        }
     
        //Test to see if add is working on a non-empty CLL.
        public void test5(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.add("B");
            assert 2 == test.size();
        }
     
        //Test to see if remove is working on an empty CLL.
        public void test6(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.remove("A");
            assert 0 == test.size();
            assert(test.remove("A") == false);
        }
     
        //Test to see if remove is working on a non-empty CLL.
        public void test7(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.remove("A");
            assert 0 == test.size();
            assert(test.remove("A") == true);
        }
     
        //Test to see if insertAt is working on an empty CLL.
        public void test8(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.insertAt(0, "A");
            assert 1 == test.size();
        }
     
        //Test to see if insertAt is working on a non-empty CLL.
        public void test9(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.add("B");
            test.add("C");
            test.insertAt(1, "D");
            assert 4 == test.size();
        }
     
        //Test to see if insertAt throws an out OutOfBoundsException.
        public void test10(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.insertAt(-1, "A");
            assert 0 == test.size();
        }
     
        //Test to see if removeAt is working on an empty CLL.
        public void test11(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.removeAt(0);
            assert 0 == test.size();
        }
     
        //Test to see if removeAt is working on a non-empty CLL.
        public void test12(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.add("B");
            test.add("C");
            test.removeAt(2);
            assert 2 == test.size();
        }
     
        //Test to see if removeAt throws an OutOfBoundsException.
        public void test13(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.removeAt(-1);
            assert 0 == test.size();
        }
     
        //Test to see if setAt overwrites index location in an empty CLL.
        public void test14(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.setAt("A", 0);
            assert 1 == test.size();
        }
     
        //Test to see if setAt overwrites index location in a non-empty CLL.
        public void test15(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.add("B");
            test.add("C");
            test.setAt("D", 1);
            assert 3 == test.size();
        }
     
        //Test to see if setAt throws an OutOfBoundsException.
        public void test16(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.setAt("A", -1);
            assert 0 == test.size();
        }
     
        //Test to see if getAt is working on a non-empty CLL.
        public void test17(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.add("B");
            test.getAt(1);
            assert 2 == test.size();
        }
     
        //Test to see if getAt throws an OutOfBoundsException.
        public void test18(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.getAt(-1);
            assert 0 == test.size();
        }
     
        //Test to see if indexOf is working in a non-empty CLL.
        public void test19(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.add("B");
            test.add("C");
            test.indexOf("B");
            assert 3 == test.size();
            assert(test.indexOf("B") == 1);
        }
     
        //Test to see if isEmpty is working on an empty CLL.
        public void test20(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.isEmpty();
            assert 0 == test.size();
            assert(test.isEmpty() == true);
        }
     
        //Test to see if isEmpty is working on a non-empty CLL.
        public void test21(){
            CLinkedList<String> test = new CLinkedList<String>();
            test.add("A");
            test.isEmpty();
            assert 1 == test.size();
            assert(test.isEmpty() == false);
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Needing a little advice on a method in my circular linked list.

    throw new IndexOutOfBoundsException(Integer.toString(index)) ;
    says to print:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: -1 ...etc
    which tells you the method was called with index given a value of -1. If -1 is not a valid value go back and see why the method is being called with index as a -1.

  3. The Following User Says Thank You to jps For This Useful Post:

    bankston13 (October 21st, 2012)

  4. #3
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Needing a little advice on a method in my circular linked list.

    I figured it out. I was purposely passing the -1 index value to test if the method threw the exception, but it seems that would be unnecessary because its tested in the other test methods. Thanks for the help.

Similar Threads

  1. Need a little help removing from a circular linked list
    By bankston13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 20th, 2012, 02:50 AM
  2. help with circular linked list
    By Seans0007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 12th, 2012, 02:37 PM
  3. [SOLVED] Circular Linked List---Need Help ASAP (before midnight)
    By The Dark Mathematician in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 17th, 2011, 02:24 PM
  4. Help with a doubly linked circular list
    By TeamRival in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:59 PM
  5. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM