Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Data Structure Problem
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Christ1m




PostPosted: Tue Mar 03, 2009 4:31 pm   Post subject: Java Data Structure Problem

my assignment is to write a 2 functions that check how many elements are in a linked list
one of the functions have to solve it recursively and the other through the use of a loop.
The program should build a list of length 3 and then call the two length functions to find its length. Building the list :

Java:
list1 = new lp(1, new lp(2, new lp(3, null)));


Your program doesn't need any input. It should look like this to the user:

code:
length computed with a loop: 3
length computed with recursion: 3


Java:
// my code:
import java.util.*;
public class lp
{

public int first;
public lp rest;
public lp list1;

public lp(int first1, lp rest1)
{
first = first1;
rest = rest1;
}

public static int count_list(lp list1)
{

int count = 0;
while (list1 != null)
{
  count++;}
return count;
}

public  static int rCount_list(lp list1)
{
if(list1.first == 0)
{
  return 0;
}
else return rCount_list(list1.rest) + 1;

}
public static void main(String[]args)
{
 int first;
 lp rest;
 lp list1;

 list1 = new lp(1, new lp(2, new lp(3, null)));
int num = count_list(list1);
int num1 = rCount_list(list1);
System.out.println("length computed with a loop: " + num);
System.out.println("length computed with recursion: " + num1);
}
}


When I run the code I do not get the expected output. Instead I get, "Unhandled exception: java.lang.NoClassDefFoundError: com/sun/jdi/VMOutOfMemoryException".
Is there something missing in my code or is it totally wrong?

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="java"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Mar 03, 2009 5:06 pm   Post subject: RE:Java Data Structure Problem

I'm not sure I understand your linked-list implementation; it's quite non-standard.

A linked list definition looks more like:
Java:

class LinkedNode {
    public LinkedNode next;  // Note: declaring these as public is bad practice. Instead, you should have methods for getNext(), getValue(), setNext(), setValue() - commonly called "setters and getters"
    public int value;

    public LinkedNode ( int value, LinkedNode next ) {
        this.next = next;
        this.value = value;
    }

}


This was probably reviewed in your class. Take a careful look at your notes, what I've specified here, and what you have as your definition.

From there, you should be able to create a List class that has methods countList() and countListRecursively(). These can then be called by a main class called "ListTest" or similar:

Java:

( put the LinkedNode from above here... )

class List {
    LinkedNode head;

    public int countList() {
        // Fill this in yourself
    }

    public int countListRecursively() {
        // Fill this in yourself
    }

}

public class ListTest {
    public static void main ( String[] args ) {
        // Build the list here
        // Call list.countList() and list.countListRecursively() here
        // Output answers here
    }
}
HellblazerX




PostPosted: Tue Mar 03, 2009 5:24 pm   Post subject: Re: Java Data Structure Problem

Your error is caused by this:
Java:
while (list1 != null)
{
  count++;}

You've got an infinite loop here, and your error was caused by the fact that you ran out of memory (you must have had this running for awhile for that error to show up). You'll need to go through all the nodes in your linked list, and have a variable to keep track of which node you're on. This variable will also be used to determine if you've reached the end of the list.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: