Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple linked list problem

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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:

    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Simple linked list problem

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/43534-simple-linked-list-problem.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Simple linked list problem

    You need to attach the textfile.txt file. It will help with working out your issue..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 05:54 PM
  2. Linked List problem, please help.
    By Axeander in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 21st, 2010, 04:01 PM
  3. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 11:33 AM
  4. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 02:33 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM