Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; /******* * Homework A: * * Edit the following file and save it as Axxxxxxxx.java where xxxxxxxx is * replaced by your 8

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 {

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

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Explain the meaning of e-business.

Answered: 1 week ago

Question

What is a management information system?

Answered: 1 week ago