栈和队列练习题
Posted 萌新的日常
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了栈和队列练习题相关的知识,希望对你有一定的参考价值。
(文章目录)
一、20. 有效的括号
typedef char datatype;
typedef struct stack
datatype* a;
int top;
int capacity;
ST;
void stackinit(ST* p);
void stackpush(ST* p,datatype x);
datatype stacktop(ST* p);
void stackpop(ST* p);
int stacksize(ST* p);
bool stackempty(ST* p);
void stackdestroy(ST* p);
void stackinit(ST* p)//栈的初始化
assert(p);
p->a = NULL;
p->top = 0;
p->capacity = 0;
void stackpush(ST* p, datatype x)//入栈
assert(p);
if (p->top == p->capacity)
int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
datatype* tmp = (datatype*)realloc(p->a, sizeof(datatype)*newcapacity);
if (tmp != NULL)
p->a = tmp;
p->capacity = newcapacity;
p->a[p->top] = x;
p->top++;
void stackpop(ST* p)//移除栈顶元素
assert(p);
assert(p->top > 0);
p->top--;
datatype stacktop(ST* p)//出栈
assert(p);
assert(p->top>0);
return p->a[p->top - 1];
bool stackempty(ST* p)//是否为空
return p->top == 0;
int stacksize(ST* p)//栈中元素个数
assert(p);
return p->top;
void stackdestroy(ST* p)//内存销毁
assert(p);
free(p->a);
p->a = NULL;
p->top = 0;
p->capacity = 0;
bool isValid(char * s)
ST p;
stackinit(&p);
while(*s)
if((*s==()||(*s==)||(*s==[))
stackpush(&p,*s);
s++;
else
if(stackempty(&p))//当只有]时,栈中没有数据存在 ,判断是否为空,
//为空则false
stackdestroy(&p);
return false;
datatype top=stacktop(&p);
stackpop(&p);
if((top==[)&&(*s!=])||(top==)&&(*s!=)||(top==()&&(*s!=)))
stackdestroy(&p);
return false;
else
s++;
bool ret=stackempty(&p);//当只有[进入判断栈是否为空 若不为空则false
stackdestroy(&p);
return ret;
二、225. 用队列实现栈
:
>请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
>注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
>示例:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
```c
typedef int datatype;
typedef struct queuenode
datatype data;
struct queuenode* next;
queuenode;
typedef struct queue
queuenode* head;
queuenode* tail;
queue;
void queueinit(queue* p);
void queuedestroy(queue* p);
void queuepush(queue* p, datatype x);
void queuepop(queue* p);
datatype queuefront(queue* p);
datatype queueback(queue* p);
int queuesize(queue* p);
bool queueempty(queue* p);
void queueinit(queue* p)//初始化队列
assert(p);
p->head = NULL;
p->tail = NULL;
void queuedestroy(queue* p)//内存销毁
assert(p);
queuenode* cur = p->head;
while (cur != NULL)
queuenode* next = cur->next;
free(cur);
cur = next;
p->head = NULL;
p->tail = NULL;
void queuepush(queue* p, datatype x)//入队列 (队尾入)
assert(p);
queuenode* newnode = (queuenode*)malloc(sizeof(queuenode));
newnode->data = x;
newnode->next = NULL;
if (p->tail == NULL)
p->tail = newnode;
p->head = newnode;
else
p->tail->next = newnode;
p->tail = newnode;
void queuepop(queue* p)//删除数据
assert(p);
assert(!queueempty(p));//断言队列是否为空
queuenode* next = p->head->next;
free(p->head);
p->head = next;
if (p->head == NULL)//当删除只剩下最后一个节点时 head与tail都指向,free(head) ,tail就变成了野指针
p->tail = NULL;
datatype queuefront(queue* p)//取队头数据
assert(p->head);
assert(!queueempty(p));
return p->head->data;
datatype queueback(queue* p)//取队尾数据
assert(p->head);
assert(!queueempty(p));
return p->tail->data;
int queuesize(queue* p)//队的数量
assert(p);
int sum = 0;
queuenode* cur = p->head;
while (cur != NULL)
sum++;
cur = cur->next;
return sum;
bool queueempty(queue* p)//判断队列是否为空
assert(p);
return p->head == NULL;
typedef struct
queue q1;
queue q2;
MyStack;
MyStack* myStackCreate() //初始化
MyStack*obj=(MyStack*)malloc(sizeof(MyStack));
queueinit(&obj->q1);
queueinit(&obj->q2);
return obj;
void myStackPush(MyStack* obj, int x) // 将元素 x 压入栈顶
if(!queueempty(&obj->q1))
queuepush(&obj->q1,x);
else
queuepush(&obj->q2,x);
int myStackPop(MyStack* obj) //移除并返回栈顶元素
queue*empty=&obj->q1;
queue*noempty=&obj->q2;
if(!queueempty(&obj->q1))
noempty=&obj->q1;
empty=&obj->q2;
while(queuesize(noempty)>1)
queuepush(empty,queuefront(noempty));
queuepop(noempty);
int top=queuefront(noempty);
queuepop(noempty);
return top;
int myStackTop(MyStack* obj) // 返回栈顶元素
if(!queueempty(&obj->q1))
return queueback(&obj->q1);
else
return queueback(&obj->q2);
bool myStackEmpty(MyStack* obj) //当两个队列都为空时
return queueempty(&obj->q1)&&queueempty(&obj->q2);
void myStackFree(MyStack* obj)
queuedestroy(&obj->q1);
queuedestroy(&obj->q2);
free(obj);
/**
* Your MyStack struct will be instantiated and called as such:
* MyStack* obj = myStackCreate();
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/
三、232. 用栈实现队列
typedef int datatype;
typedef struct stack
datatype* a;
int top;
int capacity;
ST;
void stackinit(ST* p);
void stackpush(ST* p,datatype x);
datatype stacktop(ST* p);
void stackpop(ST* p);
int stacksize(ST* p);
bool stackempty(ST* p);
void stackdestroy(ST* p);
void stackinit(ST* p)//栈的初始化
assert(p);
p->a = NULL;
p->top = 0;
p->capacity = 0;
void stackpush(ST* p, datatype x)//入栈
assert(p);
if (p->top == p->capacity)
int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
datatype* tmp = (datatype*)realloc(p->a, sizeof(datatype)*newcapacity);
if (tmp != NULL)
p->a = tmp;
p->capacity = newcapacity;
p->a[p->top] = x;
p->top++;
void stackpop(ST* p)//移除栈顶元素
assert(p);
assert(p->top > 0);
p->top--;
datatype stacktop(ST* p)//出栈
assert(p);
assert(p->top>0);
return p->a[p->top - 1];
bool stackempty(ST* p)//是否为空
return p->top == 0;
int stacksize(ST* p)//栈中元素个数
assert(p);
return p->top;
void stackdestroy(ST* p)//内存销毁
assert(p);
free(p->a);
p->a = NULL;
p->top = 0;
p->capacity = 0;
typedef struct
ST pushST;
ST popST;
MyQueue;
MyQueue* myQueueCreate()
MyQueue*obj=(MyQueue*)malloc(sizeof(MyQueue));
stackinit( &obj->pushST);
stackinit(&obj->popST);
return obj;
void myQueuePush(MyQueue* obj, int x)
stackpush(&obj->pushST,x);//将数据都传入pushST中
int myQueuePop(MyQueue* obj) //判断popST是否为空 若为空则将pushST的数据传入popST中,
//若不为空则直接返回队列开头
if(stackempty(&obj->popST))
while(stacksize(&obj->pushST))
stackpush(&obj->popST,stacktop(&obj->pushST));
stackpop(&obj->pushST);
int front=stacktop(&obj->popST);
stackpop(&obj->popST);
return front;
int myQueuePeek(MyQueue* obj) //返回队列开头的元素
if(stackempty(&obj->popST))
while(stacksize(&obj->pushST))
stackpush(&obj->popST,stacktop(&obj->pushST));
stackpop(&obj->pushST);
return stacktop(&obj->popST);
bool myQueueEmpty(MyQueue* obj) //两个栈都为空才为空
return stackempty(&obj->pushST)&&stackempty(&obj->popST);
void myQueueFree(MyQueue* obj)
stackdestroy(&obj->pushST);
stackdestroy(&obj->popST);
free(obj);
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/
四、622. 设计循环队列
typedef struct
int* a;
int front;
int tail;
int k;
MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k)
MyCircularQueue*obj=( MyCircularQueue*)malloc(sizeof( MyCircularQueue));
obj->a=(int*)malloc(sizeof(int)*(k+1));
obj->front =0;
obj->tail=0;
obj->k=k;
return obj;
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);
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;
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))
return -1;
if(obj->tail==0)
return obj->a[obj->k];
else
return obj->a[obj->tail-1];
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);
*/
以上是关于栈和队列练习题的主要内容,如果未能解决你的问题,请参考以下文章