💗 💗 博客:小怡同学
💗 💗 个人简介:编程小萌新
💗 💗 如果博客对大家有用的话,请点赞关注再收藏 🌞
力扣习题
- 括号匹配问题。
- 用队列实现栈。
- 用栈实现队列。
- 设计循环队列。
- 有效的括号
//用栈来实现
//左括号进栈 右括号出栈并销毁如果不匹配则return
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
typedef char SDatetype;
typedef struct Stack
{
SDatetype* a;
int top;
int capacity;
}Stack;
void StackInit(Stack* pq);
void StackDestory(Stack* pq);
void StackPush(Stack* pq, SDatetype x);
void StackPop(Stack* pq);
bool StackEmpty(Stack* pq);
SDatetype StackTop(Stack* pq);
int StackSize(Stack* pq);
bool StackEmpty(Stack* pq)
{
assert(pq);
return pq->top == 0;
}
void StackInit(Stack* pq)
{
assert(pq);
pq->a = NULL;
pq->capacity = 0;
pq->top = 0;
}
void StackDestory(Stack* pq)
{
assert(pq);
pq->capacity = 0;
pq->top = 0;
free(pq->a);
pq->a =NULL;
}
void StackPush(Stack* pq, SDatetype x)
{
assert(pq);
if (pq->top == pq->capacity)
{
int newcapacity = (pq->top == 0 ? 4 : (pq->capacity) * 2);
pq->a = (SDatetype*)realloc(pq->a,sizeof(SDatetype) * newcapacity);
if (pq->a == NULL)
{
perror("malloc fail");
exit(-1);
}
pq->capacity = newcapacity;
}
pq->a[pq->top++] = x;
}
void StackPop(Stack* pq)
{
assert(pq);
assert(!StackEmpty(pq));
pq->top--;
}
SDatetype StackTop(Stack* pq)
{
assert(pq);
assert(!StackEmpty(pq));
return pq->a[pq->top-1];
}
int StackSize(Stack* pq)
{
assert(pq);
return pq->top;
}
bool isValid(char * s){
Stack pq;
StackInit(&pq);
while(*s)
{
if(*s== '(' || *s == '[' || *s == '{')
{
StackPush(&pq, *s);//进栈
}
else
{
if(StackEmpty(&pq))//如果只有一个左括号 无法匹配则return
{
StackDestory(&pq);
return false;
}
char tmp = StackTop(&pq);
StackPop(&pq);
if( (tmp != '(' && *s == ')' )||
(tmp != '[' && *s == ']' )||
(tmp != '{' && *s == '}'))//如果有一对不匹配则return
{
StackDestory(&pq);//return之前需要销毁栈 以免内存泄漏
return false;
}
}
s++;
}
bool tmp = StackEmpty(&pq);//当栈里还有元素时则匹配失败
StackDestory(&pq);
return tmp;
}
225. 用队列实现栈(力扣)
//设置两个队列,入栈向有元素的队列加入, 出栈(因为栈上后进先出)所以需要把有元素的队列依次移入另一个队列 再删除
typedef int QDatetype;
typedef struct Qnode
{
int* next;
QDatetype x;
}Qnode;
typedef struct Queue
{
Qnode* head;
Qnode* tail;
}Queue;
typedef struct {
Queue Qtwo;
Queue Qone;
} MyStack;
void QueueInit(Queue* pq)
{
assert(pq);
pq->tail = pq->head = NULL;
}
void QueueDestory(Queue* pq)
{
assert(pq);
while (pq->head)
{
Qnode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
void QueuePush(Queue* pq, QDatetype q)
{
assert(pq);
Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
newnode->next = NULL;
newnode->x = q;
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 == pq->tail)
{
free(pq->head);
pq->head = pq->tail= NULL;
}
else
{
Qnode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
QDatetype QueueTop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->x;
}
int QueueSize(Queue* pq)
{
assert(pq);
Qnode* head = pq->head;
int size = 0;
while (head)
{
size++;
head = head->next;
}
return size;
}
QDatetype QueueTail(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->x;
}
//创造一个由队列创建的栈
MyStack* myStackCreate() {
MyStack* pq = (MyStack*)malloc(sizeof(MyStack));
//向堆申请空间不会随栈的销毁而销毁
QueueInit(&pq->Qtwo);
QueueInit(&pq->Qone);
return pq;
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->Qone) && QueueEmpty(&obj->Qtwo);
}
void myStackPush(MyStack* obj, int x) {
Queue* one = &obj-> Qone;
Queue* two = &obj-> Qtwo;
if(!QueueEmpty(one))
{
QueuePush(one, x);
}
else
{
QueuePush(two,x);
}
}
int myStackPop(MyStack* obj) {
Queue* noempty = &obj-> Qone;
Queue* empty = &obj-> Qtwo;
if(!QueueEmpty(empty) )
{
empty = &obj->Qone;
noempty = &obj->Qtwo;
}
while(QueueSize(noempty) >1)
{
QueuePush(empty, QueueTop(noempty));
QueuePop(noempty);
}
int x = QueueTop(noempty);
QueuePop(noempty);
return x;
}
int myStackTop(MyStack* obj) {
Queue* one = &obj-> Qone;
Queue* two = &obj-> Qtwo;
if(!QueueEmpty(one))
{
return QueueTail(one);
}
else
{
return QueueTail(two);
}
}
void myStackFree(MyStack* obj) {
QueueDestory(&obj->Qone);
QueueDestory(&obj->Qtwo);
free(obj);
}
myStackPop函数的功能实现图
232. 用栈实现队列(力扣)
//与用队列实现栈的思想不同, 这里的栈分别是push栈 和pop栈 (专门用来插入数据和删除数据)
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
typedef int SDatetype;
typedef struct Stack
{
int* a;
int top;
int capacity;
}Stack;
bool StackEmpty(Stack* pq)
{
return pq->top == 0;
}
int StackSize(Stack* pq)
{
return pq->top;
}
void StackInit(Stack* pq)
{
assert(pq);
pq->a = NULL;
pq->capacity = 0;
pq->top = 0;
}
void StackDestory(Stack* pq)
{
assert(pq);
pq->capacity = 0;
pq->top = 0;
free(pq->a);
}
void StackPush(Stack* pq, SDatetype x)
{
assert(pq);
if (pq->top == pq->capacity)
{
int newcapacity = (pq->top == 0 ? 4 : (pq->capacity) * 2);
pq->a = (SDatetype*)realloc(pq->a,sizeof(SDatetype) * newcapacity);
if (pq->a == NULL)
{
perror("malloc fail");
return;
}
pq->capacity = newcapacity;
}
pq->a[pq->top++] = x;
}
void StackPop(Stack* pq)
{
assert(pq);
assert(!StackEmpty(pq));
pq->a[pq->top--];
}
SDatetype StackTop(Stack* pq)
{
assert(pq);
assert(!StackEmpty(pq));
return pq->a[pq->top-1];
}
typedef struct {
Stack push;
Stack pop;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* pq =(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&pq->push);
StackInit(&pq->pop);
return pq;
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->push) && StackEmpty(&obj->pop);
}
void myQueuePush(MyQueue* obj, int x) {
StackPush(&obj->push, x);
}
//这个函数主要功能实现的是 出队的元素是谁
//有两种情况:
//1.pop栈中有元素,直接出队
//2.pop栈中无元素, 需要把push栈中移入到pop栈里
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&obj->pop))
{
while(!StackEmpty(&obj->push))
{
StackPush(&obj->pop ,StackTop(&obj->push));
StackPop(&obj->push);
}
}
return StackTop(&obj->pop);
}
int myQueuePop(MyQueue* obj) {
int front = myQueuePeek(obj);
StackPop(&obj->pop);
return front;
}
void myQueueFree(MyQueue* obj) {
StackDestory(&obj->push);
StackDestory(&obj->pop);
free(obj);
}
myQueuePop主要功能图
622. 设计循环队列
//主要是用顺序表存储数据
//不用链表实现是因为1.找尾不容易找到,2,链表是否存满不好判断
typedef struct {
int front;
int rear ;
int k;
int* a;
} MyCircularQueue;
//公开辟(k+1)个空间 判断是否判空
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* pq = ( MyCircularQueue*)malloc(sizeof( MyCircularQueue));
pq->front = pq->rear= 0;
pq->k = k;
pq->a = (int*)malloc(sizeof(int)*(k+1));
return pq;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->rear == obj->front ;
}
//rear+1 = front则链表满了
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->rear +1) % (obj->k+1) == (obj->front);
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if (!myCircularQueueIsFull(obj))
{
obj->a[obj->rear] = value;
obj->rear = (++obj->rear % (obj->k+1));//rear++之后下标不能大于k+1,所以%(k+1)
return true;
}
return false;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
else
{
++obj->front;
obj->front = obj->front%(obj->k+1);
return true;
}
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(!myCircularQueueIsEmpty(obj))
{
return obj->a[obj->front];
}
else
{
return -1;
}
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(!myCircularQueueIsEmpty(obj))
{
return obj->a[(obj->rear-1 + obj->k+1)%(obj->k+1)] ;
//rear--之后下标不能小于0
}
else
{
return -1;
}
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
obj->a = NULL;
free(obj);
obj = NULL;
}