62,621
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
import java.io.*;
import java.lang.*;
//*******************************ObjArraySuperclass part***********************
class ObjArraySuperclass
{
private static final int DEFAULT_SIZE = 20;
private Object[] theArray;
public ObjArraySuperclass()
{
theArray = new Object[DEFAULT_SIZE];
}
public ObjArraySuperclass( int size )
{
theArray = new Object[size];
}
public ObjArraySuperclass( ObjArraySuperclass theOriginal ) // copy constructor
{
if ( theOriginal == null )
{
System.out.println("fatal error. null pointer in copy constructor");
System.exit(0);
}
else
{
theArray = new Object[theOriginal.theArray.length];
for (int i = 0; i < theArray.length; i++)
theArray[i] = theOriginal.theArray[i];
}
}
public boolean equals( Object theOther )
{
if ( theOther == null )
return false;
else if ( theOther == this )
return true;
else if ( getClass() != theOther.getClass())
return false;
else
{
ObjArraySuperclass o = (ObjArraySuperclass)theOther;
if ( theArray.length != o.theArray.length ) return false;
for ( int i = 0; i < theArray.length; i++ )
if ( theArray[i] != o.theArray[i] ) return false;
}
return true;
}
public int length()
{
return theArray.length;
}
public Object get( int index )
{
if ( index >= 0 && index < theArray.length )
return theArray[index];
else
return Integer.MIN_VALUE;
}
public boolean put( int index, Object value )//
{
if ( index >= 0 && index < theArray.length )
{
theArray[index] = value;
return true;
}
else
return false;
}
}
//end of superclass
//*******************************deque part************************************
class deque extends ObjArraySuperclass
{
enum ErrorMessage{ OVERFLOW, UNDERFLOW, COPYERROR };
private int front = 0;
private int rear = 0;
public deque()
{
super();
}
public deque(int size)
{
super(size);
}
public deque(deque theOther)
{
super(theOther);
this.front = theOther.front;
this.rear = theOther.rear;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public Object remove()
{
if ( isNotEmpty() )
{
front = (front+1) % super.length();
return super.get(front);
}
else
{
QueueError( ErrorMessage.UNDERFLOW );
return null;
}
}
public Object removeRear()
{
if (isNotEmpty())
{
rear = (rear-1)%(super.length());//the super class has already has the length() method
return super.get(rear);//return the value
}
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}
}
public Object peekRear()
{
if(isNotEmpty())
{
return super.get(rear);
}
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}
}
public Object peek()
{
if(isNotEmpty())
{
return super.get(front);
}
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}
}
public void insert(Object value)
{
if ( isNotFull() )
{
rear = (rear+1) % super.length();//this is the basic operation of calculating the loc
super.put(rear,value);
}
else
QueueError( ErrorMessage.OVERFLOW );//this is defined in this class
}
public void addFront(Object value)
{
if (isNotFull())
{
front = (front-1) % super.length();
super.put(front,value);
}
else
QueueError( ErrorMessage.OVERFLOW );
}
public void push(Object value)//same as addInFront() method
{
if (isNotFull())
{
front = (front-1) % super.length();
super.put(front,value);
}
else
QueueError( ErrorMessage.OVERFLOW );
}
public Object pop() // same as remove() method
{
if ( isNotEmpty() )
{
front = (front+1) % super.length();
return super.get(front);
}
else
{
QueueError( ErrorMessage.UNDERFLOW );
return null;//empty
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public boolean isEmpty()
{
return front == rear;
}
public boolean isFull()
{
return (rear+1)%super.length() == front;
}
public boolean isNotFull()
{
return !isEmpty();
}
public boolean isNotEmpty()
{
return !isFull();
}
private void QueueError( ErrorMessage msg )
{
switch ( msg )
{
case UNDERFLOW:
System.out.println("QueueError: Queue Underflow" );
break;
case OVERFLOW:
System.out.println("QueueError: Queue Overflow" );
break;
case COPYERROR:
System.out.println("QueueError: null pointer sent of copy constructor" );
break;
}
System.exit(0);
}
public String toString()
{
String theString = "\tfront: " + front + " rear: "
+ rear + " contents: " ;
int loc = front;
if ( isNotEmpty() )
do
{
loc = (loc+1) % super.length();
theString = theString + super.get(loc) + " ";
}
while ( loc != rear );
theString = theString + "\n";
return theString;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
//end of deque definition
//*******************************client part***********************************
public class dequeClient
{
public static void main(String[] args) throws NullPointerException
{
try
{
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String theLine;
Integer theChar;
deque theQueue = new deque();
deque theStack = new deque();
while((theLine = br.readLine())!=null)//read a line store it in to theLine
{
System.out.println("bofore the peration: "+theLine);
for(int i=0; i<theLine.length(); i++)//this is to compute the opeartions
{
theChar = (Integer)(int)theLine.charAt(i);
while(theChar > (Integer )theQueue.peekRear())
{
Integer tempChar = (Integer )theQueue.removeRear();
theStack.push(tempChar);
}
while(theChar < (Integer )theStack.peek())
{
Integer tempChar = (Integer )theStack.pop();
theQueue.insert(tempChar);
}
//insert the new letter onto the rear of queue
theQueue.insert(theChar);
Integer temp;
while((temp = (Integer )theStack.pop())!= null )
{
theQueue.insert(temp);
}
}
System.out.println("after the opeartion: "+theQueue.toString()+"\n");
}
}
catch(IOException e)
{
System.out.println("IOexception ");
System.exit(-1);
System.out.println("ioException occurs");
System.exit(-1);
}
}
}
theChar = (Integer)(int)theLine.charAt(i);