I am working with circular list in java. I am trying to print the data in the list but I am not getting the desired output. I am getting the output as
CircularList@55f96302
CircularList@3d4eac69
CircularList@42a57993
Kindly help to solve this problem.
Thank you!!
import java.util.Iterator;
public class CircularList<T> implements Iterable<T> {
private static class myIterator<T> implements Iterator<T> {
int i = 0;
CircularList<T> myList;
Entry<T> iter;
public myIterator(CircularList<T> list) {
iter = list.head;
myList = list;
}
public boolean hasNext() {
return !(myList.size() == i)
}
public T next() {
//System.out.println("next");
i++;
T nextvalue = iter.value;
//Entry<T> nextnode = new Entry<T>();
iter = iter.next;
return nextvalue;
}
public void remove() {
if(myList.size() == 1) {
myList.head = null;
return;
}
if(iter == myList.head) {
myList.head = myList.head.next;
}
iter.prev.next = iter.next;
iter.next.prev = iter.prev;
}
} // end myIterator
private static class Entry<T> {
Entry<T> next;
Entry<T> prev;
T value;
}
private Entry<T> head;
public int size() {
int index = 0;
Entry<T> before = head.prev;
Entry<T> after = head;
//System.out.println(head.value);
//System.out.println(after.value);
do {
before = after;
after = after.next;
index++;
} while (after != head);
return index;
}
public boolean empty() {
//System.out.println(head == null);
return head == null;
}
public void append(T value) {
Entry<T> newnode = new Entry<T>();
newnode.value = value;
if (empty()) {
head = newnode;
head.next = head;
head.prev = head;
}
else {
Entry<T> before = head.prev;
Entry<T> after = head;
newnode.prev = head.prev;
newnode.next = head;
head.prev.next = newnode;
head.prev = newnode;
}
}
public void insert(int index, T value) {
Entry<T> newnode = new Entry<T>();
newnode.value = value;
if (empty()) {
head = newnode;
head.next = head;
head.prev = head;
}
else {
Entry<T> before = head.prev;
Entry<T> after = head;
if(index == 0) {
head = newnode;
}
else {
while(index > 0) {
before = after;
after = after.next;
index--;
}
newnode.prev = before;
newnode.next = after;
newnode.prev.next = newnode;
newnode.next.prev = newnode;
}
}
} // end insert()
public void remove(int index) {
Entry<T> before = head.prev;
Entry<T> after = head;
while(index > 0) {
before = after;
after = after.next;
index--;
}
after.prev.next = after.next;
after.next.prev = after.prev;
}
public void prepend(T value) {
insert(0, value);
}
public void appendList(CircularList<T> list) {
if (empty()) {
head = list.head;
return;
}
if (list.empty()) return;
Entry<T> a = list.head;
Entry<T> b = list.head.prev;
Entry<T> c = head;
Entry<T> d = head.prev;
a.prev = d;
d.next = a;
b.next = c;
c.prev = b;
}
@Override
public Iterator<T> iterator() {
return new myIterator(this);
//throw new UnsupportedOperationException("Not implemented yet.");
}
public static void toString(CircularList<Integer> list) {
String sol = "";
//for (CircularList<Integer> list : data) {
sol = sol +"\n"+ String.valueOf(list);
//}
System.out.println(sol);
//return sol;
}
}
First, I recommend you not to override the toString() method in that way. Your method is for printing, not 'returning' a string object. And it is totally different from the original toString method. Anyway, the result you are getting now is addresses of objects in circularlist I guess.
for(Integer data : list){
system.out.println(data.toString());
}
and also you have to modify your code like this :)