Question: Using circular linked list of which each node contains one word, design a method that takes the head of the linked lists and return true

Using circular linked list of which each node contains one word, design a method that takes the head of the linked lists and return true if there is a duplicate word.in java Signature: Boolean checkDuplicate (Node yourVariableName)

The methods should be implemented in CircularLinkedListClass

this is methods implemented in CircularLinkedListClass:

class CircularlyLinkedList { //---------------- nested Node class ---------------- private static class Node { private String word; private E element; // an element stored at this node private Node next; // a reference to the subsequent node in the list public Node(E e, Node n) { element = e; next = n; }

// Accessor methods public E getElement() { return element; } public Node getNext() { return next; }

// Modifier methods public void setNext(Node n) { next = n; } } //----------- end of nested Node class -----------

// instance variables of the CircularlyLinkedList private Node tail = null; // we store tail (but not head) private int size = 0; // number of nodes in the list

/** Constructs an initially empty list. */ public CircularlyLinkedList() { } // constructs an initially empty list

// access methods public int size() { return size; } public boolean isEmpty() { return size == 0; } public E first() { // returns (but does not remove) the first element if (isEmpty()) return null; return tail.getNext().getElement(); // the head is *after* the tail } public E last() { // returns (but does not remove) the last element if (isEmpty()) return null; return tail.getElement(); }

// update methods /** * Rotate the first element to the back of the list. */ public void rotate() { // rotate the first element to the back of the list // the old head becomes the new tail }

public void addFirst(E e) { // adds element e to the front of the list if (size == 0) { tail = new Node<>(e, null); tail.setNext(tail); // link to itself circularly } else { Node newest = new Node<>(e, tail.getNext()); tail.setNext(newest); } size++; } public void addLast(E e) { // adds element e to the end of the list addFirst(e); // insert new element at front of list tail = tail.getNext(); // now new element becomes the tail } public Node getTail() { return tail; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!