有效的括号-力扣
20. 有效的括号 - 力扣(LeetCode)
#include<stdio.h>
#include<assert.h>
#include <stdlib.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top;
int _capacity;
}Stack;
void StackInit(Stack* pst);
void StackDestory(Stack* pst);
void StackPush(Stack* pst,STDataType x);
void StackPop(Stack*pst);
int StackSize(Stack*pst);
int StackEmpty(Stack*pst);
STDataType StackTop(Stack* pst);
void StackInit(Stack* pst) {
assert(pst);
pst->_a = malloc(sizeof(STDataType)*4);
pst->_top=0;
pst->_capacity=4;
}
void StackDestory(Stack* pst)
{
assert(pst);
free(pst->_a);
pst->_a = NULL;
pst->_top = pst->_capacity = 0;
}
void StackPush(Stack* pst, STDataType x)
{
assert(pst);
if (pst->_top == pst->_capacity)
{
pst->_capacity *= 2;
STDataType* tmp = realloc(pst->_a, sizeof(STDataType) * pst->_capacity);
if (tmp == NULL)
{
printf("内存不足\n");
exit(-1);
}
else
{
pst->_a = tmp;
}
}
pst->_a[pst->_top] = x;
pst->_top++;
}
void StackPop(Stack* pst)
{
assert(pst);
assert(pst->_top>0);
--pst->_top;
}
int StackSize(Stack* pst)
{
assert(pst);
return pst->_top == 0 ? 1 : 0;
}
int StackEmpty(Stack* pst)
{
assert(pst);
return pst->_top == 0 ? 1 : 0;
}
STDataType StackTop(Stack* pst)
{
assert(pst);
assert(pst->_top > 0);
return pst->_a[pst->_top - 1];
}
bool isValid(char* s) {
Stack st;
StackInit(&st);
while(*s!='\0')
{
if(*s=='('||*s=='['||*s=='{')
{
StackPush(&st,*s);
++s;
}
else
{
if(StackEmpty(&st))
return false;
char top=StackTop(&st);
if(*s==']'&&top!='[')
return false;
if(*s==')'&&top!='(')
return false;
if(*s=='}'&&top!='{')
return false;
StackPop(&st);
++s;
}
}
int ret =StackEmpty(&st);
StackDestory(&st);
return ret;
}
用队列实现栈
225. 用队列实现栈 - 力扣(LeetCode)
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* _next;
QDataType _data;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;
QueueNode* _tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq,QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
void QueueInit(Queue* pq)
{
assert(pq);
pq->_head = pq->_tail = NULL;
}
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_head;
while (cur)
{
QueueNode* next = cur->_next;
free(cur);
cur = next;
}
pq->_head = pq->_tail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
printf("内存不足");
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;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->_head);
QueueNode* next = pq->_head->_next;
free(pq->_head);
pq->_head = next;
if (pq->_head == NULL)
{
pq->_tail = NULL;
}
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->_head);
return pq->_head->_data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->_tail);
return pq->_tail->_data;
}
int QueueEmpty(Queue* pq)
{
return pq->_head == NULL ? 1 : 0;
}
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QueueNode* cur=pq->_head;
while (cur)
{
size++;
cur = cur->_next;
}
return size;
}
typedef struct {
Queue _q1;
Queue _q2;
} MyStack;
MyStack* myStackCreate() {
MyStack*st=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&st->_q1);
QueueInit(&st->_q2);
return st;
}
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->_q1))
{
QueuePush(&obj->_q1,x);
}
else
{
QueuePush(&obj->_q2,x);
}
}
int myStackPop(MyStack* obj)
{
Queue*empty=&obj->_q1;
Queue*nonempty=&obj->_q2;
if(QueueEmpty(&obj->_q2))
{
empty=&obj->_q2;
nonempty=&obj->_q1;
}
while(QueueSize(nonempty)>1)
{
QueuePush(empty,QueueFront(nonempty));
QueuePop(nonempty);
}
int top=QueueFront(nonempty);
QueuePop(nonempty);
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) {
QueueDestory(&obj->_q1);
QueueDestory(&obj->_q2);
free(obj);
}
用栈实现队列
232. 用栈实现队列 - 力扣(LeetCode)
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top;
int _capacity;
}Stack;
void StackInit(Stack* pst);
void StackDestory(Stack* pst);
void StackPush(Stack* pst,STDataType x);
void StackPop(Stack*pst);
int StackSize(Stack*pst);
int StackEmpty(Stack*pst);
STDataType StackTop(Stack* pst);
void StackInit(Stack* pst) {
assert(pst);
pst->_a = malloc(sizeof(STDataType)*4);
pst->_top=0;
pst->_capacity=4;
}
void StackDestory(Stack* pst)
{
assert(pst);
free(pst->_a);
pst->_a = NULL;
pst->_top = pst->_capacity = 0;
}
void StackPush(Stack* pst, STDataType x)
{
assert(pst);
if (pst->_top == pst->_capacity)
{
pst->_capacity *= 2;
STDataType* tmp = realloc(pst->_a, sizeof(STDataType) * pst->_capacity);
if (tmp == NULL)
{
printf("内存不足\n");
exit(-1);
}
else
{
pst->_a = tmp;
}
}
pst->_a[pst->_top] = x;
pst->_top++;
}
void StackPop(Stack* pst)
{
assert(pst);
assert(pst->_top>0);
--pst->_top;
}
int StackSize(Stack* pst)
{
assert(pst);
return pst->_top == 0 ? 1 : 0;
}
int StackEmpty(Stack* pst)
{
assert(pst);
return pst->_top == 0 ? 1 : 0;
}
STDataType StackTop(Stack* pst)
{
assert(pst);
assert(pst->_top > 0);
return pst->_a[pst->_top - 1];
}
typedef struct {
Stack _pushST;
Stack _popST;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue*q=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&q->_popST);
StackInit(&q->_pushST);
return q;
}
void myQueuePush(MyQueue* obj, int x) {
StackPush(&obj->_pushST,x);
}
int myQueuePop(MyQueue* obj) {
int front=myQueuePeek(obj);
StackPop(&obj->_popST);
return front;
}
int myQueuePeek(MyQueue* obj) {
if(!StackEmpty(&obj->_popST))
{
return StackTop(&obj->_popST);
}
else
{
while(!StackEmpty(&obj->_pushST))
{
StackPush(&obj->_popST,StackTop(&obj->_pushST));
StackPop(&obj->_pushST);
}
}
return StackTop(&obj->_popST);
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->_popST)&&StackEmpty(&obj->_pushST);
}
void myQueueFree(MyQueue* obj) {
StackDestory(&obj->_popST);
StackDestory(&obj->_pushST);
free(obj);
}
循环队列
622. 设计循环队列 - 力扣(LeetCode)
//有错误,但是不知道在哪
typedef struct {
int*_a;
int _front;
int _rear;
int _k;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue*q=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
q->_a=(int*)malloc(sizeof(int)*(k+1));
q->_front=0;
q->_rear=0;
q->_k=k;
return q;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
return false;
obj->_a[obj->_rear]=value;
obj->_rear++;
obj->_rear %= (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;
else
return obj->_a[obj->_front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
else
int tail = obj->_rear-1;
if(tail==-1)
tail=obj->_k;
return obj->_a[tail];
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return q->_front==q->_rear;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->_rear+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);
*/