The Artima Developer Community
Sponsored Link

Java Answers Forum
java linklist debug error

0 replies on 1 page.

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 0 replies on 1 page
Tom Zang

Posts: 1
Nickname: tomzang
Registered: Aug, 2003

java linklist debug error Posted: Aug 14, 2003 11:06 AM
Reply to this message Reply
Advertisement
Hello friends,

I got couple errors for my program and need help. See if you can help.

The errors states:

LinkList.java.29: variable trailp might not have been initialized
trailp.next = new Node(B, p);
^
LinkList.java.44: variable trailp might not have been initialized
trailp.next = p;
^
Here is my code:

import java.io.*;
import java.util.*;


public class LinkList
{ private Node first;

class Node
{ int data;
Node next;

public Node (int iData, Node iNext)
{ data = iData;
next = iNext;
}
} // end class Node

//=======================================================================

public void insert(int B)
{ Node p = first, trailp;
while (p != null && p.data < B)
{ trailp = p;
p = p.next;
}
if (p == first)
first = new Node(B, first);
else
trailp.next = new Node(B, p);
}
//-----------------------------------------------------------------------

public boolean delete(int B)
{ Node p = first, trailp;
while ( p != null && p.data != B)
{ trailp = p;
p = p.next;
}
if (p == null)
return false;
if (p == first)
first = first.next;
else
trailp.next = p.next;

return true;
}
//-----------------------------------------------------------------------

public LinkList copy( )
{ LinkList C = new LinkList( );

if (first == null)
{ C.first = null;
return C;
}
C.first = new Node(first.data, null);
Node last = C.first;
Node p = first.next;
Node q;

while (p != null)
{ q = new Node(p.data, null);
last.next = q;
last = q;
p = p.next;
}
return C;
}
//-----------------------------------------------------------------------

public void merge(LinkList L1, LinkList L2, LinkList L3)
{ Node p = first;
if (L1.first == null)
{ L3.first = L2.first;
return;
}
if (L2.first == null)
{ L3.first = L1.first;
return;
}
Node p1, p2, last;
if (L1.first.data < L2.first.data)
{ L3.first = L1.first;
p1 = L1.first.next;
last = L3.first;
p2 = L2.first;
}
else
{ L3.first = L2.first;
p2 = L2.first.next;
last = L3.first;
p1 = L1.first;
}

while (p1 != null && p2 != null)
{ if (p1.data < p2.data)
{ last.next = p;
last = p1;
p1 = p1.next;
}
else
{ last.next = p2;
last = p2;
p2 = p2.next;
}
}
if (p1 == null)
last.next = p2;
else
last.next = p1;
return;

} // end merge
//-----------------------------------------------------------------------

public static void main(String[] arg) throws IOException
{ BufferedReader br = new BufferedReader(new FileReader(arg[0]));
String line = br.readLine();
StringTokenizer data;
int cToken;

LinkList list = new LinkList();

while (line != null)
{ data = new StringTokenizer(line);
cToken = data.countTokens();
for (int i = 0; i < cToken; i++)
list.insert(Integer.parseInt(data.nextToken()));
line = br.readLine();
}

// method sort the list
//list.sortList();

// method to print
list.output(arg[0], arg[1]);
}
//-----------------------------------------------------------------------

public void output(String inFile, String outFile) throws IOException
{
PrintWriter pw = new PrintWriter(new FileWriter(outFile));
pw.println("Input file: " + inFile +
"\nOutput file: " + outFile +
"\nWritten by: Tom Zang\n");
pw.println("Output result");
pw.println();
int count = 0;
for (Node cursor = first ; cursor != null ; cursor = cursor.next)
{ pw.println(cursor);
count++;
}
for (int i = 0; i < 80; i++)
pw.print("_");
pw.println();
pw.println();
pw.println("Size of the list = " + count);
pw.close();
}
} // end LinkListTest class

Topic: Counter Newbie Help Previous Topic   Next Topic Topic: Persistence in Swing App

Sponsored Links



Google
  Web Artima.com   

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