/*************************************************************
* 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.
* Web Exercise 4.3.2
*************************************************************/
public class CircularQuote {
// the first card in the circular linked list
private Card start;
// helper linked-list data type
private class Card {
private String word;
private Card next;
private Card(String word) {
this.word = word;
this.next = null;
}
}
// constructor - create an empty quote
public CircularQuote() {
}
// add the word w to the end of the quote
public void addWord(String w) {
// degenerate case for empty quote, w is the first word
if (____________________) {
}
// otherwise, traverse list until card points to last word
else {
}
// insert new word
}
}
// number of words in the quote
public int count() {
}
// return the ith word where i = 1 is first word in quote
public String getWord(int i) {
// check for less than i words in quote or invalid index
if (count() < i || i <= 0) {
throw new RuntimeException("index out of bounds");
}
}
// insert w after the ith word, where i = 1 is the first word
public void insertWord(int i, String w) {
// check for less than i words in quote or invalid index
if (count() < i || i <= 0)
throw new RuntimeException("index out of bounds");
// make Card for the new word
// find the ith card
// insert between ith card its current next
}
// string representation of the entire quote
public String toString(){
}
// 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.");
StdOut.println(q);
StdOut.println(q.count());
StdOut.println(q.getWord(2));
q.insertWord(3, "just");
StdOut.println(q);
StdOut.println(q.count());
}
}