CircularQuote.java


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


 /*************************************************************
  *  Compilation:  javac CircularQuote.java
  *  Execution:    java CircularQuote
  *  Dependencies: none
  *
  *  Code for a circular linked list. Each node of the linked 
  *  list is a Card class defined within this class.
  *
  *************************************************************/
 
public class CircularQuote {

    // the first card in the circular linked list
    private Card start;

    // helper linked-list data type
    private class Card {
        public String word;
        public Card next;

        public Card(String word) {
            this.word = word;
            this.next = null;
        }
    }

    // create an empty quote
    public CircularQuote() {
        start = null;
    }
   
    // add the word w to the end of the quote
    public void addWord(String w) {
        Card newWord = new Card(w);

        // degenerate case for empty Quote
        if (start == null) {
          start = newWord;
          start.next = start;
        }

        // otherwise
        else {

            // find the current last word
            Card card = start;
            do {
               card = card.next;
            } while (card.next != start);

            // insert new word
            newWord.next = start;
            card.next = newWord;
        }
    }
   
    // number of words in the quote
    public int count() {

        // empty quote
        if (start == null) return 0;

        Card card = start;
        int total = 0;
        do {
            total++;
            card = card.next;
        } while (card != start);
        return total;
    }
   
    // the ith word in the quote (where i = 1 is the first word)
    public String getWord(int i) {
        if (i > count() || i <= 0) throw new RuntimeException("index out of bounds");

        Card card = start;
        for (int j = 1; j < i; j++) {
            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 (i > count() || i <= 0) throw new RuntimeException("index out of bounds");

        Card newWord = new Card(w);

        // find the ith card
        Card card = start;
        for (int j = 1; j < i; j++) {
            card = card.next;
        }

        // insert between card and card.next
        newWord.next = card.next;
        card.next = newWord;
    }
   
    // string representation of the entire quote   
    public String toString() {
        String s = "";
        if (start == null) return s;

        Card card = start;
        do {
            s = s + card.word + " ";
            card = card.next;
        } while (card != start);
        return s;
    }
           
    // test client   
    public static void main(String[] args) { 
        CircularQuote q = new CircularQuote();
        q.addWord("A");
        q.addWord("rose");
        q.addWord("is");
        q.addWord("a");
        q.addWord("rose.");
        System.out.println(q);
        System.out.println(q.count());
        System.out.println(q.getWord(2));
        q.insertWord(3, "just");
        System.out.println(q);
        System.out.println(q.count());
    }
}


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