java中的循环链表问题

时间:2011-04-03 16:04:02

标签: java data-structures circular-list

嗨我的循环链表类有问题。我想假设有一个循环链接类,它通过一定数量的元素运行。当它到达列表的末尾时,它一直移动到列表的开头,并开始类似于循环自身。好吧,我的问题是我无法让我的列表顺利循环使用我所做的方法。我想有一个方法可以将元素添加到列表的末尾,并且有一个方法可以将它们设置到列表的前面。好吧,我的前面工作不正常,所以我想我发布,看看是否有任何一个代码帮助。我也想运行一个带字符串的循环,比如我想要创建一个循环链表,这个循环链表经过星期六结束的星期日,然后将星期六连接到星期日,然后将循环连接到整个agian,任何人都可以告诉我如何在测试我的代码时这样做。

我的输出就像

一样
  

应打印1 2 3 4 1 2 3 4 1 2 3
   1 2 3 4 1 2 3 4 1 2 3
  应打印3 4 1 2 3 4 1 2 3 4 1
   3 1 2 3 4 1 2 3 4 1 2
  应打印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());
        }
    }

}

2 个答案:

答案 0 :(得分:0)

else

setFront()中尝试这样的事情
PublicNode<T> node = head.next();
PublicNode<T> nodeToBeFound;
while(node!=head){
    is(node.element() == element)
        nodeToBeFound = node;
    node = node.next();
}
head = nodeToBeFound;
tail = head.previous();

答案 1 :(得分:0)

CircularLinkedList<T>.setFront(T)中,您正在创建一个新节点,并将其添加到循环列表的前面,而不是找到具有给定值的节点,并修改CircularLinkedList<T>.head以引用找到的节点。