HW5 Code
// TODO: YOUR FILE HEADER GOES HERE
import java.io.*;
public class List extends Base {
static boolean debug = false; // debug status
static int listCounter = 0; // used to number each List
public static final int // List controls
END = 0,
FRONT = 1;
// TODO: YOUR LISTENGINE HEADER GOES HERE
private class ListEngine {
// catastrophic error messages
static final String
ADNEXT_EMPTY = "Advance next from empty list!!!\n",
ADPRE_EMPTY = "Advance pre from empty list!!!\n",
REMOVE_EMPTY = "Remove from empty list!!!\n",
VIEW_EMPTY = "Viewing an empty list!!!\n",
WRITE_NONEXISTFILE
= "Writing to a non-existent file!!!\n";
// debug messages
static final String
ADNEXT = "[List %d - Advancing next]\n",
ADPRE = "[List %d - Advancing pre]\n",
INSERT = "[List %d - Inserting node]\n",
REMOVE = "[List %d - Removing node]\n",
VIEW = "[List %d - Viewing node]\n",
LIST_ALLOCATE
= "[List %d has been allocated]\n",
LIST_JETTISON
= "[List %d has been jettisoned]\n";
int count; // which list is it
Node end; // end of the List
long occupancy; // how many items stored
Base sample; // sample object of what is stored
Tracker tracker; // to track memory
// TODO: ListEngine CTOR METHOD HEADER
ListEngine (Base sample, String caller) {
tracker = new Tracker ("ListEngine",
Size.of (count)
+ Size.of (end)
+ Size.of (occupancy)
+ Size.of (sample)
+ Size.of (tracker),
caller + " calling ListEngine Ctor");
// ---- DO NOT CHANGE TRACKER ---- //
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine JETTISION METHOD HEADER
void jettisonList () {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine ADVANCENEXT METHOD HEADER
void advanceNext () {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine ADVANCEPRE METHOD HEADER
void advancePre () {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine CHECKTOGOFORWARD METHOD HEADER
boolean checkToGoForward (long where) {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine ISEMPTY METHOD HEADER
boolean isEmpty () {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine INSERT METHOD HEADER
boolean insert (Base element, long where) {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine LOCATE METHOD HEADER
boolean locate (long where) {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine REMOVE METHOD HEADER
Base remove (long where) {
// TODO: YOUR CODE GOES HERE
}
// TODO: ListEngine VIEW METHOD HEADER
Base view (long where) {
// TODO: YOUR CODE GOES HERE
}
// ListEngine WRITELIST
void writeList (PrintStream stream) {
if (stream == null) {
System.err.print (WRITE_NONEXISTFILE);
return;
}
// extra output if we are debugging
if (stream == System.err) {
stream.print ("List "
+ count + " has "
+ occupancy + " items in it:\n");
}
// display each Node in the List
Node oldEnd = end; // to save prior end
if (occupancy > 0) {
advanceNext ();
}
for (long idx = 1; idx <= occupancy; idx++) {
stream.print (" element " + idx + ": ");
end.writeNode (stream);
advanceNext ();
}
// memory tracking output if we are debugging
if (debug) {
System.err.print (tracker);
}
// restore end to prior value
end = oldEnd;
}
// TODO: ListEngine WRITEREVERSELIST METHOD HEADER
void writeReverseList (PrintStream stream) {
// TODO: YOUR CODE GOES HERE
}
private class Node {
// TODO: YOUR NODEENGINE HEADER GOES HERE
private class NodeEngine {
static final String WRITE_NONEXISTFILE
= "Writing to a "
+ "non-existent file!!!\n";
Base data; // the item stored
Node next; // to get to following item
Node pre; // to get to previous item
Tracker tracker; // to track memory
// TODO: NodeEngine CTOR METHOD HEADER HERE.
NodeEngine (Node newNode,
Base element, String caller) {
tracker = new Tracker ("NodeEngine",
Size.of (data)
+ Size.of (next)
+ Size.of (pre)
+ Size.of (tracker),
caller
+= " calling NodeEngine Ctor");
// ---- DO NOT CHANGE TRACKER ---- //
// TODO: YOUR CODE GOES HERE
}
// TODO: NodeEngine JETTISON METHOD HEADER HERE.
void jettisonNodeAndData () {
// TODO: YOUR CODE GOES HERE
}
// TODO: NodeEngine JETTISON METHOD HEADER HERE.
void jettisonNodeOnly () {
// TODO: YOUR CODE GOES HERE
}
// TODO: NodeEngine INSERT METHOD HEADER HERE.
Node insert (Node thisNode,
Base element) {
// TODO: YOUR CODE GOES HERE
}
// TODO: NodeEngine REMOVE METHOD HEADER HERE.
Base remove () {
// TODO: YOUR CODE GOES HERE
}
// TODO: NodeEngine VIEW METHOD HEADER HERE.
Base view () {
// TODO: YOUR CODE GOES HERE
}
// NodeEngine WRITENODE METHOD
void writeNode (PrintStream stream) {
if (stream == null) {
System.err.print (
WRITE_NONEXISTFILE);
return;
}
stream.print (data + "\n");
}
}
// -------- YOUR CODE SHOULD GO ABOVE --------
// NOTE:
// READ THE CODE BELOW TO SEE WHAT METHOD YOU CAN USE
static final String
GETPRE_NONEXISTNODE
= "Getting pre of a non-existent node!!!\n",
GETNEXT_NONEXISTNODE
= "Getting next of a non-existent node!!!\n",
SETPRE_NONEXISTNODE
= "Setting pre of a non-existent node!!!\n",
SETNEXT_NONEXISTNODE
= "Setting next of a non-existent node!!!\n",
JETTISON_NONEXISTNODE
= "Jettisoning a non-existent node!!!\n",
LOOKUP_NONEXISTNODE
= "Looking up a non-existent node!!!\n",
INSERT_NONEXISTNODE
= "Inserting a non-existent node!!!\n",
REMOVE_NONEXISTNODE
= "Removing a non-existent node!!!\n",
VIEW_NONEXISTNODE
= "Viewing a non-existent node!!!\n",
WRITE_NONEXISTNODE
= "Writing from a non-existent node!!!\n";
NodeEngine nodeEngine; // To be wrapped by a Node
// Node CTOR METHOD
Node (Base element) {
nodeEngine = new NodeEngine (
this, element, "Node Ctor");
}
// Node GETPRE METHOD
Node getPre () {
if (!exist ()) {
System.err.print (
GETPRE_NONEXISTNODE);
return null;
}
return nodeEngine.pre;
}
// Node GETNEXT METHOD
Node getNext () {
if (!exist ()) {
System.err.print (
GETNEXT_NONEXISTNODE);
return null;
}
return nodeEngine.next;
}
// Node SETNEXT METHOD
void setNext (Node next) {
if (!exist ()) {
System.err.print (
SETNEXT_NONEXISTNODE);
return;
}
nodeEngine.next = next;
}
void setPre (Node pre) {
if (!exist ()) {
System.err.print (
SETPRE_NONEXISTNODE);
return;
}
nodeEngine.pre = pre;
}
// Node JETTISON METHOD
boolean jettisonNode () {
if (!exist ()) {
System.err.print (
JETTISON_NONEXISTNODE);
return false;
}
nodeEngine.jettisonNodeAndData ();
nodeEngine = null;
return true;
}
// Node EXIST METHOD
boolean exist () {
return nodeEngine != null;
}
// Node INSERT METHOD
Node insert (Base element) {
if (!exist ()) {
System.err.print (INSERT_NONEXISTNODE);
return null;
}
return nodeEngine.insert (this, element);
}
// Node REMOVE METHOD
Base remove () {
if (!exist ()) {
System.err.print (REMOVE_NONEXISTNODE);
return null;
}
return nodeEngine.remove ();
}
// Node VIEW METHOD
Base view () {
if (!exist ()) {
System.err.print (
VIEW_NONEXISTNODE);
return null;
}
return nodeEngine.view ();
}
// Node WRITENODE METHOD
void writeNode (PrintStream stream) {
nodeEngine.writeNode (stream);
}
}
}
// catastrophic error messages
static final String
ADNEXT_NONEXIST = "Advance next from non-existent list!!!\n",
ADPRE_NONEXIST = "Advance pre from non-existent list!!!\n",
EMPTY_NONEXIST = "Empyting from non-existent list!!!\n",
ISEMPTY_NONEXIST = "Is empty check from non-existent list!!!\n",
INSERT_NONEXIST = "Inserting to a non-existent list!!!\n",
JETTISON_NONEXIST = "Jettisoning from non-existent list!!!\n",
REMOVE_NONEXIST = "Removing from non-existent list!!!\n",
OCCUPANCY_NONEXIST
= "Occupancy check from non-existent list!!!\n",
VIEW_NONEXIST = "Viewing a non-existent list!!!\n",
WRITE_NONEXISTLIST = "Writing from a non-existent list!!!\n",
WRITE_MISSINGFUNC = "Don't know how to write out elements!!!\n";
private ListEngine listEngine; // The ListEngine instance
public static void debugOn () {
debug = true;
}
public static void debugOff () {
debug = false;
}
// List CTOR METHOD
public List (Base sample, String caller) {
caller += "\n\tcalling List Ctor";
listEngine = new ListEngine (sample, caller);
}
// list JETTISON
public void jettison () {
jettisonList ();
}
// list JETTISON
public boolean jettisonList () {
if (!exist ()) {
System.err.print (JETTISON_NONEXIST);
return false;
}
listEngine.jettisonList ();
listEngine = null;
return true;
}
// List ADVANCENPRE METHOD
public void advancePre () {
if (!exist ()) {
System.err.print (ADPRE_NONEXIST);
return;
}
listEngine.advancePre ();
}
// List ADVANCENEXT METHOD
public void advanceNext () {
if (!exist ()) {
System.err.print (ADNEXT_NONEXIST);
return;
}
listEngine.advanceNext ();
}
// List EMPTY METHOD
public void empty () {
if (!exist ()) {
System.err.print (EMPTY_NONEXIST);
return;
}
while (!isEmpty ()) {
listEngine.remove (0);
}
}
// List EXIST METHOD
public boolean exist () {
return listEngine != null;
}
// List GETOCCUPANCY METHOD
public long getOccupancy () {
return listEngine.occupancy;
}
// List ISEMPTY METHOD
public boolean isEmpty () {
if (!exist ()) {
System.err.print (ISEMPTY_NONEXIST);
return false;
}
return listEngine.isEmpty ();
}
// List INSERT METHOD
public boolean insert (Base element, long where) {
if (!exist ()) {
System.err.print (INSERT_NONEXIST);
return false;
}
return listEngine.insert (element, where);
}
// List REMOVE METHOD
public Base remove (long where) {
if (!exist ()) {
System.err.print (REMOVE_NONEXIST);
return null;
}
return listEngine.remove (where);
}
// List TOSTRING METHOD
public String toString () {
writeList (System.out);
return "";
}
// List VIEW METHOD
public Base view (long where) {
if (!exist ()) {
System.err.print (VIEW_NONEXIST);
return null;
}
return listEngine.view (where);
}
// List WRITELIST METHOD
public void writeList (PrintStream stream) {
if (!exist ()) {
System.err.print (WRITE_NONEXISTLIST);
return;
}
listEngine.writeList (stream);
}
// List WRITEREVERSELIST METHOD
public void writeReverseList (PrintStream stream) {
if (!exist ()) {
System.err.print (WRITE_NONEXISTLIST);
return;
}
listEngine.writeReverseList (stream);
}
}
import java.io.EOFException;
import java.io.*;
public class Driver1 {
private static class MyRec extends Base {
long xxx; // The user id
Tracker tracker; // Tracking memory
public MyRec (long num, String caller) {
xxx = num;
caller += " calling Ctor";
tracker = new Tracker ("MyRec",
Size.of (xxx) + Size.of (tracker),
caller);
}
public String toString () {
return xxx + "";
}
public void jettison () {
tracker.jettison ();
}
}
private static int fromWhere () {
char character; // The character to be read in
int retval; // The return val of where
try {
MyLib.writeline ("Specify 1 for FRONT, 0 for END, or"
+ " location number", System.out);
MyLib.writeline ("\nPlease enter choice: ", System.out);
character = (char) MyLib.getchar ();
if (character == '\n' || !Character.isDigit (character)) {
retval = -1;
MyLib.clrbuf (character);
}
else {
MyLib.ungetc(character);
retval = (int)MyLib.decin ();
MyLib.clrbuf (character);
}
return retval;
}
catch (EOFException eof) {
return -1;
}
}
private static final short NULL = 0;
public static void main (String[] args) {
BaseStack.debugOff ();
MyRec element; // The element reference
BaseStack baseStack; // The stack wrapped list
char command; // The command the user input
boolean status; // The status of the result
String caller = "Driver.main"; // The caller location
for (int index = 0; index < args.length; ++index) {
if (args[index].equals ("-x"))
BaseStack.debugOn ();
}
baseStack = new BaseStack (null, "baseStack in Driver.main");
while (true) {
command = NULL; // reset command each time in loop
MyLib.writeline ("\nThe commands are:\n"
+ " is(e)mpty, (o)ccupancy, (c)heck memory,"
+ "\n"
+ " (a)dvance pre, advance (n)ext,\n"
+ " p(u)sh, (p)op, (t)op,\n"
+ " (i)nsert, (r)emove, (v)iew\n"
+ " (w)rite, (W)rite reverse,\n"
+ "\nPlease enter a command: ", System.out);
try {
// We have to use try catch cuz -1
MyLib.clrbuf ('\n');
command = (char) MyLib.getchar ();
MyLib.clrbuf (command); // get rid of return
switch (command) {
case 'a':
baseStack.advancePre ();
break;
case 'n':
baseStack.advanceNext ();
break;
case 'e':
if (baseStack.isEmpty ()) {
System.out.println ("Stack is empty");
}
else {
System.out.println (
"Stack is not empty");
}
break;
case 'o':
System.out.print ("Number of elements "
+ "in the list is: ");
System.out.println (baseStack.getOccupancy());
break;
case 'c':
Tracker.checkMemoryLeaks ();
break;
case 'i':
System.out.print ("Please enter "
+ "a number to insert into list: ");
element = new MyRec ((int)MyLib.decin (),
"Driver.main");
MyLib.clrbuf ((char) 0);
status = baseStack.insert (element,
Driver1.fromWhere ());
if (!status) {
System.err.println (
"\nWARNING: insert FAILED");
}
break;
case 'r':
element = (MyRec) baseStack.remove (
Driver1.fromWhere ());
if (element == null)
System.err.println (
"\nWARNING: remove FAILED\n");
else {
MyLib.writeline (
"\nNumber removed "
+ "from list is: ",
System.out);
System.out.println (
element.toString ());
element.jettison ();
}
break;
case 'W':
baseStack.writeReverseList (System.err);
break;
case 'w':
baseStack.writeList (System.err);
break;
case 'p':
element = (MyRec) baseStack.pop ();
if (element == null)
System.err.println (
"\nWARNING: pop FAILED\n");
else {
MyLib.writeline (
"\nNumber popped "
+ "from list is: ",
System.out);
System.out.println (
element.toString ());
element.jettison ();
}
break;
case 't':
element = (MyRec) baseStack.top ();
if (element == null)
System.err.println (
"\nWARNING: top FAILED\n");
else {
MyLib.writeline (
"\nNumber topped from "
+ "list is: ",
System.out);
System.out.println (
element.toString ());
}
break;
case 'u':
System.out.print("Please enter "
+ "a number to push into stack: ");
element = new MyRec ((int) MyLib.decin (),
"Driver.main");
MyLib.clrbuf ((char) 0);
status = baseStack.push (element);
if (!status) {
System.err.println (
"\nWARNING: push FAILED");
}
break;
case 'v':
element = (MyRec) baseStack.view (
Driver1.fromWhere ());
if (element == null)
System.err.println (
"\nWARNING: view FAILED\n");
else {
MyLib.writeline (
"\nNumber viewed from "
+ "list is: ",
System.out);
System.out.println (
element.toString ());
}
break;
default:
break;
}
}
catch (Exception eof) {
baseStack.jettison ();
Tracker.checkMemoryLeaks ();
break;
}
}
}
}
import java.io.EOFException;
import java.io.*;
public class Driver2 {
public static boolean copy = true;
private static class MyRec extends Base {
long xxx; // the user id
Tracker tracker; // the tracker for memory
public MyRec (String caller) {
xxx = 0;
caller += " calling Ctor";
tracker = new Tracker ("MyRec",
Size.of (xxx) + Size.of (tracker),
caller);
}
public MyRec (long num, String caller) {
xxx = num;
caller += " calling Ctor";
tracker = new Tracker ("MyRec", Size.of(xxx), caller);
}
public String toString () {
return xxx + "";
}
public void jettison () {
tracker.jettison ();
}
public Base copy (Base element) {
// cast of the Base object to myRec
MyRec myRec = (MyRec) element;
// the new copied myRec object
MyRec newRec = new MyRec (myRec.xxx, "MyRec.copy");
return newRec;
}
}
private static int fromWhere () {
char character; // the character to be read in
int retval; // the return value of where
try {
MyLib.writeline ("Specify 1 for FRONT, 0 for END, or "
+ "location number", System.out);
MyLib.writeline ("\nPlease enter choice: ", System.out);
character = (char) MyLib.getchar ();
if (character == '\n' || !Character.isDigit (character)) {
retval = -1;
MyLib.clrbuf (character);
}
else {
MyLib.ungetc(character);
retval = (int)MyLib.decin ();
MyLib.clrbuf (character);
}
return retval;
}
catch (EOFException eof) {
return -1;
}
}
private static final short NULL = 0;
public static void main (String[] args) {
BaseStack.debugOff ();
// the element copy object
MyRec element = new MyRec ("element in Driver.main");
BaseStack baseStack; // the stack wrapped list
char command; // The command the user input
boolean status; // The status of the result
String caller = "Driver.main"; // The caller location
for (int index = 0; index < args.length; ++index) {
if (args[index].equals ("-x"))
BaseStack.debugOn ();
}
baseStack = new BaseStack (element, "baseStack in Driver.main");
while (true) {
command = NULL; // reset command each time in loop
MyLib.writeline ("\nThe commands are:\n"
+ " is(e)mpty, (o)ccupancy, (c)heck memory,"
+ "\n"
+ " (a)dvance pre, advance (n)ext,\n"
+ " p(u)sh, (p)op, (t)op,\n"
+ " (i)nsert, (r)emove, (v)iew\n"
+ " (w)rite, (W)rite reverse,\n"
+ "\nPlease enter a command: ", System.out);
try {
MyLib.clrbuf ('\n');
command = (char) MyLib.getchar ();
MyLib.clrbuf (command); // get rid of return
switch (command) {
case 'a':
baseStack.advancePre ();
break;
case 'n':
baseStack.advanceNext ();
break;
case 'e':
if (baseStack.isEmpty ()) {
System.out.println ("Stack is empty");
} else {
System.out.println (
"Stack is not empty");
}
break;
case 'o':
System.out.print ("Number of elements "
+ "in the list is: ");
System.out.println (baseStack.getOccupancy());
break;
case 'c':
Tracker.checkMemoryLeaks ();
break;
case 'i':
System.out.print ("Please enter "
+ "a number to insert into list: ");
element.xxx = (int) MyLib.decin ();
MyLib.clrbuf ((char) 0);
status = baseStack.insert (element,
Driver2.fromWhere ());
if (! status) {
System.err.println (
"\nWARNING: insert FAILED");
}
break;
case 'r':
element.jettison ();
element = (MyRec) baseStack.remove(
Driver2.fromWhere ());
if (element == null) {
System.err.println (
"\nWARNING: remove FAILED\n");
element = new MyRec (
"element in Driver.main");
}
else {
MyLib.writeline (
"\nNumber removed from "
+ "list is: ",
System.out);
System.out.println (
element.toString ());
}
break;
case 'W':
baseStack.writeReverseList (System.out);
break;
case 'w':
baseStack.writeList (System.out);
break;
case 'p':
element.jettison ();
element = (MyRec) baseStack.pop ();
if (element == null) {
System.err.println (
"\nWARNING: pop FAILED\n");
element = new MyRec (
"element in Driver.main");
}
else {
MyLib.writeline (
"\nNumber popped from "
+ "list is: ", System.out);
System.out.println (
element.toString ());
}
break;
case 't':
element.jettison ();
element = (MyRec) baseStack.top ();
if (element == null) {
System.err.println (
"\nWARNING: top FAILED\n");
element = new MyRec (
"element in Driver.main");
}
else {
MyLib.writeline (
"\nNumber topped from "
+ "list is: ", System.out);
System.out.println(
element.toString ());
element = new MyRec (
"element in Driver.main");
}
break;
case 'u':
System.out.print("Please enter "
+ "a number to push into stack: ");
element.xxx = (int) MyLib.decin ();
MyLib.clrbuf ((char) 0);
status = baseStack.push (element);
if (! status) {
System.err.println (
"\nWARNING: push FAILED");
}
break;
case 'v':
element.jettison ();
element = (MyRec) baseStack.view(
Driver2.fromWhere ());
if (element == null) {
System.err.println (
"\nWARNING: view FAILED\n");
element = new MyRec (
"element in Driver.main");
}
else {
MyLib.writeline (
"\nNumber viewed from "
+ "list is: ",
System.out);
System.out.println (
element.toString ());
element = new MyRec (
"element in Driver.main");
}
break;
default:
break;
}
}
catch (Exception eof) {
baseStack.jettison ();
element.jettison ();
Tracker.checkMemoryLeaks ();
break;
}
}
}
}
public class BaseStack extends List {
public BaseStack (Base sample, String caller) {
super (sample, caller + " calling BaseStack Ctor");
}
public Base pop () {
return remove (END);
}
public boolean push (Base element) {
return insert (element, END);
}
public Base top () {
return view (END);
}
}
public abstract class Base {
public Base copy (Base element) {
return null;
}
public void jettison () {
return;
}
}