Results 1 to 3 of 3
Thread: Simple linked list problem
- 05-05-2011, 04:13 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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()); } } }
- 05-05-2011, 02:32 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Crossposted at Simple linked list problem
- 05-05-2011, 03:26 PM #3
Similar Threads
-
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
[SOLVED] Problem with Linked List
By sh4dyPT in forum New To JavaReplies: 7Last Post: 05-22-2009, 04:49 PM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks