import java.util.NoSuchElementException; /** * Author by flo on 10/25/15. * Project Num : Project 4 * Purpose : To demonstrate the use of LinkedList, Error Handling and text processing * Algorithm : Take a node and put it in this list then stack them * : * Compile : javac Project4Driver.java * : java Project4Driver */ public class LinkedList { // class data members private Node head; public int size; // Default EMPTY node Constructor public LinkedList() { this.head = new Node("", null); this.size = 0; } // insert method which will add DATA to the end of this list. public void insert( String d ) { // set data to a new Node, assign head node as current node Node tmpNode = new Node(d); Node currentNode = this.head; // loop through linkedlist to find the next node in the list while (currentNode.getNext() != null) { currentNode = currentNode.getNext(); } // add this node as the next node and // add 1 to size of this linked list currentNode.setNext(tmpNode); this.size++; } // insert method which will add DATA to // the specified Node to the list. public void insert(String d, int index) { // set data to a new Node, assign head node as current node Node tmpNode = new Node(d); Node currentNode = this.head; // loop through linkedlist by index , get next node as current Node for (int i = 1; i < index && currentNode.getNext() != null; i++) { currentNode = currentNode.getNext(); } // add this node as the next node and // add 1 to size of this linked list tmpNode.setNext(currentNode.getNext()); currentNode.setNext(tmpNode); // increment the number of elements variable this.size++; } // return size of linkedlist public int size() { return this.size; } // return true if size of linked list is 0 public boolean isEmpty() { return size() == 0; } // return a string data from a particular node public String get(int index) { // check to see if index is equal or less than 0 if (index <= 0) return null; // get the head node as current node Node currentNode = this.head.getNext(); for (int i = 1; i < index; i++) { if (currentNode.getNext() == null) return null; currentNode = currentNode.getNext(); } // return data of the last node in the list return currentNode.getData(); } // Return string data of the first node public String getHead() { // check to see if there is a first node in this linkedlist if ( this.head == null ) { throw new NoSuchElementException("NO Node exists"); } else // return the data of the head node return this.head.data; } // remove a mode and return true/false based on node index public boolean remove(int index) { // checking out of range values // i think its one of the requirements if (index < 1 || index > size()) return false; // set head node equals current node Node currentNode = this.head; for (int i = 1; i < index; i++) { if (currentNode.getNext() == null) return false; currentNode = currentNode.getNext(); } // set node to next next node currentNode.setNext(currentNode.getNext().getNext().getNext()); // decrease the size of the Linked list this.size = this.size -2; return true; } // search a list of nodes to find data public String contains(String data) { // setup temp values Node currentNode = this.head; String line=""; String[] slist; String value; // outer loop to loop through nodes search : while (currentNode != null) { line = currentNode.getData(); slist = line.split(" "); // inner loop to loop through data for ( String sr : slist) { value = sr.trim(); if ( value.equals(data) ) { line = currentNode.getData(); break search; } // for some reason, if not found, it will return the last nodes data // did i missed something here or somewhere in this code ??? // --ok, fixed it.-- else line = ""; } // roll to next node currentNode = currentNode.getNext(); } return line; } // This code should print the data in the current node // otherwise i can force this IDE to generate toString method. public void prtNode() { Node currentNode = head.getNext(); String mess; mess = "<" + currentNode.getData() + ">"; System.out.println(mess); } }