The Artima Developer Community
Sponsored Link

Java Answers Forum
MergeSort on a Linked list

1 reply on 1 page. Most recent reply: Oct 30, 2006 1:53 AM by Dave Hinton

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 1 reply on 1 page
Derp Derpy

Posts: 4
Nickname: arkhan
Registered: Oct, 2006

MergeSort on a Linked list Posted: Oct 28, 2006 4:08 PM
Reply to this message Reply
Advertisement
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 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 (list.Symbol.compareTo(list2.Symbol) < 0)
{
list.next = merge (list.next, list2);
return list;
}
else
{
list2.next = merge (list, list2.next);
return list2;
}
}
}
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);
}
}

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...)

Thanks guys.


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: MergeSort on a Linked list Posted: Oct 30, 2006 1:53 AM
Reply to this message Reply
Post your unit tests and tell us which ones fail, and how.

Flat View: This topic has 1 reply on 1 page
Topic: JSF CRUD and hibernate Previous Topic   Next Topic Topic: java.awt.Graphics

Sponsored Links



Google
  Web Artima.com   

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