circular QUEUE HOW TO PRINT!!
public class Queues
{
static Console c;
public static void main (String[] args)
{
c = new Console ();
StraightLine[] x = new StraightLine [5];
x [0] = new StraightLine ();
x [1] = new StraightonRemove ();
x [2] = new ShiftOnFull ();
x [3] = new Circular ();
char t = 'a';
int n = 1;
while (t != 'x')
{
c.println ("enter your command! x to exit i to insert d to delete r to reset");
c.setCursor (10, 1);
t = c.getChar ();
if (t == 'i')
{
x [0].add (n);
x [1].add (n);
x [2].add (n);
x [3].add (n);
n++;
if (n == 100)
n = 1;
c.println ("Straight Line!");
x [0].print (c);
c.println ("Shift on each Remove");
x [1].print (c);
c.println ("Shift on full");
x [2].print (c);
c.println ("Circular");
x [3].print (c);
}
else if (t == 'd')
{
x [0].remove ();
c.println ("Straight Line!");
x [0].print (c);
x [1].remove ();
c.println ("Shift on each Remove");
x [1].print (c);
x [2].remove ();
c.println ("Shift on full");
x [2].print (c);
x [3].remove ();
c.println ("Circular");
x [3].print (c);
}
else if (t == 'r')
{
c.clear ();
x [0] = new StraightLine ();
x [1] = new StraightonRemove ();
x [2] = new ShiftOnFull ();
x [3] = new Circular ();
n = 1;
}
}
}
}
/*class StraightLine
Fields
x- the integer array
size- physical size of the array
F- front position in the array
B- back position in the array
Methods
constructors
add- adds a given element to the array
remove- removes element from the array
empty- returns whether or not the queue is empty
full- returns whether or not the queue is full
print- prints the queue array
*/
class StraightLine
{
int[] x;
int size;
int F;
int B;
public StraightLine (int size)
{
F = 0;
B = 0;
this.size = size;
x = new int [size];
}
public StraightLine ()
{
F = 0;
B = 0;
size = 10;
x = new int [size];
}
/* method add
inserts given data into the array
parameters: data, integer of data to be inserted
returns:boolean, true if data was successfully added, false if not
*/
public boolean add (int data)
{
if (!full ())
{
x [B] = data;
B++;
return true;
}
else
return false;
}
/* method remove
removes an element from the array
parameters:none
returns:integer of the element value that was removed
*/
public int remove ()
{
int result = 99999999;
if (!empty ())
{
result = x [F];
F++;
}
return result;
}
/* method empty
returns whether the array is empty
parameters:none
returns: boolean, true if array is empty, false if not
*/
public boolean empty ()
{
return F == B;
}
/* method full
returns whether or not the array is full
parameters:none
returns: boolean, true if the array if full,false if not
*/
public boolean full ()
{
return B == size;
}
/* method print
prints the array
parameters:Console c
returns:none(void)
*/
public void print (Console c)
{
String result = "";
StringBuffer b = new StringBuffer (" ");
DecimalFormat f = new DecimalFormat ("00");
int spot = 0;
for (int i = 0 ; i < F ; i++)
result += "-- ";
for (int i = F ; i < B ; i++)
result += f.format (x [i]) + " ";
for (int i = B ; i < size ; i++)
result += "-- ";
c.println (result);
b.setCharAt (F * 3, 'F');
b.setCharAt ((B * 3) + 1, 'B');
c.print (b.toString ());
if (full ())
c.print ("(Full)");
if (empty ())
c.print ("(Empty)");
c.println ("");
}
}
/* class StraightonRemove
Fields-
None(inherited only)
Methods
constructors
add- adds a given integer to the array given its value
remove- to remove element from the queue
print- prints the queue
full- returns whether or not the Queue is full
empty- returns whether or not the Queue is empty
*/
class StraightonRemove extends StraightLine
{
public StraightonRemove (int size)
{
super (size);
if (size < 0)
size = 10;
B = 0;
F = 0;
this.size = size;
x = new int [size];
}
public StraightonRemove ()
{
super (10);
size = 10;
B = 0;
F = 0;
this.size = size;
x = new int [size];
}
/* method remove
toremove element from the queue
parameters:none
returns:integer,the value of the element that was removed */
public int remove ()
{
boolean more = false;
int result;
if (!empty ())
{
B--;
int i = F;
while (i < B)
{
x [i] = x [i + 1];
i++;
}
result = x [B];
}
else
result = 99999999;
return result;
}
}
/* class ShiftOnFull
Fields
none(only inherited)
Methods
constructors
remove- removes from the Queue
print- prints the Queue
reallyFull- returns whether or not the array is really full
*/
class ShiftOnFull extends StraightLine
{
public ShiftOnFull (int size)
{
super (size);
B = 0;
F = 0;
if (size < 0)
size = 10;
this.size = size;
x = new int [size];
}
public ShiftOnFull ()
{
B = 0;
F = 0;
size = 10;
this.size = size;
x = new int [size];
}
/* method reallyFull
returns whether or not the Queue is really full
parameters:none
returns: boolean, true if Queue is really full, false if not
*/
public boolean full ()
{
return F == 0 && super.full ();
}
public boolean add (int data)
{
if (full ())
return false;
else
{
if (F != 0 && B == size)
{
int i = F;
int pos = 0;
while (i < B)
{
x [pos++] = x [i++];
System.out.println ("did the moving");
}
B = B - F;
x [B] = data;
B++;
F = 0;
}
else if (!super.full ())
{
super.add (data);
return true;
}
return false;
}
}
}
class Circular extends StraightLine
{
public Circular (int size)
{
super (size);
if (size < 0)
size = 10;
this.size = size;
F = 0;
B = 0;
x = new int [size];
}
public Circular ()
{
size = 10;
this.size = size;
F = 0;
B = 0;
x = new int [size];
}
public boolean full ()
{
return B + 1 == F;
}
public boolean add (int data)
{
if (!full ())
{
if (B != F - 1)
x [B] = data;
if (B == size - 1)
B = (B + 1) % size;
else
B++;
return true;
}
else
return false;
}
public int remove ()
{
int result = 99999999;
if (!empty ())
{
result = x [F];
F++;
}
return result;
}
public void print (Console c)
{
String result = "";
StringBuffer b = new StringBuffer (" ");
DecimalFormat f = new DecimalFormat ("00");
int spot = 0;
for (int i = 0 ; i < F ; i++)
result += "-- ";
for (int i = F ; i < B ; i++)
result += f.format (x [i]) + " ";
for (int i = B ; i < size ; i++)
result += "-- ";
c.println (result);
b.setCharAt (F * 3, 'F');
b.setCharAt ((B * 3) + 1, 'B');
c.print (b.toString ());
if (full ())
c.print ("(Full)");
if (empty ())
c.print ("(Empty)");
c.println ("");
}
/*
*/
}