• [数据结构]栈和队列面试题解析


    作者 华丞臧.
    专栏数据结构
    各位读者老爷如果觉得博主写的不错,请诸位多多支持(点赞+收藏+关注)。如果有错误的地方,欢迎在评论区指出。
    推荐一款刷题网站 👉 LeetCode刷题网站



    一、有效括号

    题目描述

    LeetCode.20 有效的括号
    给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。

    有效字符串需满足:

    1. 左括号必须用相同类型的右括号闭合。
    2. 左括号必须以正确的顺序闭合。
    3. 每个右括号都有一个对应的相同类型的左括号。

    在这里插入图片描述

    解题思路

    我们知道在一个式子当中后出现的左括号要与先出现的右括号配对;那么对于左括号而言,就是先入后出符合栈的特性,所以我们用栈来存储左括号;当遇到右括号时,判断栈顶的括号和该右括号是否配对,如果配对成功则出栈栈顶元素,否则配对失败则return false,这样既保证了相同类型又保证了顺序闭合;当遍历完传入的字符串时,还需要检查栈中是否为空。

    代码实现

    typedef char STDataType;
    
    typedef struct Stack
    {
    	STDataType* data;
    	int top;
    	int capacity;
    }ST;
    
    
    //初始化栈
    void StackInit(ST* ps);
    
    //销毁栈
    void StackDestroy(ST* ps);
    
    //入栈
    void StackPush(ST* ps, STDataType x);
    
    //出栈
    void StackPop(ST* ps);
    
    //获取栈顶元素
    STDataType  StackTop(ST* ps);
    
    //获取栈中有效元素个数
    int StackSize(ST* ps);
    
    //检查栈是否为空,如果为空返回非0结果,如果不为空返回0
    bool StackEmpty(ST* ps);
    
    //初始化栈
    void StackInit(ST* ps)
    {
    	assert(ps);
    	ps->data = NULL;
    	ps->top = ps->capacity = 0;
    }
    
    //销毁栈
    void StackDestroy(ST* ps)
    {
    	assert(ps);
    
    	free(ps->data);
    	ps->data = NULL;
    	ps->capacity = ps->top = 0;
    }
    
    
    //入栈
    void StackPush(ST* ps, STDataType x)
    {
    	assert(ps);
    
    	if (ps->top == ps->capacity)
    	{
    		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    		STDataType* tmp = (STDataType*)realloc(ps->data, newCapacity * sizeof(STDataType));
    		if (tmp == NULL)
    		{
    			perror("realloc fail");
    			exit(-1);
    		}
    		ps->data = tmp;
    		ps->capacity = newCapacity;
    	}
    	ps->data[ps->top] = x;
    	ps->top++;
    }
    
    
    //出栈
    void StackPop(ST* ps)
    {
    	assert(ps);
    	assert(!StackEmpty(ps));
    
    	ps->top--;
    }
    
    
    //获取栈顶元素
    STDataType StackTop(ST* ps) 
    {
    	assert(ps);
    	assert(!StackEmpty(ps));
    
    	return ps->data[ps->top - 1];
    }
    
    
    //获取栈中有效元素个数
    int StackSize(ST* ps)
    {
    	assert(ps);
    
    	return ps->top;
    }
    
    
    //检查栈是否为空,如果为空返回非0结果,如果不为空返回0
    bool StackEmpty(ST* ps)
    {
    	assert(ps);
    
    	/*if (ps->top == 0)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}*/
    
    	return ps->top == 0;
    }
    
    bool isValid(char* s)
    {
        ST pst;
        StackInit(&pst);
        char* cur = s;
    
        while(*cur)
        {
            if(*cur == '(' || *cur == '{' || *cur == '[')
            {
                StackPush(&pst,*cur);
            }
            else
            {
                if(StackEmpty(&pst))
                {
                    StackDestroy(&pst);
                    return false;
                }
                char tmp = StackTop(&pst);
                StackPop(&pst);
                if((*cur == '}' && tmp != '{')
                || (*cur == ']' && tmp != '[')
                || (*cur == ')' && tmp != '('))
                {
                    StackDestroy(&pst);
                    return false;
                }
            }
        cur++;
        }
    
       bool flag = StackEmpty(&pst);
        StackDestroy(&pst);
        return flag;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154

    二、用队列实现栈

    题目描述

    LeetCode.225 用队列实现栈
    请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

    实现 MyStack 类:

    • void push(int x) 将元素 x 压入栈顶。
    • int pop() 移除并返回栈顶元素。
    • int top() 返回栈顶元素。
    • boolean empty() 如果栈是空的,返回 true ;否则,返回 false

    在这里插入图片描述

    解题思路

    队列是先入先出,而栈是先入后出。
    用两个队列实现栈,进行入栈操作时把数据放入不为空的队列(两个队列都为空则随意);出栈时,把不为空的队列中元素倒入另一个为空的队列,只留一个元素,然后再返回这个元素的值并且删除该元素。只有两个队列都为空时,栈才为空;销毁时,注意要先销毁队列再销毁用队列实现的栈。
    出队列
    在这里插入图片描述

    代码实现

    typedef int QDataType;
    
    typedef struct QListNode
    {
    	struct QListNode* next;
    	QDataType data;
    }QLNode;
    
    typedef struct Queue
    {
    	QLNode* head;
    	QLNode* tail;
    	int size;
    }QE;
    
    //初始化队列
    void QueueInit(QE* q);
    
    //队尾入队列
    void QueuePush(QE* q,QDataType x);
    
    //队头出队列
    void QueuePop(QE* q);
    
    //获取队列头部元素
    QDataType QueueFront(QE* q);
    
    //获取队列队尾元素
    QDataType QueueBack(QE* q);
    
    //获取队列有效元素个数
    int QueueSize(QE* q);
    
    //检查队列是否为空,如果为空返回非0数,如果不为空返回0
    bool QueueEmpty(QE* q);
    
    //销毁队列
    void QueueDestroy(QE* q);
    
    //申请一个结点
    QLNode* BuyQListNode(QDataType x);
    
    //初始化队列
    void QueueInit(QE* q)
    {
    	assert(q);
    
    	q->head = q->tail = NULL;
    	q->size = 0;
    }
    
    
    //申请一个结点
    QLNode* BuyQListNode(QDataType x)
    {
    	QLNode* newnode = (QLNode*)malloc(sizeof(QLNode));
    	if (newnode == NULL)
    	{
    		perror("malloc fail");
    		exit(-1);
    	}
    	newnode->data = x;
    	newnode->next = NULL;
    	return newnode;
    }
    
    //队尾入队列
    void QueuePush(QE* q, QDataType x)
    {
    	assert(q);
    
    	//申请一个结点
    	QLNode* newnode = BuyQListNode(x);
    
    	//分有数据和无数据
    	if (q->head == NULL && q->tail == NULL)
    	{
    		q->head = newnode;
    		q->tail = newnode;
    	}
    	else
    	{
    		q->tail->next = newnode;
    		q->tail = newnode;
    	}
    	q->size++;
    }
    
    
    //队头出队列
    void QueuePop(QE* q)
    {
    	assert(q);
    	assert(!QueueEmpty(q));
    
    	if (q->head->next == NULL)
    	{
    		free(q->head);
    		q->head = q->tail = NULL;
    	}
    	else
    	{
    		QLNode* tmp = q->head->next;
    		free(q->head);
    		q->head = tmp;
    	}	
    	--q->size;
    }
    
    //获取队列头部元素
    QDataType QueueFront(QE* q)
    {
    	assert(q);
    	assert(!QueueEmpty(q));
    
    	return q->head->data;
    }
    
    
    //获取队列队尾元素
    QDataType QueueBack(QE* q)
    {
    	assert(q);
    	assert(!QueueEmpty(q));
    
    	return q->tail->data;
    }
    
    
    //获取队列有效元素个数
    int QueueSize(QE* q)
    {
    	assert(q);
    	return q->size;
    }
    
    //检查队列是否为空,如果为空返回非0数,如果不为空返回0
    bool QueueEmpty(QE* q)
    {
    	assert(q);
    	return q->head == NULL && q->tail == NULL;
    }
    
    
    //销毁队列
    void QueueDestroy(QE* q)
    {
    	assert(q);
    	while (q->head)
    	{
    		QLNode* tmp = q->head->next;
    		free(q->head);
    		q->head = tmp;
    	}
    	q->head = q->tail = NULL;
    	q->size = 0;
    }
    
    typedef struct {
        QE q1;
        QE q2;
    } MyStack;
    
    
    MyStack* myStackCreate() {
        MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
        if(obj == NULL)
        {
            perror("malloc fail");
            return NULL;
        }
        QueueInit(&obj->q1);
        QueueInit(&obj->q2);    
        return obj;
    }
    
    void myStackPush(MyStack* obj, int x) {
        assert(obj);
        if(QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2))
        {
            QueuePush(&obj->q1,x);
        }
        else if(!QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2))
        {
            QueuePush(&obj->q1,x);
        }
        else if(!QueueEmpty(&obj->q2) && QueueEmpty(&obj->q1))
        {
            QueuePush(&obj->q2,x);
        }
    }
    
    int myStackPop(MyStack* obj) {
        assert(obj);
        QE* empty = &obj->q1;
        QE* nonempty = &obj->q2;
    
        if(QueueEmpty(&obj->q2))
        {
            empty = &obj->q2;
            nonempty = &obj->q1;
        }
    
        int tmp = 0;
    
        while(QueueSize(nonempty) > 1)
        {
            QueuePush(empty,QueueFront(nonempty));
            QueuePop(nonempty);
        }
        tmp = QueueFront(nonempty);
        QueuePop(nonempty);
        
        return tmp;
    }
    
    int myStackTop(MyStack* obj) 
    {
        assert(obj);
    
        if(!QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2))
        {
            return obj->q1.tail->data;
        }
        else if(!QueueEmpty(&obj->q2) && QueueEmpty(&obj->q1))
        {
            return obj->q2.tail->data;
        }
        return 0;
    }
    
    bool myStackEmpty(MyStack* obj) {
        assert(obj);
    
        return  QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
    }
    
    void myStackFree(MyStack* obj) {
        assert(obj);
        QueueDestroy(&obj->q1);
        QueueDestroy(&obj->q2);
        free(obj);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243

    三、用栈实现队列

    题目描述

    LeetCode.232 用栈实现队列
    请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

    实现 MyQueue 类:

    • void push(int x) 将元素 x 推到队列的末尾
    • int pop() 从队列的开头移除并返回元素
    • int peek() 返回队列开头的元素
    • boolean empty() 如果队列为空,返回 true ;否则,返回 false

    在这里插入图片描述

    解题思路

    用两个栈实现队列,那么我们把两个栈分为 入队列的栈 push 出队列的栈 pop;进行入队列操作时,把数据压入push。进行出队列操作时,如果pop为空,则把push中的数据压入pop中再进行出栈操作;如果push不为空,则直接对pop进行出栈操作。
    在这里插入图片描述

    代码实现

    typedef int STDataType;
    
    typedef struct Stack
    {
    	STDataType* data;
    	int top;
    	int capacity;
    }ST;
    
    
    //初始化栈
    void StackInit(ST* ps);
    
    //销毁栈
    void StackDestroy(ST* ps);
    
    //入栈
    void StackPush(ST* ps, STDataType x);
    
    //出栈
    void StackPop(ST* ps);
    
    //获取栈顶元素
    STDataType  StackTop(ST* ps);
    
    //获取栈中有效元素个数
    int StackSize(ST* ps);
    
    //检查栈是否为空,如果为空返回非0结果,如果不为空返回0
    bool StackEmpty(ST* ps);
    
    //初始化栈
    void StackInit(ST* ps)
    {
    	assert(ps);
    	ps->data = NULL;
    	ps->top = ps->capacity = 0;
    }
    
    //销毁栈
    void StackDestroy(ST* ps)
    {
    	assert(ps);
    
    	free(ps->data);
    	ps->data = NULL;
    	ps->capacity = ps->top = 0;
    }
    
    
    //入栈
    void StackPush(ST* ps, STDataType x)
    {
    	assert(ps);
    
    	if (ps->top == ps->capacity)
    	{
    		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    		STDataType* tmp = (STDataType*)realloc(ps->data, newCapacity * sizeof(STDataType));
    		if (tmp == NULL)
    		{
    			perror("realloc fail");
    			exit(-1);
    		}
    		ps->data = tmp;
    		ps->capacity = newCapacity;
    	}
    	ps->data[ps->top] = x;
    	ps->top++;
    }
    
    
    //出栈
    void StackPop(ST* ps)
    {
    	assert(ps);
    	assert(!StackEmpty(ps));
    
    	--ps->top;
    }
    
    
    //获取栈顶元素
    STDataType StackTop(ST* ps) 
    {
    	assert(ps);
    	assert(!StackEmpty(ps));
    
    	return ps->data[ps->top - 1];
    }
    
    
    //获取栈中有效元素个数
    int StackSize(ST* ps)
    {
    	assert(ps);
    	
    	return ps->top;
    }
    
    
    //检查栈是否为空,如果为空返回非0结果,如果不为空返回0
    bool StackEmpty(ST* ps)
    {
    	assert(ps);
    
    	return ps->top == 0;
    }
    
    
    typedef struct {
        ST push;
        ST pop;
    } MyQueue;
    
    
    MyQueue* myQueueCreate() {
        MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
        StackInit(&obj->push);
        StackInit(&obj->pop);
        return obj;
    }
    
    void myQueuePush(MyQueue* obj, int x) {
        assert(obj);
        StackPush(&obj->push,x);
    }
    
    int myQueuePop(MyQueue* obj) {
        assert(obj);
    
        STDataType top = 0;
        if(!StackEmpty(&obj->pop))
        {
            top = StackTop(&obj->pop);
            StackPop(&obj->pop);
        }
        else
        {
            while(StackSize(&obj->push) > 0)
            {
                StackPush(&obj->pop,StackTop(&obj->push));
                StackPop(&obj->push);
            }
            top = StackTop(&obj->pop);
            StackPop(&obj->pop);
        }
        return top;
    }
    
    int myQueuePeek(MyQueue* obj) {
        assert(obj);
        if(!StackEmpty(&obj->pop))
        {
            return StackTop(&obj->pop);
        }
        else
        {
            while(StackSize(&obj->push) > 0)
            {
                StackPush(&obj->pop,StackTop(&obj->push));
                StackPop(&obj->push);
            }
            return StackTop(&obj->pop);
        }
    }
    
    bool myQueueEmpty(MyQueue* obj) {
        assert(obj);
    
        return StackEmpty(&obj->push) && StackEmpty(&obj->pop);
    }
    
    void myQueueFree(MyQueue* obj) {
        assert(obj);
    
        free(obj->push.data);
        free(obj->pop.data);
        free(obj);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181

    四、设计循环队列

    题目描述

    LeetCode.622 设计循环队列
    设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

    循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

    你的实现应该支持如下操作:

    • MyCircularQueue(k): 构造器,设置队列长度为k
    • Front: 从队首获取元素。如果队列为空,返回 -1
    • Rear: 获取队尾元素。如果队列为空,返回 -1
    • enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
    • deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
    • isEmpty(): 检查循环队列是否为空。
    • isFull(): 检查循环队列是否已满。

    解题思路

    在这里插入图片描述
    用数组实现循环队列,循环队列如上图所示,其长度是固定的,当循环队列存满时就不能存放新的数据了;给两个标记frontback,分别用来标记循环队列的第一个元素最后一个元素;当 back下一个元素等于 front 时,循环队列存满就不能再存放数据了;当 bakc 等于 front 时,循环队列为空。删除队列元素只需要对 front 减一即可。

    代码实现

    typedef struct {
        int* data;
        int front;
        int back;
        int size;
        int capacity;
    } MyCircularQueue;
    
    bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
        //return ((back - front + obj->capacity) % obj->capacity);
        if(obj->size)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    bool myCircularQueueIsFull(MyCircularQueue* obj) {
        return obj->size == obj->capacity ;
    }
    
    MyCircularQueue* myCircularQueueCreate(int k) {
        MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
        int* tmp = (int*)malloc(sizeof(int)*k);
        if(tmp == NULL)
        {
            perror("malloc fail");
            exit(-1);
        }
        obj->data = tmp;
        obj->front = obj->back = obj->size = 0;
        obj->capacity = k;
        return obj;
    }
    
    bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
        if(myCircularQueueIsFull(obj))
        {
            return false;
        }
        obj->data[obj->back] = value;
        obj->back++;
        if(obj->back == obj->capacity)
        {
            obj->back = 0;
        }
        obj->size++;
        return true;
    }
    
    bool myCircularQueueDeQueue(MyCircularQueue* obj) {
        if(myCircularQueueIsEmpty(obj))
        {
            return false;
        }
        obj->front++;
        if(obj->front == obj->capacity)
        {
            obj->front =  0;
        }
        obj->size--;
        return true;
    }
    
    int myCircularQueueFront(MyCircularQueue* obj) {
        if(myCircularQueueIsEmpty(obj))
        {
            return -1;
        }
        return obj->data[obj->front];
    }
    
    int myCircularQueueRear(MyCircularQueue* obj) {
        if(myCircularQueueIsEmpty(obj))
        {
            return -1;
        }
        return obj->data[(obj->back - 1 + obj->capacity) % obj->capacity];
    }
    
    
    
    void myCircularQueueFree(MyCircularQueue* obj) {
        free(obj->data);
        free(obj);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
  • 相关阅读:
    AM@连续函数相关概念和运算性质
    c#程序调用c++开发dll库
    1024 向所有的程序员们致以崇高的敬意和感谢
    为什么要使用双重校验锁来实现单例模式?
    【通关MySQL】Java的JDBC编程
    针对千万级人口城市的核酸检测系统的设计分享
    Redis高可用系列——List类型底层详解
    洛谷 P1967 [NOIP2013 提高组] 货车运输(最大生成树,最近公共祖先)
    Solidity 小白教程:5. 变量数据存储和作用域 storage_memory_calldata
    Kotlin协程:flowOn与线程切换
  • 原文地址:https://blog.csdn.net/qq_59456417/article/details/127880587