import java.util.Scanner; /******* * The goal of this assignment is to create a
ID: 3791976 • Letter: I
Question
import java.util.Scanner;
/*******
* The goal of this assignment is to create a circularly linked list using * doubly linked nodes. In a circularly linked list, there is no head node and * no tail node. The node that you might view as occupying the first place * points back to the node that you might think of being in the last place, and * the last node points forward to the first.
* Apart from renaming the file and class, the only other changes that you * should make to the code is to supply implementations for the following * instance methods: * * size() to return the size of the list * isEmpty() to answer whether the list is empty * advance() to move the cursor one node forward * goBack() to move the cursor one node backward * addBefore creates and inserts a new node before the cursor, the cursor is moved to the newly created node. * addAfter creates and inserts a new node after the cursor, the cursor is moved to the newly created node. * remove removes the Node at the cursor, the cursor is moved to the next node. * * In case the list is empty and the cursor is null, then both methods addBefore * and addAfter should supply a first node for the list. In case the list * becomes empty by removing a last node, the cursor should become null again. * * I will test your homework by compiling and running the main method that has * been included in the class. The method toString() has also been included for * testing purposes. * * The files LinkedList.java and DList.java from the course provide similar * implementations --- you should review them before doing this assignment. ************/ // doubly linked list, uses a cursor and no sentinels. public class A00000000 { private DNode cursor; private int size; public A00000000() { size = 0; cursor = null; } public int size() { // CHANGE CODE HERE return 0; } public boolean isEmpty() { // CHANGE CODE HERE return true; } public void advance() { // CHANGE CODE HERE } public void goBack() { // CHANGE CODE HERE } public void addBefore(T d) { // CHANGE CODE HERE } public void addAfter(T d) { // CHANGE CODE HERE } public T remove() { // CHANGE CODE HERE return null; } // LinkedList testing methods: public String toString() { String ans = "List status (cursor marked as ^^): "; DNode n = cursor; ans += "(^^)<-->"; for (int i = 0; i < size; i++, n = n.getNext()) ans += (n.getData() + "<-->"); ans += "(^^)"; return ans; } public static void main(String args[]) { A00000000 l = new A00000000<>(); boolean done = false; Scanner s = new Scanner(System.in); while (!done) { try { System.out.println(l); System.out .println(" cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>"); String cmd = s.next(); String entry = null; char command = cmd.trim().toUpperCase().charAt(0); if (command == 'B' || command == 'A') entry = s.next(); switch (command) { case 'H': giveHelp(); break; case 'Q': done = true; break; case 'R': l.remove(); break; case '+': l.advance(); break; case '-': l.goBack(); break; case 'B': l.addBefore(entry); break; case 'A': l.addAfter(entry); break; } } catch (Exception e) { System.out.println("Error " + e.toString()); } } s.close(); } private static void giveHelp() { String help = "Commands are: H for help, Q to quit " + " + or - to move the cursor forwards of backwards in the list " + " A data, B data to insert data After or Before the cursor " + " R to remove the cursor node from the list."; System.out.println(help); } } class DNode { private T data; private DNode prev, next; public DNode(T d, DNode p, DNode n) { data = d; next = n; prev = p; } public T getData() { return data; } public DNode getNext() { return next; } public DNode getPrev() { return prev; } public void setData(T d) { data = d; } public void setNext(DNode n) { next = n; } public void setPrev(DNode p) { prev = p; } }
Explanation / Answer
Here is the code for the class:
import java.util.Scanner;
/*******
* The goal of this assignment is to create a circularly linked list using
* doubly linked nodes. In a circularly linked list, there is no head node and
* no tail node. The node that you might view as occupying the first place
* points back to the node that you might think of being in the last place, and
* the last node points forward to the first.
* Apart from renaming the file and class, the only other changes that you
* should make to the code is to supply implementations for the following
* instance methods:
*
* size() to return the size of the list
* isEmpty() to answer whether the list is empty
* advance() to move the cursor one node forward
* goBack() to move the cursor one node backward
* addBefore creates and inserts a new node before the cursor, the cursor is moved to the newly created node.
* addAfter creates and inserts a new node after the cursor, the cursor is moved to the newly created node.
* remove removes the Node at the cursor, the cursor is moved to the next node.
*
* In case the list is empty and the cursor is null, then both methods addBefore
* and addAfter should supply a first node for the list. In case the list
* becomes empty by removing a last node, the cursor should become null again.
*
* I will test your homework by compiling and running the main method that has
* been included in the class. The method toString() has also been included for
* testing purposes.
*
* The files LinkedList.java and DList.java from the course provide similar
* implementations --- you should review them before doing this assignment.
************/
// doubly linked list, uses a cursor and no sentinels.
public class A00000000<T> {
private DNode cursor;
private int size;
public A00000000() {
size = 0;
cursor = null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void advance() {
cursor = cursor.getNext();
}
public void goBack() {
cursor = cursor.getPrev();
}
public void addBefore(T d) {
DNode temp = new DNode(d, null, null);
if(cursor == null)
{
temp.setPrev(null);
temp.setNext(null);
cursor = temp;
}
else
{
temp.setPrev(cursor.getPrev());
temp.setNext(cursor);
if(cursor.getPrev() != null)
cursor.getPrev().setNext(temp);
cursor.setPrev(temp);
}
cursor = temp;
}
public void addAfter(T d) { // CHANGE CODE HERE
DNode temp = new DNode(d, null, null);
if(cursor == null)
{
temp.setPrev(null);
temp.setNext(null);
cursor = temp;
}
else
{
temp.setPrev(cursor);
temp.setNext(cursor.getNext());
if(cursor.getNext() != null)
cursor.getNext().setPrev(temp);
cursor.setNext(temp);
}
cursor = temp;
}
public T remove() { // CHANGE CODE HERE
DNode temp = cursor;
if(cursor.getPrev() != null)
cursor.getPrev().setNext(cursor.getNext());
if(cursor.getNext() != null)
cursor.getNext().setPrev(cursor.getPrev());
temp = cursor;
cursor = cursor.getNext();
return (T)temp.getData();
}
// LinkedList testing methods:
public String toString() {
String ans = "List status (cursor marked as ^^): ";
DNode n = cursor;
ans += "(^^)<-->";
for (int i = 0; i < size; i++, n = n.getNext())
ans += (n.getData() + "<-->");
ans += "(^^)";
return ans;
}
public static void main(String args[]) {
A00000000 l = new A00000000<>();
boolean done = false;
Scanner s = new Scanner(System.in);
while (!done) {
try {
System.out.println(l);
System.out
.println(" cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>");
String cmd = s.next();
String entry = null;
char command = cmd.trim().toUpperCase().charAt(0);
if (command == 'B' || command == 'A')
entry = s.next();
switch (command) {
case 'H':
giveHelp();
break;
case 'Q':
done = true;
break;
case 'R':
l.remove();
break;
case '+':
l.advance();
break;
case '-':
l.goBack();
break;
case 'B':
l.addBefore(entry);
break;
case 'A':
l.addAfter(entry);
break;
}
} catch (Exception e) {
System.out.println("Error " + e.toString());
}
}
s.close();
}
private static void giveHelp() {
String help = "Commands are: H for help, Q to quit "
+ " + or - to move the cursor forwards of backwards in the list "
+ " A data, B data to insert data After or Before the cursor "
+ " R to remove the cursor node from the list.";
System.out.println(help);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.