Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package linkedStructure; /** * WordList is a singly linked list of Strings. * It is designed to demonstrate how linked structures work. * * @author

package linkedStructure;

/** * WordList is a singly linked list of Strings. * It is designed to demonstrate how linked structures work. * * @author .......... */ public class WordList { private Node head; private Node tail; private int n; // number of words in the list

/** * Node of LinkedList that stores the item and a * single reference to the next node. */ private class Node { private String item; private Node next; } /** * Adds a node containing the new item at the * end of the list. * * @param newItem */ public void append(String newItem) { // create a new node based on the word provided by the user Node newNode = new Node(); newNode.item = newItem; if (isEmpty()) { head = newNode; tail = newNode; } else { tail.next = newNode; tail = newNode; } n++; } /** * Adds a node containing the new item at the * front of the list. * * @param newItem */ public void prepend(String newItem) { // TODO 2 } /** * Returns the index of the first occurrence of the specified item. * If the specified item in not part of the list * the method indexOf returns -1 * * @param item * @return index of the first occurrence of the item; -1 if the word was not found. */ public int indexOf(String item) { return 0; // TODO 3 } /** * Checks whether the list contains the given item. * * @param item * @return true if the item is contained in the list; false otherwise. */ public boolean contains(String item) { return false; // TODO } /** * Returns the number of elements in the list * @return the number of elements */ public int size() { return n; } /** * Determines whether the list is empty or not. * @return true if there are no elements in the list. */ public boolean isEmpty() { return n == 0; } @Override public String toString() { StringBuilder sb = new StringBuilder(); Node current = head; while(current != null) { sb.append(current.item).append(" "); current = current.next; } return sb.toString(); } /* * * * * * * * Test Client * * * * * * */ public static void main(String[] args) { WordList list = new WordList(); System.out.println("size: " + list.size()); // TODO 1 print The list is empty. or The list is not empty. // use a ternary operator to check whether the list is empty list.append("ant"); list.append("bat"); list.append("cow"); list.append("dog"); System.out.println("list: " + list);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions