# Problem with Nodes - need help!

Printable View

• 03-20-2010, 03:14 PM
Smithy
Problem with Nodes - need help!
I've got a couple of issues with this darn topic, please help me and tell me what's wrong..! Probably something quite basic but still...

1) Write a code that returns an integer chain that hold numNodes values of random numbers between 1-100.
Code:

```        public static Node<Integer> createRandomChain(int numNodes)         {                 Random r= new Random();                 Node<Integer> chain= new Node<Integer>(r.nextInt(100));                 Node<Integer> pos=chain;                 for(int i=0;i<numNodes;i++)                 {                         pos.setNext(new Node<Integer> (r.nextInt(100)));                         pos=pos.getNext();                 }                 return chain;```
2) Write a code that returns a character chain, but removes the sequence of identical characters, leaving only one.
For example: k->k->k->b->b->k->z->e->e->z
will return: k->b->k->z->e->z
Code:

```public static void compressSequences(Node<Character> chain)         {                 Node<Character> temp= new Node<Character>(null);                 Node<Character> pos=chain;                 while(pos!=null)                 {                         if(pos.getInfo()==pos.getNext().getInfo())                         {                                 temp=pos.getNext();                                 pos.setNext(temp.getNext());                         }                         pos=pos.getNext();                 }```
• 03-20-2010, 03:41 PM
JosAH
Quote:

Originally Posted by Smithy
I've got a couple of issues with this darn topic

What are the issues? Compilation problems? Runtime exceptions? Please be detailed about those issues if you want to get help.

kind regards,

Jos
• 03-20-2010, 03:54 PM
Smithy
Compilation problems, I assume. The code isn't working properly.
When I try to run them I don't get the full solution.. For the random one I only get the last (or first) node and for the sequence problem I only get the first Node..

I'm assuming something is either wrong with the main code, or with the function code..
Code:

```                Node<Integer> chain= createRandomChain(30);                 System.out.println(chain);                 Node<Character> test= new Node<Character>('z', new Node<Character>('z', new Node<Character>('a', new Node< Character> ('a'))));                 compressSequences(test);                 System.out.println(test);```
• 03-20-2010, 04:47 PM
JosAH
Quote:

Originally Posted by Smithy
Compilation problems, I assume. The code isn't working properly.

Hold on for a second: when your compiler complains about something no .class file is generated and you can't run anything at all. Is your compiler whining about something?

kind regards,

Jos