Implement the deque ADT using a circular array
Posted on 2007-02-13 06:55 QT_pixy 阅读(1122) 评论(4) 收藏 举报
A deque is a double-ended queue that supports insertion and deletion at both the front and the rear of the queue. Implement the deque ADT using a circular array.
Extend the array by doubling its capacity when a new element is inserted into a full deque. Use an initial array size of 8.
Shrink the array by half the current size N when the number of elements in the deque falls below N/4 (but the minimum capacity should be 8, the INITIAL_CAPACITY).
/*****************************
* Implement the deque ADT
* using a circular array.
* Author: QT_pixy
* Date: Feb. 5, 07
*****************************/

public class arrayDeque<AnyType>
{
private static final int INITIAL_CAPACITY = 8;

private AnyType[] queue;
private int max_size;
private int f_index;
private int r_index;
public arrayDeque()
{
queue = (AnyType[])new Object[INITIAL_CAPACITY];
max_size = INITIAL_CAPACITY;
f_index = 0;
r_index = f_index;
}


/**
* Returns the number of items in this collection.
* @return the number of items in this collection.
*/
public int size( )
{
return (max_size - f_index + r_index) % max_size;
}


/**
* Returns true if this collection is empty.
* @return true if this collection is empty.
*/
public boolean isEmpty( )
{
return (f_index == r_index);
}


/**
* Returns the first element of the deque
*
*/
public AnyType first()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
return queue[f_index];
}


/**
* Returns the last element of the deque
*
*/
public AnyType last()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
return queue[r_index - 1];
}


/**
* Inserts e at the beginning (as the first element) of the deque
*
*/
public void insertFirst(AnyType e)
{
if(size() == max_size - 1)
throw new QueueFullException("Queue full!");
if(isEmpty())
{
queue[f_index] = e;
r_index++;
}
else
{
if(f_index == 0)
{
f_index = max_size - 1;
queue[f_index] = e;
}
else
queue[--f_index] = e;
}
if(size() == max_size - 1)
extend();
}


/**
* Inserts e at the end (as the last element) of the deque
*
*/
public void insertLast(AnyType e)
{
if(size() == max_size - 1)
throw new QueueFullException("Queue full!");
queue[r_index] = e;
r_index = (r_index + 1) % max_size;
if(size() == max_size - 1)
extend();
}


/**
* Removes and returns the first element of the deque
*
*/
public AnyType removeFirst()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
AnyType temp = queue[f_index];
f_index = (f_index + 1) % max_size;

if(size() < max_size / 4 && max_size > INITIAL_CAPACITY)
shrink();
return temp;
}


/**
* Removes and returns the last element of the deque
*
*/
public AnyType removeLast()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
AnyType temp;
temp = queue[r_index % max_size - 1];
r_index = r_index % max_size - 1;
if(size() < max_size / 4 && max_size > INITIAL_CAPACITY)
shrink();
return temp;
}
private void extend()
{
AnyType[] temp = (AnyType[])new Object[max_size * 2];
int i = f_index;
int x = 0;
while (i != r_index)
{
temp[x] = queue[i];
i = (i + 1) % max_size;
x++;
}
max_size = max_size * 2;
f_index = 0;
r_index = x;
queue = temp;
}
private void shrink()
{
int new_size = max_size / 2;
if(new_size < 8)
new_size = 8;
AnyType[] temp = (AnyType[])new Object[new_size];
int i = f_index;
int x = 0;
while (i != r_index)
{
temp[x] = queue[i];
i = (i + 1) % max_size;
x++;
}
max_size = new_size;
f_index = 0;
r_index = x;
queue = temp;
}
}
Here is the Main program.
public class Main5
{
public static void main(String[] args)
{
arrayDeque<Integer> deck = new arrayDeque<Integer>();

deck.insertFirst(new Integer(3));
deck.insertFirst(new Integer(5));
// (5,3)
System.out.println(deck.removeFirst()); // prints 5
// (3)
deck.insertLast(new Integer(7));
// (3,7)
System.out.println(deck.removeFirst()); // prints 3
//(7)
System.out.println(deck.removeLast()); // prints 7
// ()
try
{
Integer a = deck.removeFirst();
System.out.println(a);
}
catch (Exception ex)
{
System.err.println(ex);
}

}
}
Extend the array by doubling its capacity when a new element is inserted into a full deque. Use an initial array size of 8.
Shrink the array by half the current size N when the number of elements in the deque falls below N/4 (but the minimum capacity should be 8, the INITIAL_CAPACITY).
/*****************************
* Implement the deque ADT
* using a circular array.
* Author: QT_pixy
* Date: Feb. 5, 07
*****************************/
public class arrayDeque<AnyType>
{
private static final int INITIAL_CAPACITY = 8;
private AnyType[] queue;
private int max_size;
private int f_index;
private int r_index;
public arrayDeque()
{
queue = (AnyType[])new Object[INITIAL_CAPACITY];
max_size = INITIAL_CAPACITY;
f_index = 0;
r_index = f_index;
}

/**
* Returns the number of items in this collection.
* @return the number of items in this collection.
*/
public int size( )
{
return (max_size - f_index + r_index) % max_size;
}

/**
* Returns true if this collection is empty.
* @return true if this collection is empty.
*/
public boolean isEmpty( )
{
return (f_index == r_index);
}

/**
* Returns the first element of the deque
*
*/
public AnyType first()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
return queue[f_index];
}

/**
* Returns the last element of the deque
*
*/
public AnyType last()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
return queue[r_index - 1];
}

/**
* Inserts e at the beginning (as the first element) of the deque
*
*/
public void insertFirst(AnyType e)
{
if(size() == max_size - 1)
throw new QueueFullException("Queue full!");
if(isEmpty())
{
queue[f_index] = e;
r_index++;
}
else
{
if(f_index == 0)
{
f_index = max_size - 1;
queue[f_index] = e;
}
else
queue[--f_index] = e;
}
if(size() == max_size - 1)
extend();
}

/**
* Inserts e at the end (as the last element) of the deque
*
*/
public void insertLast(AnyType e)
{
if(size() == max_size - 1)
throw new QueueFullException("Queue full!");
queue[r_index] = e;
r_index = (r_index + 1) % max_size;
if(size() == max_size - 1)
extend();
}

/**
* Removes and returns the first element of the deque
*
*/
public AnyType removeFirst()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
AnyType temp = queue[f_index];
f_index = (f_index + 1) % max_size;
if(size() < max_size / 4 && max_size > INITIAL_CAPACITY)
shrink();
return temp;
}

/**
* Removes and returns the last element of the deque
*
*/
public AnyType removeLast()
{
if(isEmpty())
throw new QueueEmptyException("Empty Queue");
AnyType temp;
temp = queue[r_index % max_size - 1];
r_index = r_index % max_size - 1;
if(size() < max_size / 4 && max_size > INITIAL_CAPACITY)
shrink();
return temp;
}
private void extend()
{
AnyType[] temp = (AnyType[])new Object[max_size * 2];
int i = f_index;
int x = 0;
while (i != r_index)
{
temp[x] = queue[i];
i = (i + 1) % max_size;
x++;
}
max_size = max_size * 2;
f_index = 0;
r_index = x;
queue = temp;
}
private void shrink()
{
int new_size = max_size / 2;
if(new_size < 8)
new_size = 8;
AnyType[] temp = (AnyType[])new Object[new_size];
int i = f_index;
int x = 0;
while (i != r_index)
{
temp[x] = queue[i];
i = (i + 1) % max_size;
x++;
}
max_size = new_size;
f_index = 0;
r_index = x;
queue = temp;
}
}Here is the Main program.
public class Main5
{
public static void main(String[] args)
{
arrayDeque<Integer> deck = new arrayDeque<Integer>();
deck.insertFirst(new Integer(3));
deck.insertFirst(new Integer(5));
// (5,3)
System.out.println(deck.removeFirst()); // prints 5
// (3)
deck.insertLast(new Integer(7));
// (3,7)
System.out.println(deck.removeFirst()); // prints 3
//(7)
System.out.println(deck.removeLast()); // prints 7
// ()
try
{
Integer a = deck.removeFirst();
System.out.println(a);
}
catch (Exception ex)
{
System.err.println(ex);
}
}
}


浙公网安备 33010602011771号