# help with circular linked list

• March 11th, 2012, 10:19 PM
Seans0007
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.
Code Java:

public class LinkedList<T> implements ListInterface<T> {
private int count;

count=0;
if(tail.getNext()==null)
}

tail.setData(elt);
tail.setNext(new Node<T>(null));
tail=tail.getNext();
count++;
}

count++;
}

public void addAt(T elt, int idx){
int i;
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){
int idx = 0;
while (p!=tail) {
if (p.getData().equals(elt))
return idx;
p=p.getNext();
idx++;
}
return -1;
}

public boolean contains(T elt){
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;
}

public void printList(){
while (p!=tail) {
System.out.print(p + " ");
p = p.getNext();
}
System.out.println();
}

public T findKth(int idx){
int i;
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){
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(){
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);

}
public static void main(String[] args) {

ll.printList();
ll.moveFirst("one","three");

}
}

• March 12th, 2012, 05:27 AM
Mr.777
Re: help with circular linked list
Where is the moveFirst()?
• March 12th, 2012, 02:28 PM
Seans0007
Re: help with circular linked list
Code Java:

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