Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions