Question: import java.util.Scanner; Create a circularly linked list using * doubly linked nodes. In a circularly linked list, there is no head node and * no

import java.util.Scanner;

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.

* If an observer looks at the state of the list without knowing how the nodes

* have been inserted, the observer will not be able to say which node is the

* head and which is the tail. There will be no special nodes of the list that

* point to null nodes.

*

* the list is to be accessed by using a movable

* cursor that indictaes a currently active node. Modifications to the list take

* place at that node.

*

* 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;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!