Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

need linked list help :-)  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!