// Link List Class // Uses linkNode Class // Authored by Ian Livingstone // March 10th 2008 // B00487841 // CSCI 1101 public class LinkList { // Class Variables private int nodeCount; private linkNode tailNode; // Constructor LinkList () { this.nodeCount = 0; } // Add Node Method -- Adds a Node to the Link List, Returns Void public void addNode (Object data) { // Create Temp Node linkNode _temp = new linkNode(this.tailNode.getPointer(), data); // If Count > 0 -- Set tailNode = _temp Node if (this.nodeCount > 0) { this.tailNode.setPointer(_temp); } else { _temp.setPointer(_temp); } // Set tailNode this.tailNode = _temp; // Increase number of Node's this.nodeCount++; } // Del Node Method -- Deletes a node form the Link List, returns the node that was deleted. public linkNode delNode (Object value) { // Use getNode method to search Node linkNode _temp = getNode(value); // If Number of Nodes is Greater than 1 if (this.nodeCount > 2) { linkNode _tempTwo = getPointerNode(_temp); // Now set Pointer Node to the node being deleted pointer _tempTwo.setPointer(_temp.getPointer()); } else { // Else: This is the last node! linkNode _tempTwo = getPointerNode(_temp); this.tailNode = _tempTwo; } // Decrease Number of Node's this.nodeCount--; // Return the Node return _temp; } // getNode Method -- Searches the Link List for the value, and then returns the Node public linkNode getNode (Object value) { // set Temp Node linkNode _temp = this.tailNode; // Start traversing the Link List _temp = _temp.getPointer(); // While the Pointer dosn't equal the tail node -- OR we have found the value. while (_temp.getPointer() != this.tailNode && _temp.getValue() != value) { _temp = _temp.getPointer(); } // If _temp's value dosn't equal the value we are looking for return null. if (_temp.getValue() != value) { return null; } else { return _temp; } } // traverseList -- Traverses the Link List N results, and then returns that node -- Starts at tail public linkNode traverseList (int n) { // make we havea tleast one node in the list if (this.nodeCount < 1) { return null; } // Start at Tail linkNode _temp = this.tailNode; // Now we are at the begginning: _temp = _temp.getPointer(); for (int i = 1; i <= n; i++) { _temp = _temp.getPointer(); } // Return _temp! return _temp; } // traverseList -- Traverses the Link List N results, starting from the linkNode provided, and then returns the node that it finds. public linkNode traverseList (linkNode _temp, int n) { // make sure we have atleast one node in the list if (this.nodeCount < 1) { return null; } // Start at provided linkNode for (int i = 1; i <= n; i++) { _temp = _temp.getPointer(); } // Return Temp! return _temp; } // getPointerNode Method -- Returns the Node that points to a Node public linkNode getPointerNode (linkNode value) { // set Temp Node linkNode _temp = this.tailNode; // Start the Ride! _temp = _temp.getPointer(); while (_temp.getPointer() != this.tailNode && _temp.getValue() != value) { _temp = _temp.getPointer(); } // If _tmep's value dosn't equal the value we are looking for return null if (_temp.getPointer() != value) { return null; } else { return _temp; } } // isEmpty Method -- Returns if Node is Empty public boolean isEmpty () { if (this.nodeCount == 0) { return true; } else { return false; } } // size Method -- Returns size of Link List public int size () { return this.nodeCount; } // linkNode Class // Private Class private class linkNode { // Class Variables // Reference Pointer linkNode pointer; // Value Pointer Object value; // Class Constructor -- Sets Pointer, and value linkNode (linkNode pointer, Object value) { setPointer(pointer); setValue(value); } // getPointer Method -- Returns the next Node in the List public linkNode getPointer () { return this.pointer; } // toString Method -- Returns value of the next Node in the list public Object getValue () { return this.value; } // setValue Method -- Sets the value of the node public void setValue (Object data) { this.value = data; } // setPointer -- sets the value of the pointer public void setPointer (linkNode Object) { this.value = Object; } } }