Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-15-2005, 08:41 PM   #1 (permalink)
 
Newb Techie

Join Date: Dec 2005

Posts: 2

indianj

Default need help with java

hi, i need some help with java. what is the difference between these two different types of Node(s)?

public Node()
{
data = null;
prev = this;
next = this;
}

public Node(Object obj)
{
data = obj;
prev = this;
next = this;
}
indianj is offline  
Old 12-15-2005, 09:13 PM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

I assume these are probably Constructors?

If so, the first one is a default constructor.

The second is a "copy constructor". It is called this because it copies the contents of another Node object upon creation.

They should probably be setting prev and next to NULL, however, rather than this.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 12-16-2005, 01:38 AM   #3 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Looks like you're learning how to overload constructors.

All this is doing is giving you two possible ways to access a function (method, whatever it's called in Java).

The first one will create the node with nothing in the data field, the second one allows you to pass an object as the data. This allows you to use one function call in many ways. Programmers like to be lazy, so why create an ObjectNode and Node function when you can have them both be called Node.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 12-18-2005, 09:32 PM   #4 (permalink)
 
Newb Techie

Join Date: Dec 2005

Posts: 2

indianj

Default

hi, thanks for your help. it makes sense now.

I am doing CircularLinkedList () project and I need your help. In this program user insert value between 1 though 23 and program should create linked list. Something is not right. I wondering if somone could take a look at and give me suggestion how to fix it
Code:
Here is my code:
// Harry Smith

import javax.swing.*;
import java.io.*;


public class CircularLinkedList {
	Node head;
	Node tail;

	public CircularLinkedList()
	{
		Node n = new Node();
		head = n;
		tail = n;
	}

	public CircularLinkedList(Object obj)
	{
		Node n = new Node(obj);
		head = n;
		tail = n;
	}

	public void push(Object obj) //ADD TO HEAD
	{
		if (head.data == null)
		{
			head.data = obj;
		}
		else
		{
			Node n = new Node(obj);
			n.next = head;
			n.prev = tail;
			head.prev = n;
			tail.next = n;
			head = n;
		}
	}

	public Object pop() //POP THE HEAD OFF
	{
		Object obj = head.data;
		head.data = null;
		head = head.next;
		head.prev = tail;
		tail.next = head;
		return obj;
	}

	public void add(Object obj) //ADD TO TAIL
	{
		if (tail.data == null)
		{
			tail.data = obj;
		}
		else
		{
			Node n = new Node(obj);
			n.next = head;
			n.prev = tail;
			head.prev = n;
			tail.next = n;
			tail = n;
		}
	}

	public Object cut() //REMOVE THE TAIL
	{
		Object obj = tail.data;
		tail.data = null;
		tail = tail.prev;
		tail.next = head;
		head.prev = tail;
		return obj;
	}

	public static void main(String args[]) //a test
	{
	//	String = firstNumber;	// string entered by user
		
	//	firstNumber = JOptionPane.showInputDialog("Enter value between 1 through 26");
	//	number1 = Integer.parseInt(firstNumber);
	
	System.out.println ("Please enter Circular Linked List value between 1 through 23");
	
	BufferedReader buffRead = new BufferedReader (new InputStreamReader (System.in));
	
		while (true) {
			try {
				//Reading data from standard input
				String str = buffRead.readLine ();
				//Checking condition of ending the program
				if (str == null || str.equals ("exit")) break;
				
				//Parsing of entered string
				int n	= Integer.parseInt (str);
				//Result evaluation
			// incorrect	int cll = CircularLinkedList ();
				
				//Result output
				System.out.println ("CircularLinkedList");
			}	catch (IOException ioe) {
				
				//Processing of input error from keyboard
				System.err.println ("System Error: I/O Error while reading value.");
				System.err.println ("Application finished.");
				System.exit (0);	
			}	catch (NumberFormatException nfe) {
				System.err.println ("Enter value between 1 and n.");
			//	System.err.println ("String has to represent an non-negative integer.");
			
		}
	}
	
			cll = new CircularLinkedList();	
		CircularLinkedList cll;
	
	
		for (int n = 1; n <= 10; n++)
		{
			int circle = n;
			//Integer circle = new Integer(n);
		//	cll.add(n);
			cll.push(n);
		}
		//y<=21 prints upto 21 lines
		for (int y = 1; y <= 21; y++)
		{
			int circle;
			circle = (int)cll.pop(n);
			System.out.println(n);
			
		
}



class Node
{// creating constructor
	Object data;
	Node prev;
	Node next;
	// create the node with nothing in the data field
	public Node() //default constructor
	{
		data = null;
		prev = this;
		next = this;
	}
	
	// pass an object as the data
	public Node(Object obj)//copy constructor
	{
		data = obj;
		prev = this;
		next = this;
	}
}
}

}

indianj is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On