Need this in Java with Eclipse also chegg stop closing my question this entire t
ID: 652317 • Letter: N
Question
Need this in Java with Eclipse also chegg stop closing my question this entire thing is one question. Answer it this time. I really despise this new system of asking questions. The code must work with my code.
Using the SinglyLinkedList classes built in class, make the proper modifications to make it a DoublyLinkedList. Then, write a simple text editor. Keep the entire text in a doubly linked list, storing each line in a separate node. The program begins with a line number as a prompt to enter text. From there, you have the following choices:
enter the letter I and a string S: this will insert the string S at the end
enter the letters IA, a line number N, and string S1: inserts S1 after line N
enter the letters IB, a line number N, and string S1: inserts S1 before line N
enter the letter D with a number N: deletes the Nth line of text
enter the letter L with a number N: lists the Nth line of text
enter the letters LA: lists all lines of text
enter the letter Q: quits the program
To support each of these operations, you will have the following methods in your DoublyLinkedList class:
insert: this method will take the line of text as a string and add it to the end of the list.
insertAfter: this method takes an integer parameter (used to indicate a line number) and a string parameter, and inserts the text after the appropriate node.
insertBefore: this method takes an integer parameter (used to indicate a line number) and a string parameter, and inserts the text before the appropriate node
deleteByPosition: this method will take an integer parameter indicating which element should be deleted. For example, if 3 is passed in, the 3rd element should be deleted.
printNode: this method takes an integer parameter of the node to print. For example, if 3 is passed in, it will print the 3rd node.
printAllNodes: this method takes no parameters and prints all nodes in the list.
Included are these
Assign04Driver.java: This should not be modified.
Node.java:This will need to be modified to support a doubly linked list.
DoublyLinkedList.java: A starter class for your doubly linked list. It includes method stubs which you must implement
TextEditor.java: You must not change this interface, nor can you remove the implementation of this interface from DoublyLinkedList.java.
Here is the code I have already for you.
https://www.dropbox.com/s/0710nej3882r86v/DoublyLinkedList.java?dl=0
https://www.dropbox.com/s/pmtqbf4pvryzamz/Assign04Driver.java?dl=0
https://www.dropbox.com/s/0m971tvrkz39pf5/Node.java?dl=0
https://www.dropbox.com/s/c59rr9j4qdhir39/TextEditor.java?dl=0
In the interface and DoublyLinkedList class, there are deleteNodes methods. Do nothing with those
Please read the whole thing and make sure you do the question correctly please. My last two answers from the same question were not great.
Explanation / Answer
Complete Program:
// File: Node.java
public class Node<E>
{
E data;
Node<E> next;
Node<E> prev;
public Node(E d)
{
data = d;
next = null;
prev = null;
}
}
----------------------------------------------------------
// File: DoublyLinkedList.java
public class DoublyLinkedList<E> implements TextEditor<E>
{
Node<E> head;
int size;
public DoublyLinkedList()
{
head = null;
size = 0;
}
public boolean isEmpty()
{
return (head == null);
}
public void insert(E line)
{
Node<E> newNode = new Node<E>(line);
if(isEmpty())
{
head = newNode;
}
else
{
Node<E> current = head;
while(current.next != null)
{
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
size++;
}
public void insertAfter(int lineNum, E line)
{
Node<E> newNode = new Node<E>(line);
Node<E> current = head;
int i = 1;
while(current != null && i < lineNum)
{
current = current.next;
i++;
}
if(current != null && i == lineNum)
{
newNode.next = current.next;
current.next = newNode;
newNode.prev = current;
if(newNode.next != null)
newNode.next.prev = newNode;
}
}
public void insertBefore(int lineNum, E line)
{
Node<E> newNode = new Node<E>(line);
Node<E> current = head;
int i = 1;
while(current != null && i < lineNum)
{
current = current.next;
i++;
}
if(current != null && i == lineNum)
{
newNode.prev = current.prev;
current.prev = newNode;
newNode.next = current;
if(newNode.prev != null)
newNode.prev.next = newNode;
if(lineNum == 1)
head = head.prev;
}
}
public void deleteByPosition(int position)
{
Node<E> current = head;
int i = 1;
while(current != null && i < position)
{
current = current.next;
i++;
}
if(current != null && i == position)
{
Node<E> temp1 = current.prev;
Node<E> temp2 = current.next;
if(temp1 == null)
{
head = head.next;
if(head != null)
head.prev = null;
}
else
{
temp1.next = temp2;
if(temp2 != null)
temp2.prev = temp1;
}
}
}
public void deleteNodes(E startPos, E endPos)
{
int start = 1;
int end = 1;
Node<E> current1 = head;
while(current1 != null && ((Comparable<E>)current1.data).compareTo(startPos) != 0)
{
current1 = current1.next;
start++;
}
Node<E> current2 = head;
while(current2 != null && ((Comparable<E>)current2.data).compareTo(endPos) != 0)
{
current2 = current2.next;
end++;
}
if(current1 != null && current2 != null)
{
while(start <= end)
{
deleteByPosition(start);
end--;
}
}
}
public void printNode(int position)
{
Node<E> current = head;
int pos = 1;
while(current != null && pos < position)
{
current = current.next;
pos++;
}
if(current != null && pos == position)
System.out.println(current.data);
}
public void printAllNodes()
{
Node<E> current = head;
while(current != null)
{
System.out.println(current.data);
current = current.next;
}
System.out.println();
}
public void printAllNodesReverse()
{
Node<E> current = head;
while(current != null && current.next != null)
current = current.next;
while(current != null)
{
System.out.println(current.data);
current = current.prev;
}
System.out.println();
}
}
-----------------------------------------------------------
// File: Assign04Driver.java
import java.util.Scanner;
public class Assign04Driver
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
DoublyLinkedList<String> editor = new DoublyLinkedList<String>();
Scanner scan = new Scanner(System.in);
String command = "";
do
{
System.out.print(": ");
command = scan.nextLine();
executeCommand(command.toLowerCase(), editor);
}
while (!command.equalsIgnoreCase("q"));
System.out.println("Editor ended.");
scan.close();
}
public static <E> void executeCommand(String command, DoublyLinkedList<E> dll)
{
try
{
if (command.startsWith("ia"))
{
String[] commandArray = command.split(" ", 3);
dll.insertAfter(Integer.parseInt(commandArray[1]), (E)commandArray[2]);
}
else if (command.startsWith("ib"))
{
String[] commandArray = command.split(" ", 3);
dll.insertBefore(Integer.parseInt(commandArray[1]), (E)commandArray[2]);
}
else if (command.startsWith("i"))
{
String[] commandArray = command.split(" ", 2);
dll.insert((E)commandArray[1]);
}
else if (command.startsWith("d"))
{
String[] commandArray = command.split(" ");
dll.deleteByPosition(Integer.parseInt(commandArray[1]));
}
else if (command.startsWith("la"))
{
dll.printAllNodes();
}
else if (command.startsWith("l"))
{
String[] commandArray = command.split(" ");
dll.printNode(Integer.parseInt(commandArray[1]));
}
else
{
if (!command.equalsIgnoreCase("q"))
{
System.out.println("Invalid command.");
}
}
}
catch (Exception e)
{
System.out.println("Invalid command.");
}
}
}
-----------------------------------------------------------
// File: TextEditor.java
public interface TextEditor<E>
{
public void insert(E line);
public void insertAfter(int position, E line);
public void insertBefore(int position, E line);
public void deleteByPosition(int position);
public void deleteNodes(E startPos, E endPos);
public void printNode(int position);
public void printAllNodes();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.