繁体   English   中英

Java中的循环链表问题

[英]circular linked list problem in java

您好,我遇到了我的循环链表类的麻烦。 我想有一个循环链接的类,该类贯穿一定数量的元素。 当到达列表的末尾时,它将一直返回列表的开头,并从头开始,就像遍历自身一样。 好吧,我的问题是我无法让自己的清单与我所使用的方法正确地循环。 我想有一个方法将元素添加到列表的末尾,并将其设置到列表的前面。 好吧,我的电视机前无法正常工作,所以我想我发布一下,看看是否有任何代码帮助。 我也想用一个字符串运行一个循环,比如我想创建一个循环的链表,该链表遍历一周中的每一天,从周日开始,到周六结束,然后将周六链接到周日,并在整个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());
        }
    }

}

setFront()else尝试这样的else

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();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM