开发者

circular linked list problem in java

hi im having trouble with my circular linked list class. im suppose to have a circular linked class that runs through a set amount elements. when it reaches the end of the list it moves all the way back to the begining of the list and starts all over kind of like looping over itself. well my problem is i cant get my list to loop proably with the methods i have made. im suppose to have a method that adds elements to the end of the list and a method that sets them to the front of the list. well my set front is not working right so i thought i post and see if any one code help. also i want to run a loop with strings like i want to create a circular linked list that goes through the days of the week starting at sunday ending at saturday then linking saturday to sunday and doing the loop all over agian can anyone show me how to do this in testing my code.

my output is coming out like

Should print 1 2 3 4 1 2 3 4 1 2 3

1 2 3 4 1 2 3 4 1 2 3

开发者_如何转开发Should print 3 4 1 2 3 4 1 2 3 4 1

3 1 2 3 4 1 2 3 4 1 2

Should print 3 4 1 2 -1 3 4 1 2 -1 3

3 1 2 3 4 -1 3 1 2 3 4

Should print 3 1 2 -1 3 1 2 -1 3 1 2

3 1 2 3 -1 3 1 2 3 -1 3

Code:

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;
    }

}

And

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

}


try something like this in the else from 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();


In CircularLinkedList<T>.setFront(T) you're creating a new node and adding that to the front of the circular list rather than finding the node with the given value and modifying CircularLinkedList<T>.head to reference the found node.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜