Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Help on resetting my own linked list.

807607Dec 18 2006 — edited Dec 18 2006
I got a Binary tree project, but to get things started, i decided to put all the elements into a LinkedList first.

Anyway, it seems i forgot how to go back to the root of my linkedList. I can traverse through the LinkedList, but it seems that whenever I try to call it again, it's already at the end of the List. My linkedList consist of a Integer, and a the next Node is called "left" atm.

Here's my code:
class MyTree { //class definition
	class Node {
		int data;
		Node left;
		Node right;
	}
private Node root;
private int [] rdata;
Node m = new Node();
BufferedReader in;

   public void generate(int n, int range){
		Random r = new Random();
		rdata  = new int[n];
     	        for (int i = 0; i<n; i++) {
      	             rdata[i] = r.nextInt(range);
                }
					
		
		Node b = new Node();
		for (int j = 0; j<n; j++){  
			b.data = rdata[j];
			m.left = b;
			m = m.left;
			System.out.println("m.data " + m.data);
	        }
			
Now whenever I try to call it back again, It would only print the last integer in the LinkedList, so I had to use an array to set it up it again basically.
	public void show() {
			Node b = new Node();
			for (int j = 0; j<rdata.length; j++){  
			b.data = rdata[j];
			m.left = b;
			m = m.left;
			System.out.println("m.data " + m.data);
			}
	}
I'll like to traverse through the list again without setting up the linked list every time.

Thanks in Advance.

Message was edited by:
Vertix

Message was edited by:
Vertix

Message was edited by:
Vertix

Message was edited by:
Vertix

Message was edited by:
Vertix
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 15 2007
Added on Dec 18 2006
9 comments
194 views