import java.util.Scanner; /******* * Homework A: * * Edit the following file and
ID: 3856587 • Letter: I
Question
import java.util.Scanner;/******* * Homework A: * * Edit the following file and save it as Axxxxxxxx.java where xxxxxxxx is * replaced by your 8 digit ID number. Change the class name to reflect this * file name. Include a comment with your name at the top of the program in case * there is a problem with automatic recognition of your ID number. Submit this * one file as an email attachment to kmahavadi@qc.cuny.edu * * Remove any initial package declaration that might be added to your * file in case you edit it in eclipse. * * * 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. There is a description of * circularly linked lists in Section 3.3 of the text book. * * 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. * * In the homework implementation: 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; } } import java.util.Scanner;
/******* * Homework A: * * Edit the following file and save it as Axxxxxxxx.java where xxxxxxxx is * replaced by your 8 digit ID number. Change the class name to reflect this * file name. Include a comment with your name at the top of the program in case * there is a problem with automatic recognition of your ID number. Submit this * one file as an email attachment to kmahavadi@qc.cuny.edu * * Remove any initial package declaration that might be added to your * file in case you edit it in eclipse. * * * 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. There is a description of * circularly linked lists in Section 3.3 of the text book. * * 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. * * In the homework implementation: 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; } } import java.util.Scanner;
/******* * Homework A: * * Edit the following file and save it as Axxxxxxxx.java where xxxxxxxx is * replaced by your 8 digit ID number. Change the class name to reflect this * file name. Include a comment with your name at the top of the program in case * there is a problem with automatic recognition of your ID number. Submit this * one file as an email attachment to kmahavadi@qc.cuny.edu * * Remove any initial package declaration that might be added to your * file in case you edit it in eclipse. * * * 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. There is a description of * circularly linked lists in Section 3.3 of the text book. * * 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. * * In the homework implementation: 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
import java.util.Scanner;
public class CircularDoublyLinkedList<T> {
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;
}
}
private DNode<T> cursor;
private int size;
public int getSize()
{
/*int count = 0;
if(cursor==null)
return count;
else
{
Node<T> temp=head;
do{
temp=temp.getNextNode();
count++;
}while(temp!=tail);
}
return count;*/
return size;
}
//Traverse from head
public void advance(){
DNode<T> temp=cursor;
for(int i=0; i<size;i++){
System.out.print(temp.getData()+" ");
temp=temp.getNext();
}
}
//Traverse from tail
public void goBack(){
DNode<T> temp=cursor;
for(int i=0; i<size;i++){
System.out.print(temp.getData()+" ");
temp=temp.getPrev();
}
}
/* methods related to insertion in doubly linked list starts.*/
public void addAfter(T data){
DNode<T> newnode=new DNode<T>(data,null,null);
if(cursor==null){
cursor=newnode;
newnode.setNext(null);
newnode.setPrev(null);
}else{
DNode<T> t;
t=cursor.getPrev();
if(t!=null)
{
t.setNext(newnode);
newnode.setPrev(t);
}
newnode.setNext(cursor);
cursor.setPrev(newnode);
cursor=newnode;
}
size++;
}
public void addBefore(T data){
DNode<T> newnode=new DNode<T>(data, null, null);
if(cursor==null){
cursor=newnode;
newnode.setNext(null);
newnode.setPrev(null);
}else{
DNode<T> t;
t=cursor.getPrev();
if(t!=null)
{
t.setNext(newnode);
newnode.setPrev(t);
}
newnode.setNext(cursor);
cursor.setPrev(newnode);
}
size++;
}
private void remove(){
DNode<T> t=cursor.getNext();
if(t!=null)
cursor.getNext().setPrev(cursor.getPrev());
t=cursor.getPrev();
if(t!=null)
cursor.getPrev().setNext(cursor.getNext());
cursor=cursor.getNext();
size--;
}
// 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())
{
if(n!=null) ans += (n.getData() + "<-->");
}
ans += "(^^)";
return ans;
}
public static void main(String args[]) {
CircularDoublyLinkedList<String> l = new CircularDoublyLinkedList<>();
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);
}
}
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.