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 2 of 2

Thread: Looking for help with a circular linked list

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looking for help with a circular linked list

    I'm new to java specifically and object oriented programming in general. I'm working on a Circular Linked List program. I have a moveFirst() method and a moveFirst(N) method that will move the pointer to the second element of the list or move it a variable number of times, respectively. The program compiles and runs but when I call one of these two methods the tail node is not printed. I'm posting the CircularList class and the ListDriver because these are the only two that I have really written, but I also have CircularListInterface List and Node classes which were provided by instructor and have remained unedited.

     public class CircularList<T> implements List<T>{
     
       /**first and last items in list */
          private Node<T> head, tail;
     
       	/**int used to keep track of the number of items in a list */
          int count;
     
          /**CircularList constructor method*/
          public CircularList() {
             tail = new Node<T>(null);
             head = new Node<T>(null, tail);
             count = 0;
          }
     
       	/**size accessor method for the numerical size of the list.
       	@return count*/
          public int size(){
             return count;
          }
          public boolean isEmpty(){
          /**add determines if a list is empty.
          @return count as zero.*/
             return count == 0;
          }
       	/**contains determines whether the list contains any items.
       	@param index numbered position of pointer
       	@return true if the list contains items */
          public boolean contains(T item){
             return indexOf(item) != -1;
          }
       	/**add determines if an item can be added to the list.
       	@param item a link in the list*/
          public boolean add(T item){
             tail.setData(item);
             Node<T> temp = new Node<T>(null);
             tail.setNext(temp);
             tail = tail.getNext();
             count++;
             return true;
          }
          @Override
          public String toString() {
             String s = "";
             for (Node<T> p = head.getNext(); p != tail; p=p.getNext()){
                s += p.getData().toString() + " ";
             }
             return s;
          }
     
       /**remove determines whether an item can be removed.
       	@param index numbered position of pointer
       	@return true if an item can be removed */
          public boolean remove(T item){
             int inx = indexOf(item);
             if (inx==-1)
                return false;
             Node<T> p = head;
          //after this loop, p refers to the node BEFORE the node to delete
             for (int i=0; i<inx; i++) {
                p = p.getNext();
             }
             p.setNext(p.getNext().getNext());
             return true;
          }
       /**clear clears list.
       	*/
          public void clear(){
             head = new Node<T>(null);
             count = 0;
          }
       	/**get accessor method.
       	@param index numbered position of pointer
       	@throws ArrayIndexOutOfBoundsException
       	@return p.getData() */
          public T get(int index){
     
             if (index<0 || index>=count)
                throw new ArrayIndexOutOfBoundsException("No element at " + index);
             int current;
             current = 0;
             Node<T> p = head.getNext();
             while (current<index-1){
                p = p.getNext();
                current++;
             }
             if(current==index){
                p = head.getNext();
             }
     
             return p.getData();
     
          }
       	/** set saves elements in list
       	@param index numbered position of pointer
       	@element an item
       	@throws ArrayIndexOutOfBoundsException
       	@return save */
          public T set(int index, T element){
             if (index<0 || index>=count)
                throw new ArrayIndexOutOfBoundsException("No element at " + index);
             T save;
             Node<T> p = head.getNext();
             for (int i=0; i<index; i++)
                p = p.getNext();
             save = p.getData();
             p.setData(element);
             return save;
          }
       	/**add will add a node to the end of the list.
       	@param index numbered position of pointer
       	@param item
        */
          public void add(int index, T item){
             int current;
             current = 0;
             if(head==null){
                head = new Node<T>(item);
                head.setNext(tail);
                count++;
             }
             else{
             if (index<0 || index>count)
                throw new ArrayIndexOutOfBoundsException("Cannot add at " + index);
             Node<T> p = head;
     
             for (int i=0; i<index; i++){
                p = p.getNext();
                p.setNext(new Node<T>(item,p.getNext()));
     
             }
            } 
             count++;
          }
       	/**remove will remove a node from the list.
       	@param index numbered position of pointer
       	@return save */
          public T remove(int index){
             int current;
             current = 0;
             if (index<0 || index>=count)
                throw new ArrayIndexOutOfBoundsException("No element at " + index);
     
             Node<T> p = head.getNext();
             while (current<index-1){
                p = p.getNext();
                current++;
             }
             if(current==index){
                p = head.getNext();
             }
             T save = p.getNext().getData();
             p.setNext(p.getNext().getNext());
             return save;
          }
       	/**IndexOf Keeps an index of the position of the pointer. 
       	@param T
       	@param item
       	@return -1 of item is not in the list.
       	@return idx
       	*/
          public int indexOf(T item){ // return -1 if item is not in the list
             Node<T> p = head.getNext();
             int idx = 0;
             while(p!=head && !item.equals(p.getData())){
                p = p.getNext();
                idx++;
             }
             if (p==head)
             // return -1 if item is not in the list
                return -1;
             return idx;
          }
          /** moveFirst moves the pointer to the first element of the list up one.
       If myList contains [one,two,three,four], after myList.moveFirst();
       it would then contain [two,three,four,one]
       */
          public void moveFirst(){
             Node<T> p = head.getNext();
             head = p.getNext();
          }
       /** moveFirst(N) moves the pointer to the first element of the list up N.
       If myList contains [one,two,three,four], after myList.moveFirst(3);
       it would then contain [four,one,two,three]
       @param N the number of nodes to move forward
       */
          public void moveFirst(int N){
             Node<T> p = head.getNext();
             for (int i = 0; i<N; i++)
                head = p.getNext();
          }
     
     
       }




    public class ListDriver{
    public static void main(String[] args) {
     CircularList<Character> list = new CircularList<Character>();
     list.add('Z');
      list.add('R');
      list.add('S');
      list.moveFirst(2);
     System.out.println(list);
     }
    }

    I'm only in my second semester of Intro to Algorithmic design and I haven't had a really great instructor so I'm not that knowledgeable but any assistance would be appreciated.
    Last edited by Norm; February 21st, 2013 at 05:35 PM. Reason: added / in end code tag


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help with a circular linked list

    How are you debugging the code? I don't see any calls to the println() method to print out values as the code is executed. You need to add some println statements to tell you what the computer sees when the code executes so you can see where the program is going wrong.

    There is no way to compile and test the code without all the classes it uses.
    If you don't understand my answer, don't ignore it, ask a question.

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. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 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