The Artima Developer Community
Sponsored Link

Java Answers Forum
Trouble sorting a linked list....

3 replies on 1 page. Most recent reply: Nov 2, 2007 5:29 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Derp Derpy

Posts: 4
Nickname: arkhan
Registered: Oct, 2006

Trouble sorting a linked list.... Posted: Oct 25, 2006 7:53 AM
Reply to this message Reply
Advertisement
I''m trying to sort a linked list. What it is doing is reading in a text file, and then it is to sort the words of that text file. Ive got it to read in the text file, and it will output the list from start to finish...but I cant understand how to get it to sort :(

Here is my code.. also, I know cisio is a useless file...but my professor demands we use it...

Heres the code!

import java.io.*;
 
public class cisio
{
    static boolean EndOfFile = false;
    
    public cisio()//constructor
    {
        EndOfFile = false;
    }
    
    static int GetChar()
    {
        int c;
        try
        {
            c = System.in.read();
            if (c == -1)
            {
                EndOfFile = true;
            }
            return c;
        }
        catch (IOException e)
        {
            EndOfFile = true;
            return -1;
        }
    }
    
    public static boolean eof()
    {
        return EndOfFile;
    }
    
    public static String GetLine()
    {
        int i;
        i = GetChar();
        String s = "";
        while(i>0 && i!= '\n')
        {
            if (i != '\r')
            {
                s = s + (char) i;
                i = GetChar();
            }
        }
        return s;
    }
    
    public static int GetInt()
    {
        return Integer.parseInt(GetLine());
    }
    
    public static double GetDouble()
   {
        return Double.parseDouble(GetLine());
    }
} 
 
 
NODE.JAVA
import java.util.*;
import java.text.*;
 
class Node
{
    int numlines;
    String Symbol;
    int lines[];
    Node next;
    
    Node(Token t)
    {
        Symbol = new String(t.token);
        lines  = new int [200];
        lines[0] = t.linenr;
        numlines = 1;
        next = null;
    }
    
    void addline(int n)
    {
        lines[numlines++] = n;
    }
    
    void display()
    {
        int i;
        System.out.print(Symbol+" ");
        for (i = 0; i<numlines; i++)
        {
            System.out.print(lines[i]+" ");
        }
        System.out.println();
    }
}
 
 
INDEX4.JAVA
class Index4
{
    Node head;
    Index4()
    {
        head = null;
    }
    
    void display()
    {
        Node p = head;
     
        while (p!=null)
        {
            p.display();
            p = p.next;
        } 
    }
    
    void add(Token t)
    {
        Node p = head;
        //See if this token is already on list
        while(p!=null)
        {
            if(p.Symbol.equals(t.token))
            {
                p.addline(t.linenr);
                return;
            }
            p = p.next;
        }
        //not found, add at start of list
        p = new Node(t);
        p.next = head;
        head = p;
    }
}
 
 
TOKEN.JAVA
import java.util.*;
import java.text.*;
 
class Token
{
String token;
int linenr;
private StringTokenizer tokens;
 
public Token()
{
String s;
token = new String();
s = cisio.GetLine();
tokens = new StringTokenizer(s);
linenr = 1;
System.out.println(linenr+". "+s);
}
 
public boolean getToken()
{
String s;
while(!tokens.hasMoreTokens())
{
if(cisio.eof())
return false;
s = cisio.GetLine();
if(cisio.eof())
return false;
tokens = new StringTokenizer(s);
linenr += 1;
System.out.println(linenr+". "+s);
}
token = tokens.nextToken();
return true;
}
 
public void display()
{
System.out.println("Token ='"+token+"' line number = "+linenr);
}
}
 
LAB4.JAVA (test file)
public class Lab4
{
    public static void main(String args[])
    {
        Token t = new Token();
        Index4 index = new Index4();
        while (t.getToken())
        {
            //t.display();
            index.add(t);
        }
        index.display();
    }
}



Thanks! :) PS: The text file that is being read in, is also the first part of the output.


Derp Derpy

Posts: 4
Nickname: arkhan
Registered: Oct, 2006

Re: Trouble sorting a linked list.... Posted: Oct 25, 2006 9:02 AM
Reply to this message Reply
It just occured to me that I didnt provide the output. oops!

here it is.

1. PROGRAMMING PROVERBS
2.
3. Don't get suckered in by the comments -- they can be terribly misleading.
4. Debug only code.
5.
6. It is easier to change the specification to fit the program than vice versa.
7.
8. Technological progress has merely provided us with more efficient means for going backwards.
9.
10. The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.
11.
12. If the code and the comments disagree, then both are probably wrong.
13.
wrong. 12
probably 12
are 12
both 12
then 12
disagree, 12
and 12
code 12
If 12
yet. 10
it 10
only!): 10
experts 10
(for 10
Optimization 10
Second 10
it. 10
do 10 10
Optimization: 10
Program 10 10
of 10 10
Rule 10 10
First 10
The 10 10
backwards. 8
going 8
for 8
means 8
efficient 8
more 8
with 8
us 8
provided 8
merely 8
has 8
progress 8
Technological 8
versa. 6
vice 6
than 6
program 6
fit 6
specification 6
change 6
to 6 6
easier 6
is 6
It 6
code. 4
only 4
Debug 4
misleading. 3
terribly 3
be 3
can 3
they 3
-- 3
comments 3 12
the 3 6 6 12 12
by 3
in 3
suckered 3
get 3
Don't 3 10 10
PROVERBS 1
PROGRAMMING 1

haisa jane

Posts: 1
Nickname: haisa
Registered: Nov, 2007

Re: Trouble sorting a linked list.... Posted: Nov 1, 2007 12:53 PM
Reply to this message Reply
> I''m trying to sort a linked list. What it is doing is
> reading in a text file, and then it is to sort the words
> of that text file. Ive got it to read in the text file,
> and it will output the list from start to finish...but I
> cant understand how to get it to sort :(
>
> Here is my code.. also, I know cisio is a useless
> file...but my professor demands we use it...
>
> Heres the code!
>
>
import java.io.*;
>  
> public class cisio
> {
>     static boolean EndOfFile = false;
>     
>     public cisio()//constructor
>     {
>         EndOfFile = false;
>     }
>     
>     static int GetChar()
>     {
>         int c;
>         try
>         {
>             c = System.in.read();
>             if (c == -1)
>             {
>                 EndOfFile = true;
>             }
>             return c;
>         }
>         catch (IOException e)
>         {
>             EndOfFile = true;
>             return -1;
>         }
>     }
>     
>     public static boolean eof()
>     {
>         return EndOfFile;
>     }
>     
>     public static String GetLine()
>     {
>         int i;
>         i = GetChar();
>         String s = "";
>         while(i>0 && i!= '\n')
>         {
>             if (i != '\r')
>             {
>                 s = s + (char) i;
>                 i = GetChar();
>             }
>         }
>         return s;
>     }
>     
>     public static int GetInt()
>     {
>         return Integer.parseInt(GetLine());
>     }
>     
>     public static double GetDouble()
>    {
>         return Double.parseDouble(GetLine());
>     }
> } 
> 
> 
> NODE.JAVA
> import java.util.*;
> import java.text.*;
>  
> class Node
> {
>     int numlines;
>     String Symbol;
>     int lines[];
>     Node next;
>     
>     Node(Token t)
>     {
>         Symbol = new String(t.token);
>         lines  = new int [200];
>         lines[0] = t.linenr;
>         numlines = 1;
>         next = null;
>     }
>     
>     void addline(int n)
>     {
>         lines[numlines++] = n;
>     }
>     
>     void display()
>     {
>         int i;
>         System.out.print(Symbol+" ");
>         for (i = 0; i<numlines; i++)
>         {
>             System.out.print(lines[i]+" ");
>         }
>         System.out.println();
>     }
> }
> 
> 
> INDEX4.JAVA
> class Index4
> {
>     Node head;
>     Index4()
>     {
>         head = null;
>     }
>     
>     void display()
>     {
>         Node p = head;
>      
>         while (p!=null)
>         {
>             p.display();
>             p = p.next;
>         } 
>     }
>     
>     void add(Token t)
>     {
>         Node p = head;
>         //See if this token is already on list
>         while(p!=null)
>         {
>             if(p.Symbol.equals(t.token))
>             {
>                 p.addline(t.linenr);
>                 return;
>             }
>             p = p.next;
>         }
>         //not found, add at start of list
>         p = new Node(t);
>         p.next = head;
>         head = p;
>     }
> }
> 
> 
> TOKEN.JAVA
> import java.util.*;
> import java.text.*;
> 
> class Token
> {
> String token;
> int linenr;
> private StringTokenizer tokens;
> 
> public Token()
> {
> String s;
> token = new String();
> s = cisio.GetLine();
> tokens = new StringTokenizer(s);
> linenr = 1;
> System.out.println(linenr+". "+s);
> }
> 
> public boolean getToken()
> {
> String s;
> while(!tokens.hasMoreTokens())
> {
> if(cisio.eof())
> return false;
> s = cisio.GetLine();
> if(cisio.eof())
> return false;
> tokens = new StringTokenizer(s);
> linenr += 1;
> System.out.println(linenr+". "+s);
> }
> token = tokens.nextToken();
> return true;
> }
> 
> public void display()
> {
> System.out.println("Token ='"+token+"' line number =
> "+linenr);
> }
> }
> 
> LAB4.JAVA (test file)
> public class Lab4
> {
>     public static void main(String args[])
>     {
>         Token t = new Token();
>         Index4 index = new Index4();
>         while (t.getToken())
>         {
>             //t.display();
>             index.add(t);
>         }
>         index.display();
>     }
> }
> 

>
>
> Thanks! :) PS: The text file that is being read in, is
> also the first part of the output.

Hi,
I have this problem too. I have that program with the same output. I do'nt know how I can do?:(
Please help me.
Thank you,

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Trouble sorting a linked list.... Posted: Nov 2, 2007 5:29 AM
Reply to this message Reply
1. Before you read the input file,
create a List.
2. As you read the input file, add each word to the list.
3. After you've finished reading the input file,
sort the list.
4. Output the sorted list in the format you require.

For code examples, Google the words java, list and sort. There are many examples to be found.

Flat View: This topic has 3 replies on 1 page
Topic: Trouble sorting a linked list.... Previous Topic   Next Topic Topic: abt java beans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use