栈和队列的练习题
文章目录
- 一、括号匹配问题
- 二、用队列实现栈
- 三、用栈实现队列
- 四、设计循环队列
一、括号匹配问题
思路:
该题是栈的典型应用,满足后进先出的规则(后入栈的前括号将优先与先出现的后括号相匹配),遍历字符串,遇到前括号直接入栈。遇到后括号,判断该后括号与栈顶的前括号是否匹配(若此时栈为空,则字符串无效),若不匹配则字符串无效;若匹配则删除栈顶元素,继续遍历字符串,直到字符串遍历完毕。当字符串遍历完后,检测栈是否为空,若为空,则字符串有效,若不为空,说明有前括号未匹配,字符串无效
typedef char STDataType;//栈中存储的元素类型typedef struct Stack
{STDataType* a;//栈int top;//栈顶int capacity;//容量,方便增容
}Stack;//初始化栈
void StackInit(Stack* pst)
{assert(pst);pst->a = (STDataType*)malloc(sizeof(STDataType)* 4);//初始化栈可存储4个元素pst->top = 0;//初始时栈中无元素,栈顶为0pst->capacity = 4;//容量为4
}//销毁栈
void StackDestroy(Stack* pst)
{assert(pst);free(pst->a);//释放栈pst->a = NULL;//及时置空pst->top = 0;//栈顶置0pst->capacity = 0;//容量置0
}//入栈
void StackPush(Stack* pst, STDataType x)
{assert(pst);if (pst->top == pst->capacity)//栈已满,需扩容{STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType)*pst->capacity * 2);if (tmp == NULL){printf("realloc fail\n");exit(-1);}pst->a = tmp;pst->capacity *= 2;//栈容量扩大为原来的两倍}pst->a[pst->top] = x;//栈顶位置存放元素xpst->top++;//栈顶上移
}//检测栈是否为空
bool StackEmpty(Stack* pst)
{assert(pst);return pst->top == 0;
}//出栈
void StackPop(Stack* pst)
{assert(pst);assert(!StackEmpty(pst));//检测栈是否为空pst->top--;//栈顶下移
}//获取栈顶元素
STDataType StackTop(Stack* pst)
{assert(pst);assert(!StackEmpty(pst));//检测栈是否为空return pst->a[pst->top - 1];//返回栈顶元素
}//获取栈中有效元素个数
int StackSize(Stack* pst)
{assert(pst);return pst->top;//top的值便是栈中有效元素的个数
}bool isValid(char* s) {Stack st;//创建一个栈StackInit(&st);//初始化栈char* cur=s;//cur用于遍历字符串while(*cur){//前括号统一入栈if(*cur=='('||*cur=='{'||*cur=='['){StackPush(&st,*cur);cur++;}else{//若遇到后括号,且栈为空,则字符串无效if(StackEmpty(&st)){StackDestroy(&st);return false;}char top=StackTop(&st);//获取栈顶元素//后括号与栈顶的前括号不匹配if((top == '('&&*cur != ')')||(top == '{'&&*cur != '}')||(top == '['&&*cur != ']')){StackDestroy(&st);return false;}//匹配else{StackPop(&st);cur++;}}}bool ret=StackEmpty(&st);//检测栈是否为空StackDestroy(&st);return ret;//栈为空返回true,栈不为空返回false
}
二、用队列实现栈
思路:
使用两个队列,始终保持一个队列为空
当我们需要进行压栈操作时,将数据压入不为空的队列中(若两个都为空,则随便压入一个队列),当需要进行出栈操作时,将不为空的队列中的数据导入空队列,仅留下一个数据,这时将这个数据返回并且删除即可
判断栈是否为空,即判断两个队列是否同时为空
typedef int QDataType;//队列中存储的元素类型typedef struct QListNode
{struct QListNode* next;//指针域QDataType data;//数据域
}QListNode;typedef struct Queue
{QListNode* head;//队头QListNode* tail;//队尾
}Queue;
//初始化队列
void QueueInit(Queue* pq)
{assert(pq);//起始时队列为空pq->head = NULL;pq->tail = NULL;
}//销毁队列
void QueueDestroy(Queue* pq)
{assert(pq);QListNode* cur = pq->head;//接收队头//遍历链表,逐个释放结点while (cur){QListNode* next = cur->next;free(cur);cur = next;}pq->head = NULL;//队头置空pq->tail = NULL;//队尾置空
}//队尾入队列
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QListNode* newnode = (QListNode*)malloc(sizeof(QListNode));//申请新结点if (newnode == NULL){printf("malloc fail\n");exit(-1);}newnode->data = x;//新结点赋值newnode->next = NULL;//新结点指针域置空if (pq->head == NULL)//队列中原本无结点{pq->head = pq->tail = newnode;//队头、队尾直接指向新结点}else//队列中原本有结点{pq->tail->next = newnode;//最后一个结点指向新结点pq->tail = newnode;//改变队尾指针指向}
}//检测队列是否为空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL;
}//队头出队列
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//检测队列是否为空if (pq->head->next == NULL)//队列中只有一个结点{free(pq->head);pq->head = NULL;pq->tail = NULL;}else//队列中有多个结点{QListNode* next = pq->head->next;free(pq->head);pq->head = next;//改变队头指针指向}
}//获取队列头部元素
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//检测队列是否为空return pq->head->data;//返回队头指针指向的数据
}//获取队列尾部元素
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//检测队列是否为空return pq->tail->data;//返回队尾指针指向的数据
}//获取队列中有效元素个数
int QueueSize(Queue* pq)
{assert(pq);QListNode* cur = pq->head;//接收队头int count = 0;//记录结点个数while (cur)//遍历队列{count++;cur = cur->next;}return count;//返回队列中的结点数
}typedef struct {Queue q1;//第一个队列Queue q2;//第二个队列
} MyStack;MyStack* myStackCreate() {MyStack* pst=(MyStack*)malloc(sizeof(MyStack));//申请一个Mystack类型的栈QueueInit(&pst->q1);//初始化第一个队列QueueInit(&pst->q2);//初始化第二个队列return pst;}void myStackPush(MyStack* obj, int x) {//数据压入非空的那个队列if(!QueueEmpty(&obj->q1)){QueuePush(&obj->q1,x);}else{QueuePush(&obj->q2,x);}}int myStackPop(MyStack* obj) {Queue* pEmpty=&obj->q1;//记录空队列Queue* pNoEmpty=&obj->q2;//记录非空队列//若假设不成立,则改变假设if(!QueueEmpty(&obj->q1)){pEmpty=&obj->q2;pNoEmpty=&obj->q1;}//将非空队列中的数据放入空队列中,只留下最后一个数据while(QueueSize(pNoEmpty)>1){QueuePush(pEmpty,QueueFront(pNoEmpty));QueuePop(pNoEmpty);}int front = QueueFront(pNoEmpty);//获取目标数据QueuePop(pNoEmpty);//删除目标数据return front;}int myStackTop(MyStack* obj) {//获取非空队列的队尾数据if(!QueueEmpty(&obj->q1)){return QueueBack(&obj->q1);}else{return QueueBack(&obj->q2);}}bool myStackEmpty(MyStack* obj) {//两个队列均为空,则MyStack为空return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);}void myStackFree(MyStack* obj) {QueueDestroy(&obj->q1);//释放第一个队列QueueDestroy(&obj->q2);//释放第二个队列free(obj);//释放MyStack}/*** 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);
*/
三、用栈实现队列
思路:
使用两个栈,第一个栈只用于数据的输入,第二个栈只用于数据的输出
当需要输出数据,但第二个栈为空时,先将第一个栈中的数据一个一个导入到第二个栈,然后第二个栈再输出数据即可
只有第二个栈为空时,才可以将第一个栈中的数据导入到第二个栈
typedef int STDataType;//栈中存储的元素类型typedef struct Stack
{STDataType* a;//栈int top;//栈顶int capacity;//容量,方便增容
}Stack;//初始化栈
void StackInit(Stack* pst)
{assert(pst);pst->a = (STDataType*)malloc(sizeof(STDataType)* 4);//初始化栈可存储4个元素pst->top = 0;//初始时栈中无元素,栈顶为0pst->capacity = 4;//容量为4
}//销毁栈
void StackDestroy(Stack* pst)
{assert(pst);free(pst->a);//释放栈pst->a = NULL;//及时置空pst->top = 0;//栈顶置0pst->capacity = 0;//容量置0
}//入栈
void StackPush(Stack* pst, STDataType x)
{assert(pst);if (pst->top == pst->capacity)//栈已满,需扩容{STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType)*pst->capacity * 2);if (tmp == NULL){printf("realloc fail\n");exit(-1);}pst->a = tmp;pst->capacity *= 2;//栈容量扩大为原来的两倍}pst->a[pst->top] = x;//栈顶位置存放元素xpst->top++;//栈顶上移
}//检测栈是否为空
bool StackEmpty(Stack* pst)
{assert(pst);return pst->top == 0;
}//出栈
void StackPop(Stack* pst)
{assert(pst);assert(!StackEmpty(pst));//检测栈是否为空pst->top--;//栈顶下移
}//获取栈顶元素
STDataType StackTop(Stack* pst)
{assert(pst);assert(!StackEmpty(pst));//检测栈是否为空return pst->a[pst->top - 1];//返回栈顶元素
}//获取栈中有效元素个数
int StackSize(Stack* pst)
{assert(pst);return pst->top;//top的值便是栈中有效元素的个数
}typedef struct {Stack pushST;//插入数据时用的栈Stack popST;//删除数据时用的栈
} MyQueue;MyQueue* myQueueCreate() {MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));//申请一个队列类型StackInit(&obj->pushST);//初始化pushSTStackInit(&obj->popST);//初始化popSTreturn obj;}void myQueuePush(MyQueue* obj, int x) {StackPush(&obj->pushST,x);//插入数据,向pushST插入
}int myQueuePeek(MyQueue* obj) {//popST为空时,需先将pushST中数据全部导入popSTif(StackEmpty(&obj->popST)){//将pushST数据全部导入popSTwhile(!StackEmpty(&obj->pushST)){StackPush(&obj->popST,StackTop(&obj->pushST));StackPop(&obj->pushST);}}return StackTop(&obj->popST);//返回popST栈顶的元素
}int myQueuePop(MyQueue* obj) {int top = myQueuePeek(obj);StackPop(&obj->popST);//删除数据,删除popST中栈顶的元素return top;
}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);
*/
四、设计循环队列
思路:
在环形队列中,队列为空时,队头队尾指向同一个位置。当队列不为空时,队头指向插入的第一个数据,队尾指向最后一个数据的下一个位置。当tail+1等于front时,说明环形队列已满
注意:
在链表和数组里面首要选择数组,因为链表取尾不好取
环形队列的队尾不能像常规队列中队尾一样指向最后一个数据,如果这样的话,我们将不能区别环形队列的状态是空还是满,因为此时队头和队尾都指向同一个位置。这就意味着,我们必须留出一个空间,这个空间不能存放数据,这样我们才能很好的区别环形队列的状态是空还是满。或者创建一个变量size记录
若用数组来实现这个环形队列,则上面两种状态对应于下面两种状态
可以看出,此时这个数组和环形完全扯不上关系,这其实很简单,我们只需注意判断两个地方:
1. 当指针指向整个数组的后方的时候,让该指针重新指向数组的第一个元素。
2. 当指针指向整个数组的前方的时候,让该指针直接指向数组最后一个有效元素的后面
typedef struct {int* a;//数组模拟环形队列int k;//队列可存储的有效数据总数int front;//队头int tail;//队尾的最后一个位置
} MyCircularQueue;MyCircularQueue* myCircularQueueCreate(int k) {MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));//申请一个环形队列obj->a=(int*)malloc(sizeof(int)*(k+1));//开辟队列空间obj->k=k;//设置队列可存储的有效数据个数//初始时,队头和队尾均为0obj->front=0;obj->tail=0;return obj;}bool myCircularQueueIsEmpty(MyCircularQueue* obj) {//当front和tail指向同一位置时,队列为空return obj->front==obj->tail;
}bool myCircularQueueIsFull(MyCircularQueue* obj) {//int tailNext = obj->tail+1;//if(tailNext == obj->k+1)//当指针指到队列末尾时,指针返回队列开头,使队列循环// {//tailNext = 0;// }//return tailNext == obj->front;//当tail+1指向的位置与front相同时,队列满 return (obj->tail + 1) % (obj->k + 1) == obj->front;//用取模运算简化
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {//队列已满,不能再插入数据if(myCircularQueueIsFull(obj)){return false;}//插入数据else{obj->a[obj->tail]=value;obj->tail++;//使队列循环if(obj->tail==obj->k+1){obj->tail=0;}return true;}
}bool myCircularQueueDeQueue(MyCircularQueue* obj) {//当队列为空时,无法再删除数据if(myCircularQueueIsEmpty(obj)){return false;}//删除数据else{obj->front++;//使队列循环if(obj->front==obj->k+1){obj->front=0;}return true;}
}int myCircularQueueFront(MyCircularQueue* obj) {//当队列为空时,无数据可返回if(myCircularQueueIsEmpty(obj)){return -1;}else{return obj->a[obj->front];//返回队头指向的数据}
}int myCircularQueueRear(MyCircularQueue* obj) {//当队列为空时,无数据返回if(myCircularQueueIsEmpty(obj)){return -1;}//返回tail-1指向的位置else{int tailPrev=obj->tail-1;//使队列循环if(tailPrev==-1){tailPrev=obj->k;}return obj->a[tailPrev];}
}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);
*/