This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes need linked list help  :-) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need linked list help  :-)" Watch "need linked list help  :-)" New topic
Author

need linked list help :-)

Josh Wetzel
Greenhorn

Joined: Sep 30, 2000
Posts: 3
Hi, I'm trying to write a program that will accept string input and sort values using the insertion sort via a linked list. I would greatly appreciate if someone could shed some light on how to sort values via a linked list. Thanks Josh
My code follows:
import java.io.*;
import java.lang.*;
//
//
class List{

private Node head;

List() {
head = null;
}
void add_item(String s) {
if (head == null) {
List my_list = new List();
return;
}
if ((s.compareTo(head.s)) < 0) {
Node head = new Node();
return;
}
else {
for (Node t = head; t != null; t = t.next) {
t.item = s;
}
}
Node temp = new Node();
temp.item = s;
temp.next = head;
head = temp;
}
void show_list(){
for (Node t = head; t != null; t = t.next) {
System.out.println(t.item);
}
}
private class Node {
String item;
Node next;
}
void list_menu() {
System.out.println("List Maintainer");
System.out.println("A - Add word to the list");
System.out.println("D - Display the list");
System.out.println("M - Display this menu");
System.out.println("Q - Quit the program\n\n");
}
}
//
//
//
public class Lister_2 {
public static void main(String args[]){
List my_list = new List();
try{
my_list.list_menu();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String choice;
System.out.print("Enter choice: ");
choice = in.readLine().toUpperCase();
String line_in;

while( !(choice.equals("Q"))) {
if (choice.equals("A")) {//adds word to list
System.out.println("Enter word: ");
line_in = in.readLine().toUpperCase();
my_list.add_item(line_in);
}
else if (choice.equals("D")) { //display the list
my_list.show_list();
}
else if (choice.equals("M")) { //display the menu
my_list.list_menu();
}
else if (choice.equals("Q")) { //quit the program
System.exit(0);
}
else {
System.out.println("That choice was not recognized\n");
my_list.list_menu();
}
System.out.print("Enter choice: ");
choice = in.readLine().toUpperCase();
}
} catch (IOException e) { System.out.println("IO ERROR"); }
System.out.println("\n\n");
} // main
} // L
jungec
Greenhorn

Joined: Mar 07, 2001
Posts: 1
I would modify your add_item() method to insert each Node object into the list in sorted order; However, I am not familiar with the insertion sort algorithim. Found a useful graphical representation of insertion sort graphically. you will need lots of extra node pointers to do the sorting after the fact.
http://blackcat.brynmawr.edu/~spoonen/JavaProject/sorter.html
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
jungec,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a proffesional forum and would prefer that you use your REAL name.

[This message has been edited by Cindy Glass (edited March 08, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need linked list help :-)