力扣练习23_5_20

有效的括号

在这里插入图片描述

主要是栈的实现,理解了就简单了,这次需要注意的是开辟空间用int,一开始用char时一直运行不出来

typedef int STDataType;
typedef struct Stack
{
    STDataType* a;
    STDataType top;
    STDataType capacity;
}ST;

void StackInit(ST* pst)
{
    assert(pst);
    pst->a=NULL;
    pst->top=0;
    pst->capacity=0;
}

void StackPush(ST* pst,STDataType x)
{
    assert(pst);
    if(pst->top==pst->capacity)
    {
        STDataType newcapacity=pst->capacity==0?4:pst->capacity*2;
        STDataType* newnode=(STDataType*)realloc(pst->a,sizeof(STDataType)*newcapacity);
        if(newnode==NULL)
        {
            printf("realloc fail");
            return;
        }
        pst->a=newnode;
        pst->capacity=newcapacity;
    }
    
    pst->a[pst->top]=x;
    pst->top++;
}

bool StackEmpty(ST* pst)
{
     assert(pst);
     return pst->top==0;
}

void StackPop(ST* pst)
{
    assert(pst);
    assert(!StackEmpty(pst));
    pst->top--;
}

void StackDestory(ST* pst)
{
    assert(pst);
    free(pst->a);
    pst->a=NULL;
    pst->top=pst->capacity=0;
}

STDataType StackTop(ST* pst)
{
    assert(pst);
    assert(!StackEmpty(pst));
    return pst->a[pst->top-1];
}

bool isValid(char * s){
    ST st;
    StackInit(&st);
    while(*s)
    {
        if(*s=='('
        ||*s=='{'
        ||*s=='[')
        {
            StackPush(&st,*s);
        }
        else
        {
            if(StackEmpty(&st))
            {
                StackDestory(&st);
                return false;
            }
            STDataType top=StackTop(&st);
            StackPop(&st);
            if((*s==')'&&top!='(')||(*s==']'&&top!='[')||(*s=='}'&&top!='{'))
            {
                StackDestory(&st);
                return false;
            }
        }
        s++;
    }
    bool ret=StackEmpty(&st);
    StackDestory(&st);
    return ret;
}

用队列实现栈

在这里插入图片描述

用两个队列进行实现,非空队列的元素依次移到空队列,只剩最后一个元素时,打印该元素

typedef int QDataType;
typedef struct QListNode
{
    QDataType* next;
    QDataType data;
}QLNode;

typedef struct Queue
{
    QLNode* phead;
    QLNode* ptail;
    int size;
}Queue;

void QueueInit(Queue* pq)
{
    assert(pq);
    pq->phead=pq->ptail=NULL;
    pq->size=0;
}

void QueueDestory(Queue* pq)
{
    assert(pq);
    QLNode* cur=pq->phead;
    while(cur)
    {
        QLNode* next=cur->next;
        free(cur);
        cur=next;
    }
    pq->phead=pq->ptail=NULL;
    pq->size=0;
}

bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->size==0;
}

void QueuePush(Queue* pq,QDataType x)
{
    assert(pq);
    QLNode* newnode=(QLNode*)malloc(sizeof(QLNode));
    if(newnode==NULL)
    {
        printf("malloc fail");
        return;
    }
    newnode->next=NULL;
    newnode->data=x;

    if(pq->ptail==NULL)
    {
        assert(pq->phead==NULL);
        pq->phead=pq->ptail=newnode;
    }
    else
    {
        pq->ptail->next=newnode;
        pq->ptail=newnode;
    }
    pq->size++;
}

void QueuePop(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    if(pq->phead->next==NULL)
    {
        free(pq->phead);
        pq->phead=pq->ptail=NULL;
    }
    else
    {
        QLNode* next=pq->phead->next;
        free(pq->phead);
        pq->phead=next;
    }
    pq->size--;
}

QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->phead->data;
}

QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->ptail->data;
}

int QueueSize(Queue* pq)
{
    assert(pq);
    return pq->size;
}


typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack* obj=(MyStack*)malloc(sizeof(MyStack));
    if(obj==NULL)
    {
        printf("malloc fail");
        return NULL;
    }
    QueueInit(&obj->q1);
    QueueInit(&obj->q2);
    return obj;
}

void myStackPush(MyStack* obj, int x) {
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1,x);
    }
    else
    {
        QueuePush(&obj->q2,x);
    }
}

int myStackPop(MyStack* obj) {
    Queue* pqempty=&obj->q1;
    Queue* pqnonempty=&obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        pqempty=&obj->q2;
        pqnonempty=&obj->q1;
    }

    while(QueueSize(pqnonempty)>1)
    {
        QueuePush(pqempty,QueueFront(pqnonempty));
        QueuePop(pqnonempty);
    }
    int top=QueueFront(pqnonempty);
    QueuePop(pqnonempty);
    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);
}

用栈实现队列

用两个栈实现,一个为pushst,一个为popst,将pushst数据依次放入popst中,依次输出,即为队列输出顺序
typedef int STDataType;
typedef struct Stack
{
    STDataType* a;
    STDataType top;
    STDataType capacity;
}Stack;

void StackInit(Stack* pt)
{
    assert(pt);
    pt->a=NULL;
    pt->top=pt->capacity=0;
}

void StackDestory(Stack* pt)
{
    assert(pt);
    free(pt->a);
    pt->a=NULL;
    pt->top=pt->capacity=0;
}

void StackPush(Stack* pt,STDataType x)
{
    assert(pt);
    if(pt->top==pt->capacity)
    {
        STDataType newcapacity=pt->capacity==0?4:pt->capacity*2;
        STDataType* ret=(STDataType*)realloc(pt->a,sizeof(STDataType)*newcapacity);
        if(ret==NULL)
        {
            printf("realloc fail");
            return;
        }
        pt->capacity=newcapacity;
        pt->a=ret;
    }
    pt->a[pt->top]=x;
    pt->top++;
}

bool StackEmpty(Stack* pt)
{
    assert(pt);
    return pt->top==0;
}

void StackPop(Stack* pt)
{
    assert(pt);
    assert(!StackEmpty(pt));
    pt->top--;
}

STDataType StackTop(Stack* pt)
{
    assert(pt);
    assert(!StackEmpty(pt));
    return pt->a[pt->top-1];
}



int StackSize(Stack* pt)
{
    assert(pt);
    return pt->top;
}



typedef struct {
    Stack pushst;
    Stack popst;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
    if(obj==NULL)
    {
        printf("malloc fail");
        return NULL;
    }
    StackInit(&obj->pushst);
    StackInit(&obj->popst);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->pushst,x);
}

int myQueuePop(MyQueue* obj) {
    int temp=myQueuePeek(obj);
    StackPop(&obj->popst);
    return temp;
}

int myQueuePeek(MyQueue* obj) {
    if(StackEmpty(&obj->popst))
    {
        while(!StackEmpty(&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) {
    StackDestory(&obj->pushst);
    StackDestory(&obj->popst);
    free(obj);
}

设计循环队列

在这里插入图片描述

用数组实现队列,理解列表为空时:rear == front,满时:rear+1== front
当求队尾元素时,坐标为(rear-1+k+1)%(k+1)

typedef int QDataType;
typedef struct QNode
{
    struct QNode* next;
    QDataType data;
}QNode;

typedef struct Queue
{
    QNode* phead;
    QNode* ptail;
    int size;
}Queue;

void QueueInit(Queue* pq)
{
    assert(pq);
    pq->phead=NULL;
    pq->ptail=NULL;
    pq->size=0;
}

void QueueDestory(Queue* pq)
{
    assert(pq);
    QNode* cur=pq->phead;
    while(cur)
    {
        QNode* next=cur->next;
        free(cur);
        cur=next;
    }
    pq->phead=pq->ptail=NULL;
    pq->size=0;
}

bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->size==0;
}

void QueuePush(Queue* pq,QDataType x)
{
    assert(pq);
    QNode* newnode=(QNode*)malloc(sizeof(QNode));
    if(newnode==NULL)
    {
        printf("malloc fail");
        return;
    }
    newnode->next=NULL;
    newnode->data=x;

    if(pq->ptail==NULL)
    {
        assert(pq->phead==NULL);
        pq->ptail=pq->phead=newnode;
    }
    else
    {
        pq->ptail->next=newnode;
        pq->ptail=newnode;
    }
    pq->size++;
}

void QueuePop(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    if(pq->phead->next==NULL)
    {
        free(pq->phead);
        pq->phead=pq->ptail=NULL;
    }
    else
    {
        QNode* next=pq->phead->next;
        free(pq->phead);
        pq->phead=next;
    }
    pq->size--;
}

QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->phead->data;
}

QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->ptail->data;
}

int QueueSize(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->size;
}






typedef struct {
    int front;
    int rear;
    int k;
    int* a;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if(obj==NULL)
    {
        printf("malloc fail\n");
        return NULL;
    }
    obj->front=obj->rear=0;
    obj->k=k;
    obj->a=(int*)malloc(sizeof(int)*(k+1));

    return obj;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->front==obj->rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->rear+1)%(obj->k+1)==obj->front;
}

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;
    }
    return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->a[(obj->rear+obj->k)%(obj->k+1)];
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值