473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class StackOfObjects {
  5.     private Node first;
  6.  
  7.  
  8.     public class Node {
  9.         private Object item;
  10.        int goals=0;
  11.         boolean qualify =true;
  12.         private Node next;
  13.  
  14.         public Node(){
  15.             item=null;
  16.             goals=0;
  17.             qualify=true;
  18.         }
  19.  
  20.         public Node(Object item, int goals, boolean qualify){
  21.             this.item=item;
  22.             this.goals=goals;
  23.             this.qualify=qualify;
  24.         }
  25.     }
  26.  
  27.  
  28.  
  29.  
  30.     public StackOfObjects() {
  31.         first = null;
  32.                     }
  33.  
  34.  
  35.     public boolean isEmpty() { return (first == null); }
  36.  
  37.  
  38.     public int random(int goals){
  39.  
  40.         Random random=new Random();
  41.         return goals=random.nextInt(6);
  42.  
  43.     }
  44.     public void print(){
  45.  
  46.                          Country s=(Country) pop();
  47.  
  48.                             System.out.println ( s.name+"   "+random(0)+" "+printQual(true)) ;
  49.     }
  50.  
  51.     public String printQual(boolean qualify){
  52.         String result;
  53.         if(qualify==true){
  54.             result="In";
  55.         }
  56.         else{
  57.             result="Out";
  58.         }
  59.         return result;
  60.  
  61.     }
  62.     public void round1(boolean qualify, int goals){
  63.         Node curr=first;
  64.         if (qualify==true){
  65.             if(curr.goals>curr.next.goals){
  66.                 curr.next.qualify=false;
  67.             }
  68.                 else  {curr.qualify=false;
  69.                         }
  70.             curr=curr.next.next;
  71.         }
  72.     }
  73.     public void remUnqualify(){
  74.         Node curr=first;
  75.         Node prev=first;
  76.  
  77.         if(curr.qualify==true){
  78.             prev=curr;
  79.             curr=curr.next;
  80.             remUnqualify();
  81.         }
  82.         else if (curr.qualify==false){
  83.             prev=curr.next;
  84.             curr=curr.next;
  85.  
  86.         }
  87.  
  88.     }
  89.     public boolean getQualify(boolean qualify) {
  90.                 return qualify;
  91.             }
  92.     public void push(Object item) {
  93.         Node oldfirst = first;
  94.         first = new Node();
  95.         first.item = item;
  96.         first.next = oldfirst;
  97.     }
  98.     public void setGoals(int goals){
  99.         Random rand = new Random();
  100.         goals=rand.nextInt(6);
  101.  
  102.     }
  103.  
  104.  
  105.  
  106.     public Object pop() {
  107.         if (isEmpty()) throw new RuntimeException("Stack underflow");
  108.         Object item = first.item;      
  109.         first = first.next;            
  110.         return item;                   
  111.     }
  112.  
  113.     public static void main(String[] args) {
  114.         StackOfObjects stack = new StackOfObjects();
  115.  
  116.  
  117.  
  118.         try{
  119.  
  120.              FileInputStream fstream = new FileInputStream("textfile.txt");
  121.  
  122.              DataInputStream in = new DataInputStream(fstream);
  123.                  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  124.              String strLine;
  125.  
  126.              while ((strLine = br.readLine()) != null)   {
  127.  
  128.                  Country count=new Country();
  129.                  count.name=strLine;
  130.                  stack.push(count);
  131.  
  132.  
  133.              }
  134.             System.out.println ("\nReturning what is in the stack\n");
  135.             while( ! stack.isEmpty() ){
  136.  
  137.                 stack.print();
  138.             }
  139.  
  140.  
  141.           while( ! stack.isEmpty() ){
  142.              stack.round1(false, 0);
  143.              stack.remUnqualify();
  144.  
  145.           }
  146.           System.out.println ("\nReturning what is in the stack\n");
  147.            while( ! stack.isEmpty() ){
  148.  
  149.                stack.print();
  150.            }
  151.  
  152.              in.close();
  153.              }catch (Exception e){
  154.                System.err.println("Error: " + e.getMessage());
  155.  
  156.  
  157.          }
  158.                  }
  159. }
  160.  
  161.  
  162.  
May 5 '11 #1
0 1227

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kay | last post by:
This function is used getline function to get data. The data is stored as String. I want to add it in a linked list. However, the strcpy that I have pointed cause the program segementation fault....
5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
11
by: bofh1234 | last post by:
Hello, I am having a problem with linked lists. My program is based on a client server model. The client sends some packets of data to the server. The server reads those packets and is...
4
by: Niks | last post by:
Hey.. I tried to creat a linked list by using a structure defined in a class as follows: class LL { struct node { int data; node *next,*previous
1
by: codergem | last post by:
Helo friends!! I m having problems in understanding that how every node is added at last.Could anyone explain me this in much better manner. Well here it is.... we use a local "reference pointer"...
2
by: zl2k | last post by:
hi, I have one program runs fine on my i386 linux box but get the "glibc detected *** corrupted double-linked list" error on the x86_64 linux box. Please help me to figure out if it is my...
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.