Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q2. [20 ptsl Write the following classes that implement a double Linked List of

ID: 3861491 • Letter: Q

Question

Q2. [20 ptsl Write the following classes that implement a double Linked List of Strings: 1) Class Word: It includes three instance variables: String word, word next and word prev It contains the following methods: o A constructor that initializes the instance variable word. o Set and get method for each instance variable. 2) Class DoubleLinkedList: It includes two instance variables: Word head and Word tail. It contains the following methods: o public void insert (String n) that creates and adds a Word at the end of the linked List. o public void printForwardo that prints the content of the linked ist forward o public void printBackward0 that prints the content of the linked list backward. o public void remove (String n that removes a word if it exists in the list. 3) class TestDoubleLinkedList in the main method, do the following: Create an object of class DoubleLinkedList. sentence). Read a sentence from the user and store it in a variable (for example, String split the sentence into words and store the words in an array. (Hint use method split Insert each word of the array in the linked list method insert. Call method printForward 0 method printBackward 0 printForward Read a string from the user, call method remove and then call me

Explanation / Answer


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Sam
*/
public class TestDoubleLinkedList {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DoubleLinkedList dll = new DoubleLinkedList();
        System.out.println("Enter a String");
        for(String s:br.readLine().split(" "))
            dll.insert(s);
        System.out.println("Printing linked list forward:");
        dll.printForward();
        System.out.println("Printing linked list backward:");
        dll.printBackward();
        System.out.println("Enter a word to delete:");
        dll.remove(br.readLine());
        System.out.println("Printing linked list forward:");
        dll.printForward();
    }
}
class DoubleLinkedList{
    private Word head;
    private Word tail;

    public DoubleLinkedList() {
        head = tail = null;
    }
  
    public void insert(String n){ //O(1)
        Word newWord = new Word(n);
        if (tail == null)
            head = tail = newWord;
        else {
            tail.setNext(newWord);
            newWord.setPrev(tail);
            tail = newWord;
        }
    }
  
    public void remove(String n){ //O(length of list)
        Word temp = head;
        while (temp!=null){
            if (temp.getWord().equals(n))
                break;
            temp = temp.getNext();
        }
      
        if (temp == null)
            return;
        if (temp.getNext()!=null)
            temp.getNext().setPrev(temp.getPrev());
        else
            tail = tail.getPrev();
      
        if (temp.getPrev()!=null)
            temp.getPrev().setNext(temp.getNext());
        else
            head = head.getNext();
      
    }
  
    public void printForward(){ //O(length of list)
        Word temp = head;
        while (temp!=null){
            System.out.print(temp.getWord()+" ");
            temp = temp.getNext();
        }
        System.out.println();
    }
  
    public void printBackward(){ //O(length of list)
        Word temp = tail;
        while (temp!=null){
            System.out.print(temp.getWord()+" ");
            temp = temp.getPrev();
        }
        System.out.println();
    }
  
}

class Word {
    private String word;
    private Word next;
    private Word prev;

    public Word(String word) {
        this.word = word;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public Word getNext() {
        return next;
    }

    public void setNext(Word next) {
        this.next = next;
    }

    public Word getPrev() {
        return prev;
    }

    public void setPrev(Word prev) {
        this.prev = prev;
    }
  
}