コンテナ構造(スタックとキュー)


      class Queue{
		private int front;
		private int rear;
        private Object[] array;
        private int maxSize;
        public Queue(int maxSize){
            this.front=0;
            this.rear=-1;
            this.maxSize=maxSize;
            array=new Object[this.maxSize];

        }

        public synchronized void push(Object obj){
             if(isFull()){
            	 throw new RuntimeException(" ");
             }
             if(rear==maxSize-1){
            	 rear=-1;
             }
             array[++rear]=obj;
        }

        public synchronized Object pop(){
        	if(isEmpty()){
        		throw new RuntimeException(" ");
        	}
            Object temp=array[front];
            array[front]=null;
            front++;
            if(front>maxSize-1){
            	front=0;
            }
            return temp;
        }


        public synchronized boolean isFull(){
           return (rear+2==front) || (front+maxSize-2==rear);
        }

        public synchronized boolean isEmpty(){
        	return (rear+1==front) || (front+maxSize-1==rear);
        }
	}

	class Stack{
		private int maxSize;
		private int  top;
		private Object[] array;
		public Stack(int maxSize){
              this.maxSize=maxSize;
              this.top=-1;
              this.array=new Object[this.maxSize];
		}

		public synchronized void push(Object obj){
			if(isFull()){
				throw new RuntimeException(" ");
			}else{
                array[++top]=obj;
			}

		}

		public synchronized Object pop(){
			if(isEmpty()){
               throw new RuntimeException(" ");
			}
			Object result=array[top];
			array[top]=null;
			top--;
			return result;
		}

		public synchronized Object peek(){
			if(isEmpty()){
               throw new RuntimeException(" ");
			}
			return array[top];
		}


		public synchronized boolean isFull(){
            if(top==maxSize-1){
            	return true;
            }
            return false;
		}

		public synchronized boolean isEmpty(){
            if(top==-1){
                return true;
            }
            return false;
		}

	}