haisa jane
Posts: 1
Nickname: haisa
Registered: Nov, 2007
|
|
Re: Trouble sorting a linked list....
|
Posted: Nov 1, 2007 12:53 PM
|
|
> 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,
|
|