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

Thread: help with circular linked list

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with circular linked list

    i have an assignment to create a circular linked list.
    after adding to my list and printing it out it looks likt
    "one two three four"
    after using a moveFirst() method and say i wanted to start with three it should look like this
    "three four one two"
    However i cannot get my moveFirst() method to work properly i have tried many different ways but cannot get it to work. I understand all the concepts of how to do this it is just the actual coding to get it to do so.
    public class LinkedList<T> implements ListInterface<T> {
          private Node<T> head, tail;
          private int count;
     
          public LinkedList() {
             count=0;
             head = new Node<T>(null);
             head.setNext(new Node<T>(null));
             tail = head.getNext();
             if(tail.getNext()==null)
             	tail.setNext(head);
          }
     
     
          public void add(T elt){
             tail.setData(elt);
             tail.setNext(new Node<T>(null));
             tail=tail.getNext();
             count++;
          }
     
          public void addFirst(T elt){
             head.setNext(new Node<T>(elt, head.getNext()));
             count++;
          }
     
     
     
          public void addAt(T elt, int idx){
             int i;
             Node<T> p=head;
             if (idx<0 || idx>count)
                throw new IllegalArgumentException("Out of bounds: " + idx);
             for (i=0; i<idx; i++)
                p = p.getNext();
             p.setNext(new Node<T>(elt,p.getNext()));
             count++;
          }
     
          public int find(T elt){
             Node<T> p = head.getNext();
             int idx = 0;
             while (p!=tail) {
                if (p.getData().equals(elt))
                   return idx;
                p=p.getNext();
                idx++;
             }
             return -1;
          }
     
          public boolean contains(T elt){
             Node<T> p = head.getNext();
             while (p!=tail) {
                if (p.getData().equals(elt))
                   return true;
                p=p.getNext();
             }
             return false;
          }
     
          public boolean isEmpty(){
             return count==0; }
     
          public void makeEmpty(){
             count=0;
             head.setNext(tail);
          }
     
          public void printList(){
             Node<T> p= head.getNext();
             while (p!=tail) {
                System.out.print(p + " ");
                p = p.getNext();
             }
             System.out.println();
          }
     
          public T findKth(int idx){
             int i;
             Node<T> p = head.getNext();
             if (idx<0 || idx>=count)
                throw new IllegalArgumentException("Out of bounds: "+idx);
             for (i=0; i<idx; i++)
                p=p.getNext();
             return p.getData();
          }
     
          public boolean remove(T elt){
             int place = find(elt);
             return removeAt(place);
          }
     
          public boolean removeAt(int idx){
             Node<T> p = head;
             if (idx<0 || idx>=count)
                return false;
             for (int i=0; i<idx; i++)
                p=p.getNext();
             p.setNext(p.getNext().getNext());
             count--;
             return true;
          }
     
          public int size(){
             return count; }
     
          public Object[] toArray(){
             Node<T> p = head.getNext();
             Object[] newArray = new Object[count];
             int i=0;
             while (p!=tail) {
                newArray[i] = p.getData();
                ++i;
                p=p.getNext();
             }
             return newArray;
          }
    	public void moveFirst(T newTail, T newFirst) {
    		tail.setData(newTail);
    		System.out.println(tail);
    		head.setData(newFirst);
    		System.out.println(head);
     
     
    	}
          public static void main(String[] args) {
             LinkedList<String> ll = new LinkedList<String>();
     
             ll.add("one");
             ll.add("two");
             ll.add("three");
             ll.add("four");
             ll.printList();
             ll.moveFirst("one","three");
     
     
     
          }
       }
    Last edited by Seans0007; March 11th, 2012 at 11:25 PM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: help with circular linked list

    Where is the moveFirst()?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with circular linked list

    public void moveFirst(T newTail, T newFirst) {
    		tail.setData(newTail);
    		System.out.println(tail);
    		head.setData(newFirst);
    		System.out.println(head);
     
     
    	}

    they print lines were for me to just see what exactly was happening to make sure i was doing it correctly.
    I can set the head and the tail to the new values given but when i reprint the line in the main method it just simply reprints the original line????

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: help with circular linked list

    A comment on you debug output using printlns. If you add an ID String, a long list if printouts will be easier to understand:
    System.out.println("head="+head);

Similar Threads

  1. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM
  2. Need help to understanding with Doubly-linked circular Lists
    By mrdeath9x in forum Java Theory & Questions
    Replies: 3
    Last Post: May 5th, 2011, 04:51 AM
  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, 03: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, 11:40 AM