Is this correct?
Ark here again. :)
So, this assignment is to "modify the following java code to produce an index in sorted order of the words read from standard input"
So my question is: The code Ive got here, is it doing the above?
CISIO.JAVA (< Did someone mention at one point that this file is useless as Java already does it?)
import java.io.*;
publicclass cisio
{
staticboolean EndOfFile =false;
public cisio()//constructor
{
EndOfFile =false;
}
staticint GetChar()
{
int c;
try
{
c = System.in.read();
if (c == -1)
{
EndOfFile =true;
}
return c;
}
catch (IOException e)
{
EndOfFile =true;
return -1;
}
}
publicstaticboolean eof()
{
return EndOfFile;
}
publicstatic String GetLine()
{
int i;
i = GetChar();
String s ="";
while(i>0 && i!='\n')
{
if (i !='\r')
{
s = s + (char) i;
i = GetChar();
}
}
return s;
}
publicstaticint GetInt()
{
return Integer.parseInt(GetLine());
}
publicstaticdouble 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 =newint [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);
}
}
[/code]
LAB4.JAVA (test file)
publicclass Lab4
{
publicstaticvoid main(String args[])
{
Token t =new Token();
Index4 index =new Index4();
while (t.getToken())
{
//t.display();
index.add(t);
}
index.display();
}
}
or is this just traversing the linked list from start to end...?
If so, what is the best approach to sorting everything?
Stuff that I gathered is Shell sort, or partitioning?...maybe?
Thanks">
[7933 byte] By [
Arkhana] at [2007-10-3 8:10:35]

I would love to chuck cisio.java out. Unfortunatley, this is an assignment from my professor and he insist we use the code given, which includes that file.
The program prints my output in reverse order, which makes sense since it is coming from a linked list.
Im trying to figure out how I would sort this linked list so that the output prints the words in sorted order.
Here is the input/output for when this is run.
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
I cant seem to figure out an efficient way to sort this list....
*bump* :)
I decided merge sorting would be pretty doable for this shindig...and started writing code for it. Hit a roadblock.
Im being told with list.Symbol, that it cant find Symbol.list is passed into the function as Node type, so shouldn't it be able to find it?
Here is my code as of right now.
CISIO.JAVA = remains unchanged and useless :-P
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();
}
}
Here is Index4. It contains what i started as a mergesort.
import java.util.*;
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;
}
public Node merge_sort (Node list)
{
if (list == null || list.next == null)
return list; // checks for empty or single list
Node list2 = split (list);
list = merge_sort (list);
list2 = merge_sort (list2);
return merge (list, list2);
}
public Node split (Node list)
{
if (list == null || list.next == null)
return null;
Node list2 = list.next;
list.next = list2.next;
list2.next = split (list2.next);
return list2;
}
public Node merge (Node list, Node list2)
{
if (list == null)
return list2;
if (list2 == null)
return list;
if (list1.Symbol >< list2.Symbol)
{
list.next = merge (list.next, list2);
return list;
}
else
{
list2.next = merge (list, list2.next);
return list2;
}
}
}
I cant seem to figure out why it says it cant find symbol, but I do think that once I jump that roadblock, Ill use that comparesto() method.....
Did I miss anything, or does this look safe?
:) Thanks