C/C++刷题DAY7

目录

第一题

第二题

第三题


第一题

20. 有效的括号 - 力扣(LeetCode)

 思路分析

用栈的知识来做

左括号,入栈

右括号,出栈栈顶元素 进行匹配

参考代码


typedef char STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int capacity;
	int top;   // 初始为0,表示栈顶位置下一个位置下标
}ST;

void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDatatype x);
void StackPop(ST* ps);
STDatatype StackTop(ST* ps);
bool StackEmpty(ST* ps);
int StackSize(ST* ps);







void StackInit(ST* ps)
{
	assert(ps);

	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	ps->top = 0;
	ps->capacity = 4;
}

void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void StackPush(ST* ps, STDatatype x)
{
	assert(ps);

	// 扩容
	if (ps->top == ps->capacity)
	{
		STDatatype* tmp = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}

	//先放数据,top再++
	ps->a[ps->top] = x;
	ps->top++;
}

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->top--;
}

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}










bool isValid(char * s)
{
    ST st;
    StackInit(&st);
    while(*s)
    {
        if(*s == '[' || *s == '{' || *s == '(')
        {
            StackPush(&st,*s);
            s++;
        }
        else
        {
            if(StackEmpty(&st))
            {
                StackDestroy(&st);
                return false;
            }


            char top = StackTop(&st);
            StackPop(&st);
            //不匹配
            if((  *s==']'  && top!='[')
                ||(*s=='}' && top!='{')
                ||(*s==')' && top!='('))
                {
                    StackDestroy(&st);
                    return false;
                }
                else//继续
                {
                    s++;
                }
        }
    }

    bool ret = StackEmpty(&st);
    StackDestroy(&st);

    return ret;


}

第二题

225. 用队列实现栈 - 力扣(LeetCode)

PS:重点是考察队列和栈的性质,实现性质转换。

 思路分析:

 void myStackFree(MyStack* obj) 的释放方式图解

参考代码(C语言)


#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int QDataType;
//链式结构:表示队列
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QNode;
//队列的结构
typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;
//队列初始化
void QueueInit(Queue* pq);
//队列销毁
void QueueDestroy(Queue* pq);
//队尾入队列
void QueuePush(Queue* pq, QDataType x);
//队头出队列
void QueuePop(Queue* pq);
//获取队头元素
QDataType QueueFront(Queue* pq);
//获取队尾元素
QDataType QueueBack(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
//获取队列有效元素个数
int QueueSize(Queue* pq);



void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->head;
	while (cur)
	{
		QNode* del = cur;
		cur = cur->next;

		free(del);
		//del = NULL;
	}

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	newnode->data = x;
	newnode->next = NULL;

	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* del = pq->head;
		pq->head = pq->head->next;

		free(del);
	}

	pq->size--;
}

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;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL && pq->tail == NULL;
}



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


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)
{
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1,x);
    }
    else
    {
        QueuePush(&obj->q2,x);
    }
}

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

    //非空队列的前n-1个数据倒入到空队列
    while(QueueSize(nonemptyQ) > 1)
    {
        QueuePush(emptyQ,QueueFront(nonemptyQ));
        QueuePop(nonemptyQ);
    }

    int top = QueueFront(nonemptyQ);
    QueuePop(nonemptyQ);

    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);
}

第三题

232. 用栈实现队列 - 力扣(LeetCode)

 思路分析

参考代码


#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
 
typedef int STDatatype;
typedef struct Stack//栈
{
	STDatatype* a;
	int capacity;
	int top;   // 初始为0,表示栈顶位置下一个位置的下标
}ST;
 
//栈初始化
void StackInit(ST* ps);
//栈销毁
void StackDestroy(ST* ps);
//压栈,入栈
void StackPush(ST* ps, STDatatype x);
//出栈
void StackPop(ST* ps);
//取栈顶数据
STDatatype StackTop(ST* ps);
//判空
bool StackEmpty(ST* ps);
//栈中有效元素个数
int StackSize(ST* ps);



void StackInit(ST* ps)
{
	assert(ps);
 
	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
 
	ps->top = 0;
	ps->capacity = 4;
}
 
void StackDestroy(ST* ps)
{
	assert(ps);
 
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
 
void StackPush(ST* ps, STDatatype x)
{
	assert(ps);
 
	// 扩容
	if (ps->top == ps->capacity)
	{
		STDatatype* tmp = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
 
		ps->a = tmp;
		ps->capacity *= 2;
	}
 
	//先放数据,top再++
	ps->a[ps->top] = x;
	ps->top++;
}
 

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
 
	ps->top--;
}
 
STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
 
	return ps->a[ps->top - 1];
}
 
bool StackEmpty(ST* ps)
{
	assert(ps);
 
	return ps->top == 0;
}
 
int StackSize(ST* ps)
{
	assert(ps);
 
	return ps->top;
}


typedef struct 
{
    ST pushst;
    ST popst;

} MyQueue;

bool myQueueEmpty(MyQueue*obj);

MyQueue* myQueueCreate() 
{
    MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&pq->pushst);
    StackInit(&pq->popst);

    return pq;
}


void myQueuePush(MyQueue* obj, int x) 
{
    assert(obj);

    StackPush(&obj->pushst,x);
}

int myQueuePop(MyQueue* obj)
{
    assert(obj);
    assert(!myQueueEmpty(obj));

    int peek = myQueuePeek(obj);
    StackPop(&obj->popst);

    return peek;
}

int myQueuePeek(MyQueue* obj) 
{
    assert(obj);
    assert(!myQueueEmpty(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) 
{
    assert(obj);
    return StackEmpty(&obj->popst) &&StackEmpty(&obj->pushst);
}

void myQueueFree(MyQueue* obj) 
{
    assert(obj);

    StackDestroy(&obj->pushst);
    StackDestroy(&obj->popst);

    free(obj);
}

  • 17
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

待己以诚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值