/* LinkedList.java 1.0 */ /* originally from Georgia Tech's CS 1322 Module 27 notes */ /* on Linked Lists, Stacks, and Queues, */ /* with influences from course text, "Java Structures",*/ /* pp. 168-189. */ /* modified/adapted by Sharon Tuttle */ /* last modified: 2-27-03 */ public class LinkedList { /*------------------------------------ fields -------------------------------------*/ // beginning of linked list private ListNode head; // number of items currently in linked list private int count; /*-------------------------------------- constructors ---------------------------------------*/ // creates an empty linked list public LinkedList() { this.head = null; this.count = 0; } /*------------------------------------------------------- accessors -------------------------------------------------------*/ public ListNode getHead() { return this.head; } /*------------------------------------------------------- modifiers -------------------------------------------------------*/ public void setHead(ListNode newHead) { this.head = newHead; } /*------------------------------------------ overridden methods -------------------------------------------*/ public String toString() { return "LinkedList[size = " + this.count + ", elements are:\n" + toStringHelper(this.head) + "]"; } /*------------------------------------------------------- toStringHelper Purpose: auxiliary function for toString(), to help it print all of the elements in a LinkedList (strictly for local use...!) ---------------------------------------------------------*/ private String toStringHelper(ListNode currNode) { // BASE CASE: if node is null, nothing to print --- // time to stop, return an empty String. if (currNode == null) { return ""; } // else, RECURSIVE CASE: create string for current // node, concatenating the strings for the rest of the // nodes... else { return "" + currNode.getData() + "\n" + toStringHelper(currNode.getNext()); } } /*--------------------------------------- other methods ----------------------------------------*/ /*-------------------------------------------------------- size Purpose: return current size of the list (why not make this an accessor? Well, I'd like to hide the implementation detail count --- but, whether I store it or not, finding the size of a list is a useful piece of info for many lists.) ----------------------------------------------------------*/ public int size() { return this.count; } /*------------------------------------------------------- addFirst Purpose: add given data newObj to beginning of list. ---------------------------------------------------------*/ public void addFirst(Comparable newObj) { // here's a list node to hold the new data ListNode newNode = new ListNode(newObj); // new list node should be set to point to // current first element in list... // (what if list is currently empty? that's OK, // head is null, and new node's next field is // simply reset to null, which is just what we // want for a 1-element list... newNode.setNext(this.head); // now, newNode can be NEW first element in list! this.head = newNode; // and don't forget to increment the count! this.count = this.count + 1; } /*------------------------------------------------------- addLast() Purpose: add given data newObj to end of list. ---------------------------------------------------------*/ public void addLast(Comparable newObj) { // here's a list node to hold the new data ListNode newNode = new ListNode(newObj); // what if list is empty? then first is last, too! if (this.head == null) { this.head = newNode; } // but, more likely, you'll need to "walk" to the end... else { ListNode currNode = this.head; // keep "walking" down the list until you reach the last // node; how do you know? When the node you are looking // at has NO "next" node! while (currNode.getNext() != null) { // make the NEXT node the next node to consider... currNode = currNode.getNext(); } // you KNOW currNode is the last node, now! // ... and so it is the one leading to our new node-to-add currNode.setNext(newNode); } // in either case, don't forget to increase list size! this.count = this.count + 1; } }