容器结构(栈与队列)

      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;
		}

	}

你可能感兴趣的:(object,null,Class)