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

import java.util.Scanner; Create a circularly linked list using * doubly linked

ID: 3912903 • Letter: I

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 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<T> {

private DNode<T> 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<T> 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<String> 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<T> {

private T data;

private DNode<T> prev, next;

public DNode(T d, DNode<T> p, DNode<T> n) {

data = d;

next = n;

prev = p;

}

public T getData() {

return data;

}

public DNode<T> getNext() {

return next;

}

public DNode<T> getPrev() {

return prev;

}

public void setData(T d) {

data = d;

}

public void setNext(DNode<T> n) {

next = n;

}

public void setPrev(DNode<T> p) {

prev = p;

}

}

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


import java.util.Scanner;
// doubly linked list, uses a cursor and no sentinels.
public class A00000000<T> {
private DNode<T> cursor;
private int size;
public A00000000() {
size = 0;
cursor = null;
}
public int size() {  
return size;
}
public boolean isEmpty() {  
return size == 0;
}
public void advance() {
if(cursor != null)
cursor = cursor.getNext();
}
public void goBack() {
if(cursor != null)
cursor = cursor.getPrev();
}
public void addBefore(T d) {
DNode<T> n = new DNode(d, null, null);
if(cursor == null){
n.setNext(n);
n.setPrev(n);
}
else{
n.setPrev(cursor.getPrev());
n.setNext(cursor);
cursor.setPrev(n);
n.getPrev().setNext(n);
}
size++;
}
public void addAfter(T d) { // CHANGE CODE HERE
DNode<T> n = new DNode(d, null, null);
if(cursor == null){
n.setNext(n);
n.setPrev(n);
cursor = n;
}
else{
n.setNext(cursor.getNext());
n.setPrev(cursor);
cursor.setNext(n);
n.getNext().setPrev(n);
}
size++;
}
public T remove() { // CHANGE CODE HERE
if(isEmpty())
return null;
else {
T value = cursor.getData();
if(size == 1)
cursor = null;
else
{
DNode<T> prev = cursor.getPrev();
DNode<T> next = cursor.getNext();
prev.setNext(next);
next.setPrev(prev);
cursor = prev.getNext();
}
size--;
return value;
}
}
// LinkedList testing methods:
public String toString() {
String ans = "List status (cursor marked as ^^): ";
DNode<T> 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<String> 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<T> {
private T data;
private DNode<T> prev, next;
public DNode(T d, DNode<T> p, DNode<T> n) {
data = d;
next = n;
prev = p;
}
public T getData() {
return data;
}
public DNode<T> getNext() {
return next;
}
public DNode<T> getPrev() {
return prev;
}
public void setData(T d) {
data = d;
}
public void setNext(DNode<T> n) {
next = n;
}
public void setPrev(DNode<T> p) {
prev = p;
}
}

output

-----

List status (cursor marked as ^^): (^^)<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

A 2

List status (cursor marked as ^^): (^^)<-->2<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

A 4

List status (cursor marked as ^^): (^^)<-->2<-->4<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

A 6

List status (cursor marked as ^^): (^^)<-->2<-->6<-->4<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

+

List status (cursor marked as ^^): (^^)<-->6<-->4<-->2<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

+

List status (cursor marked as ^^): (^^)<-->4<-->2<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

-

List status (cursor marked as ^^): (^^)<-->6<-->4<-->2<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

-

List status (cursor marked as ^^): (^^)<-->2<-->6<-->4<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

A 5

List status (cursor marked as ^^): (^^)<-->2<-->5<-->6<-->4<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

-

List status (cursor marked as ^^): (^^)<-->4<-->2<-->5<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

R

List status (cursor marked as ^^): (^^)<-->2<-->5<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

R

List status (cursor marked as ^^): (^^)<-->5<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

A 1-

List status (cursor marked as ^^): (^^)<-->5<-->1-<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

A 2

List status (cursor marked as ^^): (^^)<-->5<-->2<-->1-<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>

R

List status (cursor marked as ^^): (^^)<-->2<-->1-<-->6<-->(^^)

cmds are H(elp) B(efore) A(fter) R(emove) + - Q(uit): >>