622. 设计循环队列 & 641. 设计循环双端队列

622. 设计循环队列

额外定义了一个length用来保存当前循环队列中元素的个数,效率会降低一些,当初学习数据结构的时候,书上的做法是额外留了一个空间用来判断元素是否满或空,等到回学校再看看书吧。

class MyCircularQueue {
	int[] queue; // 用数组模拟
	int front; // 指向队首元素
	int rear; // 指向队尾元素
	int capacity; // 循环队列的长度
	int length; // 循环队列中元素的个数

	/** Initialize your data structure here. Set the size of the queue to be k. */
	public MyCircularQueue(int k) {
		queue = new int[k];
		front = 0;
		rear = 0;
		capacity = k;
		length = 0;
	}

	/**
	 * Insert an element into the circular queue. Return true if the operation is
	 * successful.
	 */
	public boolean enQueue(int value) {
		if (length < capacity) {
			rear = (rear + 1) % capacity;
			if(length == 0)
				front = rear;
			queue[rear] = value;
			length++;
			return true;
		} else 
			return false;
	}

	/**
	 * Delete an element from the circular queue. Return true if the operation is
	 * successful.
	 */
	public boolean deQueue() {
		if (length > 0) {
			length--;
			front = (front + 1)%capacity;
			if (length == 1) {	//如果当前剩余元素只有1个,就将首尾指针对齐
				rear = front;
			}
			return true;
		} else 
			return false;
	}

	/** Get the front item from the queue. */
	public int Front() {
		return length > 0 ? queue[front] : -1;
	}

	/** Get the last item from the queue. */
	public int Rear() {
		return length > 0 ? queue[rear] : -1;
	}

	/** Checks whether the circular queue is empty or not. */
	public boolean isEmpty() {
		return length == 0 ? true : false;
	}

	/** Checks whether the circular queue is full or not. */
	public boolean isFull() {
		return length == capacity ? true : false;
	}
}

641. 设计循环双端队列

一模一样的思路做这个题,基本不用改动。
循环双端队列和循环队列的区别就是前者可以在队首添加或删除元素,从而改变了队列先进先出的特性。

class MyCircularDeque {
    int[] queue; // 用数组模拟
	int front; // 指向队首元素
	int rear; // 指向队尾元素
	int capacity; // 循环队列的长度
	int length; // 循环队列中元素的个数

    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
		queue = new int[k];
		front = 0;
		rear = 0;
		capacity = k;
		length = 0;        
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public boolean insertFront(int value) {
        if(length < capacity){
            front = (front - 1 + capacity) % capacity;
            if(length == 0)
                front = rear;
            queue[front] = value;
            length ++ ;
            return true;
        }else
            return false; 
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public boolean insertLast(int value) {
        if(length < capacity){
            rear = (rear + 1) % capacity;
            if(length == 0)
                front = rear;
            queue[rear] = value;
            length ++ ;
            return true;
        }else
            return false;       
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public boolean deleteFront() {
        if (length > 0) {
			length--;
			front = (front + 1)%capacity;
			if (length == 1) {	//如果当前剩余元素只有1个,就将首尾指针对齐
				rear = front;
			}
			return true;
		} else 
			return false;
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public boolean deleteLast() {
         if (length > 0) {
			length--;
			rear = (rear - 1 + capacity)%capacity;
			if (length == 1) {	//如果当前剩余元素只有1个,就将首尾指针对齐
				rear = front;
			}
			return true;
		} else 
			return false;       
    }
    
    /** Get the front item from the deque. */
    public int getFront() {
        return length > 0 ? queue[front] : -1;
    }
    
    /** Get the last item from the deque. */
    public int getRear() {
        return length > 0 ? queue[rear] : -1;
    }
    
    /** Checks whether the circular deque is empty or not. */
    public boolean isEmpty() {
        return length == 0 ? true : false;
    }
    
    /** Checks whether the circular deque is full or not. */
    public boolean isFull() {
		return length == capacity ? true : false;        
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */

时隔多日,终于复习的时候看到了,补上数据结构书上经典的实现方式:

  • 留下一个多余的空间用于区分是队列满还是队列空。
  • 队首指针始终指向有效的队首元素
  • 队尾指针始终指向有效的队尾元素的下一个位置
class MyCircularQueue {
	int[] queue; // 用数组模拟
	int front; // 指向队首元素
	int rear; // 指向队尾元素的下一个位置
    int capacity; //容量加1,浪费一个位置

	public MyCircularQueue(int k) {
		queue = new int[k+1];   //多留一个空间用于区分满和空
        capacity = k+1;
		front = 0;
		rear = 0;
	}

	public boolean enQueue(int value) {
        //入队判满
        if (isFull()) {
            return false;
        }
        queue[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
	}

	public boolean deQueue() {
        //出队判空
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % capacity;
        return true;
	}

	public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return queue[front];
	}

	public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        return queue[(rear - 1 + capacity) % capacity];
	}

	public boolean isEmpty() {
		return rear == front;
	}

	public boolean isFull() {
		return (rear + 1) % capacity == front;
	}
}

另一种实现方式:
一个空间也不多留,但是另设一个tag位来区分队满和队空

class MyCircularQueue {
	int[] queue; // 用数组模拟
	int front; // 指向队首元素
	int rear; // 指向队尾元素的下一个位置
    int capacity; //容量加1,浪费一个位置
    int tag = 0;    // 0表示队空,1表示队满
	/** Initialize your data structure here. Set the size of the queue to be k. */
	public MyCircularQueue(int k) {
		queue = new int[k];   //多留一个空间用于区分满和空
        capacity = k;
		front = 0;
		rear = 0;
	}

	/**
	 * Insert an element into the circular queue. Return true if the operation is
	 * successful.
	 */
	public boolean enQueue(int value) {
        //入队判满
        if (isFull()) {
            return false;
        }
        queue[rear] = value;
        rear = (rear + 1) % capacity;
        //入队可能导致队满,修改标志为1
        tag = 1;
        return true;
	}

	/**
	 * Delete an element from the circular queue. Return true if the operation is
	 * successful.
	 */
	public boolean deQueue() {
        //出队判空
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % capacity;
        //出队可能导致队空,修改标志为0
        tag = 0;
        return true;
	}

	/** Get the front item from the queue. */
	public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return queue[front];
	}

	/** Get the last item from the queue. */
	public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        return queue[(rear - 1 + capacity) % capacity];
	}

	/** Checks whether the circular queue is empty or not. */
	public boolean isEmpty() {
        if(rear == front && tag == 0)
		    return true;
        else
            return false;
	}

	/** Checks whether the circular queue is full or not. */
	public boolean isFull() {
        if(rear == front && tag == 1)
		    return true;
        else
            return false;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值