//node.java /*--------------------------------------------------------------- ' class node for package dataStruct ' ' Shawn McKenna ' ' DESC: This class is an implementation of a single-linked list and some appropriate ' methods for creating, inserting and deleting single linked nodes. The list holds ' String data, though it can always be changed to hold more fields or objects. ' ' NOTE: Many of these methods are taken directly from Geoff Friesen's "Java 2 by Example" ' a good beginning/intermediate Java book -- though the inserting and deleting methods ' are mine. Since Java does not have pointers, a double linked list is much easier ' to implement than a single linked list. ' Java also has a linked list class for objects (java.util.LinkedList). ' ' GENERIC NOTE: All my code has been tested, my apologies for any unforseen errors. ' Suggestions are always welcome. Feel free to use or change any of this ' code. ' ' CONSTRUCTOR: node (String data) -- public ' ' METHODS: ' setData(String data) -- public ' DESC: sets the data for the current node. ' RETURN TYPE: void ' getData () -- public ' DESC: returns the current string value for the current node. ' RETURN TYPE: String ' setNext (node next) -- public ' DESC: sets the next node for the current node. ' RETURN TYPE: void ' getNext () -- public ' DESC: returns the next node. ' RETURN TYPE: node ' insertB (node topB, String data) -- public ' DESC: inserts a node at the beginning. ' RETURN TYPE: node ' insertA (node top, String data) -- public ' DESC: inserts a node at the end of the linked list. ' RETURN TYPE: node ' insertA (node top, node temp) -- public ' DESC: inserts a node or a linked list at the end of the original linked list. ' RETURN TYPE: node ' insertIB (node top, String data, String after) -- public ' DESC: inserts a node inbetween nodes. It does this by finding the String 'after' ' and inserting the String 'data' after 'after' he he he. ' RETURN TYPE: node ' NOTE: if 'after' does not exist in linked list, the linked list is not changed. ' This can be changed later to possibly add some Exception when the 'after' is ' not found. ' deleteNode (node top, String data) -- public ' DESC: deletes a node where the String 'data' matches the passed parameter. ' RETURN TYPE: node ' NOTE: if 'data' does not exist in linked list, the linked list is not changed. ' This can be changed later to possibly add some Exception when the 'data' is ' not found. ' ' EXAMPLE FOR DECLARING METHODS -- ' node top = new node("rest"); ' top = top.insertB(top, "hello"); ' top = top.insertA(top, "Last"); ' top = top.insertIB(top, "NotWork", "but"); //no but String so linked list is not ' top = top.insertIB(top, "SecondLast", "rest"); ' top = top.deleteNode(top, "but"); //no but String, so linked list is not changed. ' top = top.deleteNode(top, "Last"); ' ' DATE MODIFIED: 11/5/2000 ' VERSION: 1.0 '---------------------------------------------------------------- */ package dataStruct; public class node { private String data; // data for the node private node next; // declare the next node public node (String data) { setData (data); setNext (null); } // end of constructor public void setData (String data) { this.data = data; } // end of method setData public String getData () { return data; } // end of method getData public void setNext (node next) { this.next = next; } // end of method setNext public node getNext () { return next; } // end of method getNext public node insertB (node topB, String data) { node temp = new node (data); temp.setNext (topB); topB = temp; return topB; } // end of method insertB public node insertA (node top, String data) { node temp = new node (data); node last = top; while (last != null) if (last.getNext () != null) last = last.getNext (); else break; if (last != null) last.setNext (temp); else top = temp; return top; } // end of method insertA public node insertA (node top, node temp) { node last = top; while (last != null) if (last.getNext () != null) last = last.getNext (); else break; if (last != null) last.setNext (temp); else top = temp; return top; } // end of method insertA public node insertIB (node top, String data, String after) { node tempIB = null; node insert = null ; // the new node with data while (top != null) { if (tempIB != null) { tempIB = insertA(tempIB, top.getData()); } else tempIB = new node(top.getData()); if (top.getData() == after) { insert = new node(data); // the new node with data node second = top.getNext(); insert.setNext (second); break; } // end of if statement top = top.getNext (); } // end of while tempIB = insertA (tempIB, insert); return tempIB; } // end of method insertIB public node deleteNode (node top, String data) { node temp = top; node prev = null; while (temp != null) { if (temp.getData().equals (data)) { if (prev != null) prev.setNext (temp.getNext()); else top = temp.getNext(); break; } // end of if statement else { prev = temp; temp = temp.getNext(); } // end of else statement } // end of while return top; } // end of method deleteNode } // end of class node