[Leedcode] Necessary interview questions for stacks and queues (Phase 4)

[Leedcode] Necessary interview questions for stacks and queues (Phase 4)



1. Topic

Leedcode link


insert image description here


insert image description here


insert image description here

I will implement and explain these interfaces one by one that we need to implement!


2. Idea + diagram

1. Declare structure

insert image description here

The code is as follows (example):

typedef struct {
    
    
    int *a;
    int front;
    int tail;
    int k;
} MyCircularQueue;

2. Circular linked list opens up dynamic structure space

insert image description here


insert image description here


insert image description here


The code is as follows (example):

MyCircularQueue* myCircularQueueCreate(int k) {
    
    
    MyCircularQueue* cp = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    cp->a = (int*)malloc(sizeof(int)*(k+1));
    cp->front = cp->tail = 0;
    cp->k = k;
    return cp;
}

3. Insert an element into the circular queue

insert image description here


insert image description here


The code is as follows (example):

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    
    
    if(myCircularQueueIsFull(obj))
    {
    
    
        return false;
    }
    obj->a[obj->tail] = value;
    obj->tail++;
    obj->tail %= (obj->k+1);
    return true;
}

4. Delete an element in the circular queue

Just ++obj->front directly, pay attention: front is a circular linked list, you can: obj->front %= (obj->k+1);
insert image description here


insert image description here


The code is as follows (example):

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    
    
    if(myCircularQueueIsEmpty(obj))
    {
    
    
        return false;
    }

    obj->front++;
    obj->front %= (obj->k+1);
    return true;
}

5. Get elements from the head of the queue

Directly return obj->a[obj->front] , don't forget that the title request cannot find return -1

The code is as follows (example):

int myCircularQueueFront(MyCircularQueue* obj) {
    
    
    //取首元素数据
     if(myCircularQueueIsEmpty(obj))
    {
    
    
        return -1;
    }
    return obj->a[obj->front];
}

6. Get the element at the end of the queue

insert image description here


![Insert picture description here](https://img-blog.csdnimg.cn/2ae4e685ecae47c3aacbd8cbc565b768.pn


![Insert picture description here](https://img-blog.csdnimg.cn/0822c4a4cf264bd08c279c80f2b705fd.png


The code is as follows (example):

int myCircularQueueRear(MyCircularQueue* obj) {
    
    
     if(myCircularQueueIsEmpty(obj))
    {
    
    
        return -1;
    }
    if(obj->tail == 0)
    {
    
    
        return obj->a[obj->k];
    }
    else
    {
    
    
        return obj->a[obj->tail-1];
    }
}

7. Check if the circular queue is empty

The remaining interfaces are relatively simple, just go to the code!

The code is as follows (example):

bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
{
    
    
    return obj->front == obj->tail;
}

8. Check if the circular queue is full

Whether it is judged empty or full, k+1 spaces must be opened, which is the essence of this question!!!


insert image description here

The code is as follows (example):

bool myCircularQueueIsFull(MyCircularQueue* obj) 
{
    
    
    return (obj->tail+1)%(obj->k+1) == obj->front;
}

9. Release the circular linked list

Free up space twice in turn !

The code is as follows (example):

void myCircularQueueFree(MyCircularQueue* obj) 
{
    
    
    free(obj->a);
    free(obj);
}

3. Possible problems

At this point we submit the code, the following problems will arise!
insert image description here


Here it shows that the two functions myCircularQueueIsEmpty and myCircularQueueIsFull we defined in the interface need to be declared!
insert image description here


4. Overall source code

The code is as follows (example):

//声明结构体
typedef struct {
    
    
    int *a;
    int front;
    int tail;
    int k;
} MyCircularQueue;

bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);

MyCircularQueue* myCircularQueueCreate(int k) {
    
    
    //开辟动态结构体空间,用指针p维护
    MyCircularQueue* cp = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    //一次性动态开辟好数组
    cp->a = (int*)malloc(sizeof(int)*(k+1));
    //初始化值
    cp->front = cp->tail = 0;
    cp->k = k;
    //函数要求返回指针  指针的类型是MyCircularQueue
    return cp;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    
    
    //如果数组是满的返回 false
    if(myCircularQueueIsFull(obj))//注意在这里调用了下面的自定义函数 所以要在前面提前声明函数
    {
    
    
        return false;
    }
    //如果数组没满来到这里 进行导入输入 并修改tail的值 
    obj->a[obj->tail] = value;
    obj->tail++;
    obj->tail %= (obj->k+1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    
    
    if(myCircularQueueIsEmpty(obj))//注意在这里调用了下面的自定义函数 所以要在前面提前声明函数
    {
    
    
        return false;
    }

    obj->front++;
    obj->front %= (obj->k+1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    
    
    //取首元素数据
     if(myCircularQueueIsEmpty(obj))
    {
    
    
        return -1;
    }
    return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    
    
    //取尾元素数据
     if(myCircularQueueIsEmpty(obj))
    {
    
    
        //当数组为空 则返回-1
        return -1;
    }
    //数组不为空来到这 tail指向的地址的值肯定是空的 所以需要找tail后面的一个元素(tail-1)
    //但是由于是环形数组当tail是0下标的时候 0-1 = -1  可实际情况我们是要找数组的最后一个下标 数组最后一个下标不可能是-1
    //那么特殊情况 特殊对待做if else判断
    if(obj->tail == 0)
    {
    
    
        return obj->a[obj->k];
    }
    else
    {
    
    
        return obj->a[obj->tail-1];
    }
    //其实下面还有一种运算方法  套用tail=0 我们可以发现 i就等于了数组最后一个元素的下标k 
    //因为数组实质开辟空间是开辟的k+1个元素 那么k就是数组的尾元素下标
    //int i = (obj->tail+obj->k)%(obj->k+1);
    //return obj->a[i];
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    
    
    //判断数组是否为空
    return obj->front == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    
    
    //判断数组是否为满
    return (obj->tail+1)%(obj->k+1) == obj->front;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    
    
    //依次释放空间
    free(obj->a);
    free(obj);
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);
 
 * bool param_2 = myCircularQueueDeQueue(obj);
 
 * int param_3 = myCircularQueueFront(obj);
 
 * int param_4 = myCircularQueueRear(obj);
 
 * bool param_5 = myCircularQueueIsEmpty(obj);
 
 * bool param_6 = myCircularQueueIsFull(obj);
 
 * myCircularQueueFree(obj);
*/


insert image description here


Summarize

The above is what I want to talk about today. This article introduces [Leedcode] the necessary interview questions for stacks and queues (the fourth issue).
If my blog is helpful to you, remember to support it three times. Thank you for your support!
insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/129280151