Need this in Java with Eclipse also chegg stop closing my question. Using the Si
ID: 651671 • Letter: N
Question
Need this in Java with Eclipse also chegg stop closing my question.
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
insertAfter
insertBefore
deleteByPosition
printNode
printAllNodes
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
Explanation / Answer
C++
void DoublyLinkedList<T>::deleteNode(T data)
{
Node<T> * tmp = head;
while (tmp && tmp->data != data)
{
tmp = tmp->next;
}
CODE IN JAVA
public E remove(int index)
{
assert(index >= 0 && index < size()); //force valid index
temp = head; //start at the beginning of the list
if(index == 0)
{
E elem = head.elem;
head = head.next;
counter--;
return elem;
}
else if(index == size())
{
E elem = tail.elem;
tail = tail.previous;
counter--;
return elem;
}
//iterate to the position before the index
for(int i = 0; i < index-1; i++)
temp = temp.next;
Node<E> two = temp.next;
//set temp.next to point to the Node next to the Node to be removed
temp.next = two.next;
E elem = two.elem; //store the element to return
two = null; //remove the node
counter--; //decrement size
return elem; //return the element at that position
}
public E remove(E elem)
{
temp = head; //start at the beginning of the list
Node<E> two = null;
if(head.elem.equals(elem))
{
head = head.next;
head.previous = null;
counter--;
return elem;
}
else if(tail.elem.equals(elem))
{
tail = tail.previous;
tail.next = null;
counter--;
return elem;
}
//while the elem hasn't been found but there is another node
while(temp != null && !temp.elem.equals(elem))
{
two = temp; //have a reference to the element before the one to remove
temp = temp.next; //in this method, temp will be the elem to remove
}
if(temp == null) return null;
two.next = temp.next;
E spare = temp.elem; //return element
temp = null;
counter--; //decrement size
return spare;
}
if (tmp)
{
if (tmp->prev && tmp->next) // no change to head or tail
{
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
}
else if (tmp->prev) // change to tail
{
tmp->prev->next = tmp->next;
tail = tmp->prev;
}
else if (tmp->next) // change to head
{
tmp->next->prev = tmp->prev;
head = tmp->next;
}
delete tmp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.