import java.util.ArrayList; import javax.swing.JOptionPane; /** * * @author Bill Kraynek */ public class Example5 { static ArrayList memory = new ArrayList(); static int start; /** * @param args the command line arguments */ public static void main(String[] args) { start = 0; memory.add(new Node("This", 1)); memory.add(new Node("is", 2)); memory.add(new Node("example", 3)); memory.add(new Node("sentence", -1)); display(start); memory.set(1, new Node(memory.get(1).data, memory.size())); memory.add(new Node("an", 2)); display(start); int p = start; int q = -1; while (p != -1 && !memory.get(p).data.equals("example")) { q = p; p = memory.get(p).next; }// end while Node node = new Node("insert", p); memory.add(node); if (q != -1) { memory.get(q).next = memory.size() - 1; } else { start = memory.size() - 1; }// end if display(start); }// end main static void display(int list) { int p = list; String out = ""; while (p != -1) { out += memory.get(p).data + " "; p = memory.get(p).next; }// end while out = out.substring(0, out.length() - 1) + ".\n"; out += "Start = " + start + "\nMemory:\n"; int index= 0; for( Node node : memory ) { out += index + ": " + node + "\n"; index++; }// end for JOptionPane.showMessageDialog(null, out); }// end display static class Node { String data; int next; Node(String data, int next) { this.data = data; this.next = next; }// end constructor public String toString() { return "(" + data + " , " + next + ")"; }// end toString }// end Node }