/*************************************************************************
* Name:
* NetID:
* Precept:
*
* Description: A circularly linked list of words that supports insertion
* without copying any elements.
*
* Remarks:
* - This code is based on Exercise 4.3.51.
*
*************************************************************************/
public class CircularQuote {
// the first card in the circular linked list
private Node start;
// node stores a word and a reference to the next node
private class Node {
private String word; // word in a sentence
private Node next; // next word
private Node(String word) {
this.word = word;
this.next = null;
}
}
// creates an empty quote
public CircularQuote() {
}
// adds a word to the end of the quote
public void addWord(String word) {
// degenerate case for empty quote
if (____________________) {
}
else {
// traverse linked list until you find the last node
// 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 fewer than i words in quote or invalid index
if (count() < i || i <= 0) {
throw new RuntimeException("index out of bounds");
}
}
// insert specified word after the ith word (i = 1 is the first word)
public void insertWord(int i, String word) {
// check for fewer than i words in quote or invalid index
if (count() < i || i <= 0)
throw new RuntimeException("index out of bounds");
// make Node 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 quote = new CircularQuote();
quote.addWord("A");
quote.addWord("rose");
quote.addWord("is");
quote.addWord("a");
quote.addWord("rose.");
StdOut.println(quote);
StdOut.println(quote.count());
StdOut.println(quote.getWord(2));
quote.insertWord(3, "just");
StdOut.println(quote);
StdOut.println(quote.count());
}
}