User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,427 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,019 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1385 | Replies: 7
Reply
Join Date: Oct 2005
Posts: 11
Reputation: brian63304 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
brian63304 brian63304 is offline Offline
Newbie Poster

Need some help

  #1  
Oct 12th, 2005
I have this code and I am trying to input numbers so I can sort them. Could someone help?

Code:
class ListSortExample
{
static class Node
{ int val; Node next;
Node(int v, Node t) { val = v; next = t; }
}
static Node create()
{ Node a = new Node(0, null);
for (In.init(); !In.empty(); )
a.next = new Node(In.getInt(), a.next);
return a;
}
static Node sort(Node a)
{ Node t, u, x, b = new Node(0, null);
while (a.next != null)
{
t = a.next; u = t.next; a.next = u;
for (x = b; x.next != null; x = x.next)
if (x.next.val > t.val) break;
t.next = x.next; x.next = t;
}
return b;
}
static void print(Node h)
{ for (Node t = h.next; t != null; t = t.next)
Sytstem.out.println(t.val + ""); }
public static void main(String[] args)
{ print(sort(create())); }
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 142
Reputation: G-Do is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 11
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Need some help

  #2  
Oct 13th, 2005
What you have here is a so-called "singly-linked list" data structure.

Basically, a singly-linked list is a collection of Node objects. Each Node object has two fields: a data field (often a String or integer, though really it could be anything - even another object), and a link field which "links" the Node object to one other Node object. Conceptually, what this looks like is:
o -> o -> o -> ... -> o
|    |    |           |
d    d    d           d
a    a    a           a
t    t    t           t
a    a    a           a
In this diagram, each circle is a Node. It has attached data (going vertically) and a link to another Node, which itself has attached data and a link to another Node, which itself has attached data and a link to another Node, and so on. The last Node in the chain has attached data, but a null link. The first Node is often called the "head" and the last Node is often called the "tail."
It looks like your main method tries to sort a linked list that is generated by the create() method. But now I'm confused - in the create() method, where did the ln variable come from? What is that?
And what, exactly, do you want to do with this thing?
Vi veri veniversum vivus vici
Reply With Quote  
Join Date: Oct 2005
Posts: 11
Reputation: brian63304 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
brian63304 brian63304 is offline Offline
Newbie Poster

Re: Need some help

  #3  
Oct 13th, 2005
Well, I got this code from a book and I need to insert some numbers in it and have this code sort the nodes. I know that they are linked list nodes... thanks for exlaining it.. i have a better concept of them know.
Anyways, I haven't taken Java for a while and I need to insert this code into a java program that inserts numbers, sort them, and finally ouputs the data.
Second, the 'In' variable was from the book and I don't even know where it came from... first I thought it would be System.In; just like System.out, but it is not... do you get what I mean?
Reply With Quote  
Join Date: Jun 2005
Posts: 142
Reputation: G-Do is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 11
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Need some help

  #4  
Oct 13th, 2005
I see. Where are these numbers coming from? Are they being read in from a file? Are they being typed in while the program runs (via a call to System.in)? Are they entered on the command-line when you call the program? The answer to this question determines how you will create your linked list.

As for how to sort and print it, observe that both the sort() and print() methods accept a single Node as the input. That Node is supposed to be the "head" or the first Node of the linked list. So, in the body of your program, keep track of just the head Node - you don't need to worry about the others because the head connects to the rest of the chain with its .next field. To print the list, just pass the head into print().

The sort() method looks a little funny to me. Is it broken? If I'm reading it correctly, you pass in the head Node, it creates a sorted copy of the list connected to the head, and it returns a new Node called b which is the head of the sorted copy. Yes? If that's the case, all you have to do is set your head Node equal to the value of sort(head), and your list will be sorted.

I hope that helps somewhat.
Vi veri veniversum vivus vici
Reply With Quote  
Join Date: Oct 2005
Posts: 11
Reputation: brian63304 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
brian63304 brian63304 is offline Offline
Newbie Poster

Re: Need some help

  #5  
Oct 13th, 2005
I see what you are talking about... I always used text boxes to insert text in classes. How would I insert the numbers using System.in? Could you help me out with this part. Thanks
Reply With Quote  
Join Date: Jun 2005
Posts: 142
Reputation: G-Do is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 11
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Need some help

  #6  
Oct 13th, 2005
Hi brian63304,

If you want to grab data from the keyboard via System.in, you're going to want to use a BufferedReader. The typical setup is this:
import java.io.*;
public class Something {
     public static void main(String[] args) throws Exception {
          InputStreamReader isr = new InputStreamReader(System.in);
          BufferedReader br = new BufferedReader(isr);
          //--Now, to get a single line of text from the keyboard, just say:
          String input = br.readLine();
          //--To transform a String value into an integer value, say:
          int a = Integer.parseInt(input);
          //--To collect a bunch of integer values, run a while loop.
          //--Stop when the user enters a blank line or hits CTRL-D.
          //--Check for a blank line by checking the length of 'input'.
          //--Check for CTRL-D by checking to see whether input is null.
          //--Don't forget to close your BufferedReader:
          br.close();
     }
}
Every time you get a new number, build a Node for it. Then link the previous Node to this new Node by using the previous Node's .next field. Clear?
Vi veri veniversum vivus vici
Reply With Quote  
Join Date: Oct 2005
Posts: 11
Reputation: brian63304 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
brian63304 brian63304 is offline Offline
Newbie Poster

Re: Need some help

  #7  
Oct 15th, 2005
Ok.... I get the it to run but when I open the Jframe, I can not get it to run.
Reply With Quote  
Join Date: Oct 2005
Posts: 11
Reputation: brian63304 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
brian63304 brian63304 is offline Offline
Newbie Poster

Re: Need some help

  #8  
Oct 19th, 2005
I finally worked out the problem... thanks for the info.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 2:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC