Results 1 to 3 of 3
  1. #1
    babe20042004 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Simple linked list problem

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  3. [SOLVED] Problem with Linked List
    By sh4dyPT in forum New To Java
    Replies: 7
    Last Post: 05-22-2009, 04:49 PM
  4. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •