嗨我的循環鏈表類遇到問題。即時通訊假設有一個循環鏈接的類,通過一定數量的元素運行。當它到達列表的末尾時,它會一直移動到列表的開頭,並開始進行各種類型的循環。以及我的問題是我不能讓我的名單循環與我已經做出的方法。即時通訊假設有一個方法,將元素添加到列表的末尾以及將它們設置到列表的前面的方法。以及我的前臺不能正常工作,所以我想我發佈,看看是否有任何代碼的幫助。我也想運行一個字符串循環,比如我想創建一個循環鏈表,從週六開始到週六結束,然後將星期六和星期日連接起來,然後循環遍歷每一個循環,任何人都可以告訴我如何在測試我的代碼時做到這一點。java中的循環鏈表問題
我的輸出出來像
應該打印1 2 3 4 1 2 3 4 1 2 3
應該打印3 4 1 2 3 4 1 2 3 4 1
應打印3 4 1 2 -1 3 4 1 2 -1 3
3 1 2 3 4 -1 3 1 2 3 4
應打印3 1 2 -1 3 1 2 -1 3 1 2
3 1 2 3 -1 3 1 2 3 -1 3
代碼:
public class LinkedListIterator<T> implements Iterator<T> {
private PublicNode<T>first;
private PublicNode<T>current;
public LinkedListIterator(PublicNode<T> first){
this.first =first;
current = first;
}
public boolean hasNext() {
return current!=null;
}
public T next() {
if(!hasNext()){
throw new NoSuchElementException();
}
T result = current.getElement();
current = current.getNext();
return result;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public void setFirst(PublicNode<T> first) {
this.first = first;
}
public PublicNode<T> getFirst() {
return first;
}
}
而且
public class CircularLinkedList<T> implements CircularList<T> {
private PublicNode<T> head;
private PublicNode<T> tail;
private int size;
public CircularLinkedList() {
head = null;
tail = null;
size = 0;
}
//bigO(1)
public PublicNode<T> getHead() {
return head;
}
//bigO(1)
public void setHead(PublicNode<T> head) {
this.head = head;
}
//bigO(1)
public PublicNode<T> getTail() {
return tail;
}
//bigO(1)
public void setTail(PublicNode<T> tail) {
this.tail = tail;
}
//bigO(1)
public int getSize() {
return size;
}
//bigO(1)
public void setSize(int count) {
this.size = count;
}
//bigO(1)
public boolean isEmpty() {
return tail == null || head == null;
}
// add element to the end of the list
public void addLast(T element) {
PublicNode<T> node = new PublicNode<T>(element);
if(this.tail==null){
node.setNext(null);
node.setPrevious(null);
this.tail=node;
}else{
PublicNode<T> oldTail = this.tail;
oldTail.setNext(node);
node.setNext(head);
node.setPrevious(oldTail);
this.tail =node;
}if(this.head==null){
this.head=node;
}
this.size++;
}
// set element to be front of the list
//bigO(n)
public void setFront(T element) {
PublicNode<T> node = new PublicNode<T>(element);
if (isEmpty()) {
throw new NoSuchElementException();
}else{
PublicNode<T> oldHead = this.head;
oldHead.setPrevious(node);
node.setNext(oldHead);
node.setPrevious(null);
this.head=node;
}
if(this.tail==null){
this.tail=node;
}
this.size++;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CircularList<Integer> list = new CircularLinkedList<Integer>();
for (int i = 1; i <= 4; i++) {
list.addLast(i);
}
System.out.println("\nShould print 1 2 3 4 1 2 3 4 1 2 3");
Iterator<Integer> iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.setFront(3);
System.out.println("Should print 3 4 1 2 3 4 1 2 3 4 1");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.addLast(-1);
System.out.println("Should print 3 4 1 2 -1 3 4 1 2 -1 3");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.remove(4);
System.out.println("Should print 3 1 2 -1 3 1 2 -1 3 1 2");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
}
}
你有沒有嘗試用調試器逐步執行代碼? – 2011-04-03 16:11:50
沒有看到'CircularLinkedList.iterator()'的定義,這個問題是無法回答的。 – Eric 2011-04-03 16:15:35
@Oli - 通過調試器步進似乎被視爲一種幾乎沒有人現在教導的黑色藝術...... jikes! – 2011-04-12 17:19:09