I wrote a merge sort function to sort a linked list but I cant quite get it to work right...
here is the code. Can someone help fix it?
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()); } }
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(); } }
import java.util.*; class Index4 { Node head; Node p = head; Index4() { head = null; }
void display() { while (p!=null) { p.display(); p = p.next; } }
void add(Token t) { //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 void display() { System.out.println("Token ='"+token+"' line number = "+linenr); } }
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.merge_sort(index.head); index.display(); } }
I have been slaving over this for the better half of a week and still cant get it to work properly...The deadline for it is rapidly approaching also (Monday...)