【数据结构刷题集】栈和队列

😽PREFACE
🎁欢迎各位→点赞👍 + 收藏⭐ + 评论📝
📢系列专栏:数据结构刷题集
🔊本专栏涉及到题目是数据结构专栏的补充与应用,只更新相关题目,旨在帮助提高代码熟练度
💪种一棵树最好是十年前其次是现在

 括号匹配问题 

题目链接:力扣

//手搓栈
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
void stackInit(ST* ps);//初始化
void stackDestroy(ST* ps);//销毁
void stackPush(ST* ps,STDataType x);//入栈
void stackPop(ST* ps);//出栈
bool stackEmpty(ST* ps);//判空
STDataType stackTop(ST* ps);//取栈顶元素
int stackSize(ST* ps);//获取栈的长度

void stackInit(ST* ps)//初始化
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void stackDestroy(ST* ps)//销毁
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

void stackPush(ST* ps,STDataType x)//入栈
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void stackPop(ST* ps)//出栈
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

bool stackEmpty(ST* ps)//判空
{
	assert(ps);
	return ps->top == 0;
}

STDataType stackTop(ST* ps)//取栈顶元素
{
	assert(ps);
	assert(!stackEmpty(ps));
	return ps->a[ps->top - 1];
}

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);
        }
        else
        {
            if(stackEmpty(&st))//如果本身为空,那么直接返回false
            {
                return false;
            }
            char top=stackTop(&st);
            stackPop(&st);
            if((*s==')'&&top!='(')||(*s==']'&&top!='[')||(*s=='}'&&top!='{'))
            {
                return false;
            }
        }
        ++s;
    }
    bool ret=stackEmpty(&st);
    stackDestroy(&st);
    return ret;
}

 用队列实现栈

题目链接:力扣

//创建队列结构
typedef int QDataType;
//创建队列结点
typedef struct QueueNode
{
	struct QueueNode* next;//记录下一个结点
	QDataType data;//存储数据
}QNode;
//保存队头和队尾
typedef struct Queue
{
	QNode* head;//头指针
	QNode* tail;//尾指针
}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);//获取队尾元素
int QueueSize(Queue* pq);//元素个数
bool QueueEmpty(Queue* pq);//判空

//初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}

//销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur != NULL)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

//判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

//入队列
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	//创建一个新结点保存数据
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	//暴力检测newnode,因为malloc的都要检测
	assert(newnode);
	newnode->next = NULL;
	newnode->data = x;
	//如果一开始没有数据,为空的情况
	if (pq->tail == NULL)
	{
		assert(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 && pq->tail);//tail和head均不能为空
	//特殊:当删到head=tail位置时,tail会变成野指针
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	//一般情况
	else
	{
		//保存head的下一个结点
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

//获取队头元素
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 QueueSize(Queue* pq)
{
	assert(pq);
	int n = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		++n;
		cur = cur->next;
	}
	return n;
}
// *******************************************************************/


typedef struct {
    Queue q1;//队列1
    Queue q2;//队列2
} MyStack;


MyStack* myStackCreate() {
    MyStack* pst=(MyStack*)malloc(sizeof(MyStack));//申请一个MyStack类型的栈
    assert(pst);
    QueueInit(&pst->q1);//初始化队列1
    QueueInit(&pst->q2);//初始化队列2
    return pst;
}

void myStackPush(MyStack* obj, int x) {
    assert(obj);
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1,x);//如果q1不为空,就往q1插入数据
    }
    else
    {
        QueuePush(&obj->q2,x);//这里直接push
    }
}

int myStackPop(MyStack* obj) {
    assert(obj);
    Queue* emptyQ=&obj->q1;//默认q1为空
    Queue* nonEmtpyQ=&obj->q2;//默认q2不为空
    if(!QueueEmpty(&obj->q1))
    {
        emptyQ=&obj->q2;//若假设错误,则q2为空
        nonEmtpyQ=&obj->q1;//此时q1就为空       
    }
    while(QueueSize(nonEmtpyQ)>1)
    {
        QueuePush(emptyQ,QueueFront(nonEmtpyQ));//把非空的导入空的队列,直到剩下最后一个
        QueuePop(nonEmtpyQ);//此时把非空的队头数据删掉,便于后面导入数据
    }
    int top=QueueFront(nonEmtpyQ);//记录此时的栈顶数据
    QueuePop(nonEmtpyQ);//删除栈顶数据,置空队列
    return top;
}

int myStackTop(MyStack* obj) {
    assert(obj);
    if(!QueueEmpty(&obj->q1))
    {
        return QueueBack(&obj->q1);//如果q1不为空,返回
    }
    else
    {
        return QueueBack(&obj->q2);
    }
}

bool myStackEmpty(MyStack* obj) {
    assert(obj);
    //两个队列均为空,则为空
    return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}

void myStackFree(MyStack* obj) {
    assert(obj);
    QueueDestroy(&obj->q1);//释放q1
    QueueDestroy(&obj->q2);//释放q2
    free(obj);
}

 用栈实现队列

题目链接:力扣

 

//创建栈的结构
typedef int STDataType;
typedef struct stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
void stackInit(ST* ps);//初始化
void stackDestroy(ST* ps);//销毁
void stackPush(ST* ps, STDataType x);//入栈
void stackPop(ST* ps);//出栈
STDataType stackTop(ST* ps);//取栈顶数据
int stackSize(ST* ps);//栈的大小
bool stackEmpty(ST* ps);//判断是否为空

void stackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;//指向初始值的下一个
	ps->capacity = 0;
}

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

void stackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)*newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void stackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

bool stackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

STDataType stackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

int stackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

/******************************************************/


typedef struct {
    ST pushST;//插入数据的栈
    ST popST;//删除数据的栈
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));//申请队列类型
    assert(obj);
    stackInit(&obj->pushST);//初始化pushST
    stackInit(&obj->popST);//初始化popST
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    assert(obj);
    stackPush(&obj->pushST,x);//无论有没有数据,都要插入
}

int myQueuePop(MyQueue* obj) {
    assert(obj);
    if(stackEmpty(&obj->popST))//如果popST数据为空,要从pushST里导入数据才能删除
    {
        while(!stackEmpty(&obj->pushST))//pushST数据不为空,就一直向popST里面导入数据
        {
            stackPush(&obj->popST,stackTop(&obj->pushST));//把pushST栈顶数据导入到popST里
            stackPop(&obj->pushST);//删除popST栈顶元素,实现队列先进先出
            
        }
    }
    int front=stackTop(&obj->popST);//记录popST栈顶元素
    stackPop(&obj->popST);//删除popST栈顶元素,实现队列先进先出
    return front;//返回栈顶数据
}

int myQueuePeek(MyQueue* obj) {
    assert(obj);
    //如果popST数据为空,要从pushST里导入数据才能取到队头数据
    if(stackEmpty(&obj->popST))
    {
        while(!stackEmpty(&obj->pushST))//pushST数据不为空,就一直向popST里导入数据
        {
            stackPush(&obj->popST,stackTop(&obj->pushST));//把pushST栈顶数据导入到popST里
            stackPop(&obj->pushST);//导完后把pushST栈顶元素删掉,便于后面继续导
        }
    }
    return stackTop(&obj->popST);//直接返回栈顶元素
}

bool myQueueEmpty(MyQueue* obj) {
    return stackEmpty(&obj->pushST)&&stackEmpty(&obj->popST);
}

void myQueueFree(MyQueue* obj) {
    assert(obj);
    stackDestroy(&obj->pushST);
    stackDestroy(&obj->popST);
    free(obj);
}

 设计循环队列

题目链接:力扣

typedef struct {
    int* a; //用数组模拟环形队列
    int front;//队头
    int tail; //队尾
    int k; //表示存的数据长度为k
} MyCircularQueue;
 
bool myCircularQueueIsFull(MyCircularQueue* obj); //前置声明
bool myCircularQueueIsEmpty(MyCircularQueue* obj);//前置声明
 
MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));//创建环形链表结构
    assert(obj);
    obj->a = (int*)malloc(sizeof(int) * (k + 1));//多开一个空间,便于后续区分空或满
    obj->front = obj->tail = 0;
    obj->k = k; //队列存储有效数据长度为k
    return obj;
}
 
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
    {
        return false; //队列已满,不能插入数据
    }
    obj->a[obj->tail] = value; //赋值
    if (obj->tail == obj->k)
    {
        obj->tail = 0; //当tail走到尾端
    }
    else
    {
        obj->tail++;
    }
    return true;
}
 
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return false; //队列为空,不能删除
    }
    if (obj->front == obj->k)
    {
        obj->front = 0; //当front走到尾端
    }
    else
    {
        obj->front++;
    }
    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; //队列为空,取不了
    }
    if (obj->tail == 0)
    {
        return obj->a[obj->k]; //tail为0,队尾在长度的最后一个位置
    }
    else
    {
        return obj->a[obj->tail - 1];
    }
}
 
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->front == obj->tail; //front==tail时为空
}
 
bool myCircularQueueIsFull(MyCircularQueue* obj) {
    if (obj->tail == obj->k && obj->front == 0)
    {
        return true; //当tail尾端,front在头端时也是满
    }
    else
    {
        return obj->tail + 1 == obj->front; //一般情况,当tail的下一个位置为front时为满
    }
}
 
void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);
}

 

  • 33
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 34
    评论
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值