My function round1(boolean qualify, int goals) is supposed to go through a list of nodes comparing every 2 side by side nodes for the highest goals and then has the boolean variable qualify changed to false for the node with the lowest goals. The nodes with qualify as false are then deleted from the list using remUnqualify() .However when I print the 2nd time around it prints a blank stack.
Here is my code:
import java.io.*; import java.util.*; public class StackOfObjects { private Node first; public class Node { private Object item; int goals=0; boolean qualify =true; private Node next; public Node(){ item=null; goals=0; qualify=true; } public Node(Object item, int goals, boolean qualify){ this.item=item; this.goals=goals; this.qualify=qualify; } } public StackOfObjects() { first = null; } public boolean isEmpty() { return (first == null); } public int random(int goals){ Random random=new Random(); return goals=random.nextInt(6); } public void print(){ Country s=(Country) pop(); System.out.println ( s.name+" "+random(0)+" "+printQual(true)) ; } public String printQual(boolean qualify){ String result; if(qualify==true){ result="In"; } else{ result="Out"; } return result; } public void round1(boolean qualify, int goals){ Node curr=first; if (qualify==true){ if(curr.goals>curr.next.goals){ curr.next.qualify=false; } else {curr.qualify=false; } curr=curr.next.next; } } public void remUnqualify(){ Node curr=first; Node prev=first; if(curr.qualify==true){ prev=curr; curr=curr.next; remUnqualify(); } else if (curr.qualify==false){ prev=curr.next; curr=curr.next; } } public boolean getQualify(boolean qualify) { return qualify; } public void push(Object item) { Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; } public void setGoals(int goals){ Random rand = new Random(); goals=rand.nextInt(6); } public Object pop() { if (isEmpty()) throw new RuntimeException("Stack underflow"); Object item = first.item; first = first.next; return item; } public static void main(String[] args) { StackOfObjects stack = new StackOfObjects(); try{ FileInputStream fstream = new FileInputStream("textfile.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { Country count=new Country(); count.name=strLine; stack.push(count); } System.out.println ("\nReturning what is in the stack\n"); while( ! stack.isEmpty() ){ stack.print(); } while( ! stack.isEmpty() ){ stack.round1(false, 0); stack.remUnqualify(); } System.out.println ("\nReturning what is in the stack\n"); while( ! stack.isEmpty() ){ stack.print(); } in.close(); }catch (Exception e){ System.err.println("Error: " + e.getMessage()); } } }