LeetCode brush queue Subjects

641. design cycle deque

Design and Implementation deque.
You need to support implementation of the following operations:
MyCircularDeque (K): Constructor, size deque to be k. insertFront (): adding an element to the head of the deque.
Returns true if the operation was successful. insertLast (): adding an element to the tail of the deque. Returns true if the operation was successful.
deleteFront (): Delete a header element from the deque. Returns true if the operation was successful.
deleteLast (): removes an element from the deque tail. Returns true if the operation was successful.
getFront (): get a header element from the deque. If the deque is empty, return -1. getRear (): get the last element of a deque.
If the deque is empty, return -1. isEmpty (): Check deque is empty. isFull (): Check whether the deque is full. Example:

MyCircularDeque circularDeque = new MycircularDeque (3) ; // set the size of the capacity. 3
circularDeque.insertLast (. 1); // Returns to true
circularDeque.insertLast (2); // Returns to true
circularDeque.insertFront (. 3); // Returns to true
circularDeque .insertFront (4); // full, return false
circularDeque.getRear (); // returns circularDeque.isFull 2 ();
// returns true circularDeque.deleteLast (); // return to true
circularDeque.insertFront (4) ; // returns to true
circularDeque.getFront (); // returns 4

prompt:

All values ​​for the range of [1, 1000] is the number of the operation range [1, 1000] do not use the built-in library deque.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/design-circular-deque
Difficulty: Medium
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.


Thinking: this design topic beginning I was a little puzzled, first contact with the so-called double-ended queue. But the solution to a problem I go to see a clear explanation of the high praise after answering dalao called circular deque what stuff.
To sum up on the word: the queue, rear and front accountable, are responsible for a one (namely management and out), but shared responsibilities deque rear and front of. This tells us that it is important to set the cycle can double-ended queue queue based on conditions, I only need to operate front and rear of sharing (ie, both of which can be managed out). Then the conditional team full of empty team can be designed based on the queue.
Well Here is the sequential storage structure, time and then distributed to achieve Storage Structure.
Finally had to sigh, LeetCode on C ++ simply reap the advantages, high-quality solution to a problem almost all C ++.

typedef struct MyCircularDeque 
{
    int front;
    int rear;
    int MAXSIZE;
    int data[];     //这是伸缩型数组成员,可以理解为动态数组,大小依据内存分配而定。不明白自行查阅
} MyCircularDeque;


bool myCircularDequeIsEmpty(MyCircularDeque* obj);
bool myCircularDequeIsFull(MyCircularDeque* obj);


/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque* myCircularDequeCreate(int k) {
    MyCircularDeque* obj = (MyCircularDeque*)malloc(sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    if(!obj)
        return NULL;
    memset(obj, 0, sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    obj->MAXSIZE = k + 1;	//多分配了一个位置是为了方便队满队空的判断.

    return obj;
}

/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertFront(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->front = (obj->front - 1 + obj->MAXSIZE) % obj->MAXSIZE;
    obj->data[obj->front] = value;
    return true;
}

/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertLast(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->data[obj->rear] = value;
    obj->rear = (obj->rear + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteFront(MyCircularDeque* obj) {
    if(myCircularDequeIsEmpty(obj))
        return false;
    
    obj->front = (obj->front + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteLast(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return false;

     obj->rear = (obj->rear - 1 + obj->MAXSIZE) % obj->MAXSIZE;
     return true;
}

/** Get the front item from the deque. */
int myCircularDequeGetFront(MyCircularDeque* obj) { //OVER
    return myCircularDequeIsEmpty(obj) ? -1 : obj->data[obj->front];
}

/** Get the last item from the deque. */
int myCircularDequeGetRear(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return -1;

    int index = (obj->rear - 1 +obj->MAXSIZE) % obj->MAXSIZE;
    return obj->data[index];
}

/** Checks whether the circular deque is empty or not. */
bool myCircularDequeIsEmpty(MyCircularDeque* obj) { //OVER
    return obj->rear == obj->front ? true : false;
}

/** Checks whether the circular deque is full or not. */
bool myCircularDequeIsFull(MyCircularDeque* obj) {  //OVER
    return (obj->rear +1) % obj->MAXSIZE == obj->front ? true : false;
}

void myCircularDequeFree(MyCircularDeque* obj) {    //OVER
    free(obj);
    obj = NULL;
}

622. design cycle queue

Design and Implementation deque. You need to support the realization of the following:

MyCircularDeque (k): Constructor, size deque to be k. insertFront (): adding an element to the head of the deque.
Returns true if the operation was successful. insertLast (): adding an element to the tail of the deque. Returns true if the operation was successful.
deleteFront (): Delete a header element from the deque. Returns true if the operation was successful.
deleteLast (): removes an element from the deque tail. Returns true if the operation was successful.
getFront (): get a header element from the deque. If the deque is empty, return -1. getRear (): get the last element of a deque.
If the deque is empty, return -1. isEmpty (): Check deque is empty. isFull (): Check whether the deque is full. Example:

MyCircularDeque circularDeque = new MycircularDeque (3) ; // set the size of the capacity. 3
circularDeque.insertLast (. 1); // Returns to true
circularDeque.insertLast (2); // Returns to true
circularDeque.insertFront (. 3); // Returns to true
circularDeque .insertFront (4); // full, return false
circularDeque.getRear (); // returns circularDeque.isFull 2 ();
// returns true circularDeque.deleteLast (); // return to true
circularDeque.insertFront (4) ; // returns to true
circularDeque.getFront (); // returns 4

prompt:

All values ​​for the range of [1, 1000] is the number of the operation range [1, 1000] do not use the built-in library deque.

Source: stay button (LeetCode)
Difficulty: Medium
link: https: //leetcode-cn.com/problems/design-circular-deque
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Thinking: nothing to say. . .

typedef struct MyCircularDeque 
{
    int front;
    int rear;
    int MAXSIZE;
    int data[];     //这是伸缩型数组成员,可以理解为动态数组,大小依据内存分配而定。不明白自行查阅
} MyCircularDeque;


bool myCircularDequeIsEmpty(MyCircularDeque* obj);
bool myCircularDequeIsFull(MyCircularDeque* obj);


/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque* myCircularDequeCreate(int k) {
    MyCircularDeque* obj = (MyCircularDeque*)malloc(sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    if(!obj)
        return NULL;
    memset(obj, 0, sizeof(MyCircularDeque) + (k + 1) * sizeof(int));
    obj->MAXSIZE = k + 1;

    return obj;
}

/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertFront(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->front = (obj->front - 1 + obj->MAXSIZE) % obj->MAXSIZE;
    obj->data[obj->front] = value;
    return true;
}

/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertLast(MyCircularDeque* obj, int value) {
    if(myCircularDequeIsFull(obj))
        return false;

    obj->data[obj->rear] = value;
    obj->rear = (obj->rear + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteFront(MyCircularDeque* obj) {
    if(myCircularDequeIsEmpty(obj))
        return false;
    
    obj->front = (obj->front + 1) % obj->MAXSIZE;
    return true;
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteLast(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return false;

     obj->rear = (obj->rear - 1 + obj->MAXSIZE) % obj->MAXSIZE;
     return true;
}

/** Get the front item from the deque. */
int myCircularDequeGetFront(MyCircularDeque* obj) { //OVER
    return myCircularDequeIsEmpty(obj) ? -1 : obj->data[obj->front];
}

/** Get the last item from the deque. */
int myCircularDequeGetRear(MyCircularDeque* obj) {  //OVER
    if(myCircularDequeIsEmpty(obj))
        return -1;

    int index = (obj->rear - 1 +obj->MAXSIZE) % obj->MAXSIZE;
    return obj->data[index];
}

/** Checks whether the circular deque is empty or not. */
bool myCircularDequeIsEmpty(MyCircularDeque* obj) { //OVER
    return obj->rear == obj->front ? true : false;
}

/** Checks whether the circular deque is full or not. */
bool myCircularDequeIsFull(MyCircularDeque* obj) {  //OVER
    return (obj->rear +1) % obj->MAXSIZE == obj->front ? true : false;
}

void myCircularDequeFree(MyCircularDeque* obj) {    //OVER
    free(obj);
    obj = NULL;
}

Summary : For this cohort design, the main consideration is what the team empty, what team is full, there is clearly the team when empty and full team, how to position rear and front pointer. Considering these conditions, we believe that a queue is not difficult to design.

Published 19 original articles · won praise 2 · Views 2538

Guess you like

Origin blog.csdn.net/SC_king/article/details/104905739