简体   繁体   中英

How to print Linked List only if it contains one element in Java

I am using a Linked list/ Iterater combo to compute the Josephus problem, and need it to only print if there is only one element left in the list, but how would I use an if statement to check a condition that references to the size of the list rather than the number in the list with "ourList" being of type "LinkedList"? Here is a piece of the code:

if(atEnd() == true){ 
  //System.out.println("Value removed: " + current.dData);
    previous.next = current.next;
    current = ourList.getFirst();
    //if(ourList == Only contains one element){ <--------------
    //ourList.displayList();
     // }
   }

for reference heres my LinkedList + Iterator:

class Link{

    public int dData;
    public Link next;

    public Link(int data){
        dData = data;
    }

    public void displayLink(){
        System.out.print(dData + " ");
    }

}

class LinkList{

    private Link first;

    public LinkList(){
        first = null;
    }

    public Link getFirst(){
        return first;
    }

    public void setFirst(Link f){
        first = f;
    }

    public boolean isEmpty(){
        return first == null;
    }

    public ListIterator getIterator(){
        return new ListIterator(this);
    }

    public void displayList(){
        Link current = first;
        while(current != null){
            current.displayLink();
            current = current.next;
        }
        System.out.println("");
    }


}

class ListIterator{

    private Link current;
    private Link previous;
    private LinkList ourList;

    public ListIterator(LinkList list){
        ourList = list;
        reset();
    }

    public void reset(){
        current = ourList.getFirst();
        previous = null;
    }

    public boolean atEnd(){
        return(current.next == null);
    }

    public boolean atEndPrevious(){
        return(previous.next == null);
    }

    public Link getCurrent(){
        return current;
    }

    public void nextLink(){
        previous = current;
        current = current.next;
    }

    public void insert(int value){
        Link newLink = new Link(value);
        if(ourList.isEmpty() == true){
            ourList.setFirst(newLink);
            current = newLink;
        }else{
            newLink.next = current.next;
            current.next = newLink;
            nextLink();
        }
    }

    public void beginningPoint(int begin){
        if(begin == 1){
            current = ourList.getFirst();
            previous = null;
            //        System.out.println("Current Location: " + current.dData);
        }else if(begin != 1){
            current = ourList.getFirst();
            previous = null;
            for(int i = 1; i < begin; i++){
                nextLink();
                //          System.out.println("Current Location: " + current.dData);
            }
        }
    }

Adding pseudocode, since it will help you understand the process, but learn by programming it yourself. Also note you could remove some else 's through the behavior of exiting.

All of this functions through the iterator.

if at end
  dont print and exit (0 elements)
else
  get and save next element to variable
  if at end
    print the variable you saved (only 1 element)
  else
    dont print and exit (more than 1 element)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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