其他分享
首页 > 其他分享> > Leetcode 641. 设计循环双端队列

Leetcode 641. 设计循环双端队列

作者:互联网

设计实现双端队列。
你的实现需要支持以下操作:

示例:

MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1);			        // 返回 true
circularDeque.insertLast(2);			        // 返回 true
circularDeque.insertFront(3);			        // 返回 true
circularDeque.insertFront(4);			        // 已经满了,返回 false
circularDeque.getRear();  				// 返回 2
circularDeque.isFull();				        // 返回 true
circularDeque.deleteLast();			        // 返回 true
circularDeque.insertFront(4);			        // 返回 true
circularDeque.getFront();				// 返回 4
 

 

提示:

使用双向链表实现双端队列

class MyCircularDeque {
private:
	struct Node {

		int val;
		Node* next;
		Node* prev;

		Node(int val) {
			this->val = val;
			next = NULL;
			prev = NULL;
		}

	};

	Node* head;
	Node* tail;
	int size;
	int maxsize;



public:
	/** Initialize your data structure here. Set the size of the deque to be k. */
	MyCircularDeque(int k) {
		size = 0;
		maxsize = k;
		head = new Node(-1);
		tail = head;

	}

	/** Adds an item at the front of Deque. Return true if the operation is successful. */
	bool insertFront(int value) {
		if (size >= maxsize)
			return false;

		Node* node = new Node(value);
		if (head == tail) {
			// 当前队列为空
			head->next = node;
			node->prev = head;
			tail = node;
		}
		else {
			node->next = head->next;
			head->next->prev = node;
			head->next = node;
			node->prev = head;
		}
		size++;
		return true;
	}

	/** Adds an item at the rear of Deque. Return true if the operation is successful. */
	bool insertLast(int value) {
		if (size >= maxsize)
			return false;

		Node* node = new Node(value);
		if (head == tail) {
			// 空队列情况
			head->next = node;
			node->prev = head;
			tail = node;
		}
		else {
			tail->next = node;
			node->prev = tail;
			tail = node;
		}
		size++;
		return true;
	}

	/** Deletes an item from the front of Deque. Return true if the operation is successful. */
	bool deleteFront() {
		if (head==tail)
			return false;

		Node* temp = head->next;
		if (head->next == tail) {
			// 队列中只有一个元素
			
			delete(temp);
			head->next = NULL;
			tail = head;
		}
		else {
            
			head->next = temp->next;
			temp->next->prev = head;
			delete temp;
		}
		size--;
		return true;

	}

	/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
	bool deleteLast() {
		if (head == tail)
			return false;

		Node* temp = tail;
		tail = temp->prev;
		tail->next = NULL;
		delete temp;
		size--;
		return true;

	}

	/** Get the front item from the deque. */
	int getFront() {

		return head->next == NULL ? -1 : head->next->val;

		/*if(head->next==NULL)
			return -1;
		return head->next->val;*/
	}

	/** Get the last item from the deque. */
	int getRear() {

		return head == tail ? -1 : tail->val;
		/*if(head==tail)
			return -1;
		return tail->val;*/
	}

	/** Checks whether the circular deque is empty or not. */
	bool isEmpty() {
		return head == tail;

	}

	/** Checks whether the circular deque is full or not. */
	bool isFull() {
		return size == maxsize;
	}
};

 

标签:head,return,双端,next,641,tail,true,Leetcode
来源: https://blog.csdn.net/wwxy1995/article/details/87884594