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.
Code java:
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();
}
}
Code java:
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.
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.