10-栈和队列题目

有效的括号-力扣

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);
//返回1是空,返回0是非空
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;
}
//返回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;
}

//返回1是空,返回0是非空
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);
}

/**
 * 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);
*/

用栈实现队列

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);
//返回1是空,返回0是非空
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;
}
//返回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);
}

/**
 * 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);
*/

循环队列

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);
*/
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值