Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArrayQueue{
- private int count;
- private int fIndex,rIndex;
- private int maxSize;
- private Object []data;
- public ArrayQueue(){
- data=new Object[10];
- maxSize=10;
- fIndex=0;
- rIndex=maxSize-1;
- count=0;
- }
- public ArrayQueue(int _maxSize){
- data=new Object[_maxSize];
- maxSize=_maxSize;
- fIndex=0;
- rIndex=maxSize-1;
- count=0;
- }
- public void add(Object _data){
- if(count==maxSize){
- System.out.println("Stack Over Flow");
- }
- else{
- rIndex=(rIndex+1)%maxSize;
- data[rIndex]=_data;
- count++;
- }
- }
- public void delete(){
- if(count==0){
- System.out.println("Stack Under Flow");
- }
- else{
- count--;
- data[fIndex]=null;
- fIndex=(fIndex+1)%maxSize;
- }
- }
- public Object front(){
- if(data[fIndex]==null){
- return null;
- }
- else{
- return data[fIndex];
- }
- }
- public Object rear(){
- if(data[rIndex]==null){
- return null;
- }
- else{
- return data[rIndex];
- }
- }
- public int size(){
- return count;
- }
- public boolean isEmpty(){
- return (count==0);
- }
- public boolean isFull(){
- return (count==maxSize);
- }
- public String toString()
- {
- String output = "";
- if(!isEmpty()){
- int index=0;
- for(int i=fIndex;i<count+fIndex;i++){
- index=i%maxSize;
- output += "[" + data[index] + "]";
- }
- return output;
- }
- else{
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment