Quote.java


Below is the syntax highlighted version of Quote.java from §4.3 Stacks and Queues.


 /*************************************************************
  *  Compilation:  javac Quote.java
  *  Execution:    java Quote
  *  Dependencies: none
  *
  *  Code for a null terminated linked list. Each node of the linked 
  *  list is a Card class defined within Quote.
  *
  *  % A rose is a rose. 
  *  5
  *  rose
  *  A rose is just a rose. 
  *  6
  *
  *************************************************************/
 
public class Quote {

    // the first card in null-terminated linked list
    private Card start;

    // helper linked-list data type  
    private class Card {
        private String word;
        private Card next;
    
        public Card(String word) {
           this.word = word;
           this.next = null;
        }
    }
  
    // create an empty quote
    public Quote() {
        start = null;
    }
  
    // add the word w to the end of the quote
    public void addWord(String w) {
        Card newWord = new Card(w);

        // degenerate case when w is the first word
        if (start == null) start = newWord;

        // otherwise,
        else {

            // traverse list until card points to last word
            Card card = start;                 
            while (card.next != null) {
                card = card.next;
            }

            // add card for the new word to end of list
            card.next = newWord; 
        }
    }
  
    // number of words in the quote
    public int count() {
        int total = 0;
        for (Card card = start; card != null; card = card.next) {
            total++;
        }
        return total;
    }
  
    // return the ith word in the quote (where i = 1 is the first word)
    public String getWord(int i) {
        if (count() < i || i <= 0) throw new RuntimeException("index out of bounds");

        Card card = start;
        for (int count = 1; count < i; count++) {
            card = card.next;
        }
        return card.word;
    }
 
    // insert the word w after the ith word (where i = 1 is the first word)
    public void insertWord(int i, String w) {
        if (count() < i || i <= 0) throw new RuntimeException("index out of bounds");

        Card newWord = new Card(w);
        Card card = start;
        for (int j = 1; j < i; j++) {
            card = card.next;
        }
        newWord.next = card.next;
        card.next = newWord;
    }

    // string representation of the quote  
    public String toString() {
        String s = "";
        for (Card card = start; card != null; card = card.next) {
            s = s + card.word + " ";
        }
        return s;
    }
  
 
  
    public static void main(String[] args) { 
        Quote q = new Quote();
        q.addWord("A");
        q.addWord("rose");
        q.addWord("is");
        q.addWord("a");
        q.addWord("rose.");
        StdOut.println(q);
        StdOut.println(q.count());
        StdOut.println(q.getWord(2));
        q.insertWord(3, "just");
        StdOut.println(q);
        StdOut.println(q.count());
    }
}


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Mon Apr 4 12:02:50 EDT 2011.