This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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"
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).]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!