栈队列经典OJ题(详细过程)

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

  第一题判断有效的括号,这道题我们会用到栈的知识,栈是后进先出的,可以根据这个来解这道题,先看一下题目和示例。

1.1整体思路 

我们通过示例可以看出括号匹配就返回true,不匹配就返回false,这里的思路就是使用栈,如果是左括号,我们就入栈,如果是右括号,我们就取栈顶元素和右括号进行比较,如果匹配就比较下一个,不匹配直接返回false,我们可以结合图例来理解一下!

 以上是一些常见的例子,但是也有一些特殊的情况

1.']'只有一个右括号时,这个时候我们就需要加一些条件,如果栈顶没有元素,证明没有左括号入栈,那么也就是说没有左括号和右括号匹配,那就直接返回false。

2.'['只有一个括号时,那么也就是说只有左括号入栈,然后就直接返回了,这里我们可以用判断一下栈是否为空,用bool来返回,如果为空,返回true,如果不是空,证明栈里还有左括号,那也说明左括号和右扩号的数量不匹配,这样就可以有效解决了

1.2代码分析 

因为还没有学C++,所以这里的栈都是自己模拟实现的

1.3整体代码

typedef char STDatatype;

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


//函数声明
//函数的初始化和销毁
void STInit(ST* pst);
void STDestroy(ST* pst);

//进栈和出栈
void STPush(ST* pst, STDatatype x);
void STPop(ST* pst);

//栈顶
STDatatype STTop(ST* pst);

//栈是否为空
bool STEmpty(ST* pst);

//个数
int STSize(ST* pst);
//函数的初始化和销毁
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}
//进栈和出栈
void STPush(ST* pst, STDatatype x)
{
	assert(pst);
	if (pst->capacity == pst->top)
	{
		//增加容量
		int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
		STDatatype* tmp = (STDatatype*)realloc(pst->a,sizeof(STDatatype)*newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			exit(1);
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}
//栈顶
STDatatype STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	return pst->a[pst->top - 1];
}
//栈是否为空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
	//等于就是空,不等于0就是非空
}
//个数
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}
bool isValid(char* s) {
    ST st;
   STInit(&st);
   while(*s != 0)
   {
    //左括号入栈
    if(*s == '(' || *s == '[' || *s == '{')
    {
        STPush(&st,*s);
    }
    else
    {
         if(STEmpty(&st))
         {
             return false;
         }
        char top = STTop(&st);
        STPop(&st);
        if(top == '(' && *s != ')'
        || top == '{' && *s != '}'
        || top == '[' && *s != ']')
        {
            return false;
        }
        
    }
    ++s;
   }
  bool ret = STEmpty(&st);
  return ret;
}

 2. 用队列实现栈

这道题是让我们用两个队列来实现一个栈,我们知道队列是先进先出的,而栈是后进先出的,那我们要如何实现呢?

2.1整体思路:

这道题的思路就是我们把要插入的数据放到那个非空的队列中,要保持一个队列是空,一个队列是非空,注意不可以两个队列都有数据,这样我们会把自己绕进去!要删除数据的时候,将非空队列的最后一个数据留下来,将其他的插入到空的队列当中,然后再将留下的那个数据删除即可。画图来理解一下

 

2.2 代码分析

2.3整体代码 

//创建节点

typedef int QDatatype;
typedef struct Queuenode
{
	QDatatype val;
	struct Queuenode* next;
}Qnode;

//定义一个结构体存放指针
typedef struct Queue
{
	Qnode* phead;
	Qnode* ptail;
	int size;
}Queue;

//队列的初始化和销毁
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);

//队列的插入和删除
void QueuePush(Queue* pq, QDatatype x);
void QueuePop(Queue* pq);

//取队列头和尾的值
QDatatype QueueFront(Queue* pq);
QDatatype QueueBank(Queue* pq);

//是否为空
bool QueueEmpty(Queue* pq);

//size
int QueueSize(Queue* pq);

//队列的初始化和销毁
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;
}

//队列的插入和删除
void QueuePush(Queue* pq, QDatatype x)
{
	assert(pq);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL)
	{
		perror("malloc:");
		exit(1);
	}
	//申请成功
	newnode->next = NULL;
	newnode->val = x;
	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->size != 0);
	//一个节点
	if (pq->phead == pq->ptail)
	{
		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(pq->phead);
	return pq->phead->val;
}
QDatatype QueueBank(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->val;
}

//是否为空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
}

//size
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* empty = &(obj->q1);
    Queue* noempty = &(obj->q2);
    if(!QueueEmpty(&(obj->q1)))
    {
        empty = &(obj->q2);
        noempty = &(obj->q1);
    }
    while(QueueSize(noempty) > 1)
    {
        QueuePush(empty,QueueFront(noempty));
        QueuePop(noempty);
    }
    int top = QueueFront(noempty);
    QueuePop(noempty);
    return top;
}

int myStackTop(MyStack* obj) {
    if(!QueueEmpty(&(obj->q1)))
    {
        return QueueBank(&(obj->q1));
    }
    else
    {
        return QueueBank(&(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);
*/

 3. 用栈实现队列

这道题是用两个栈实现队列,栈是后进先出,队列是先进先出,这个就比较好实现了

3.1整体思路

我们可以建一个pushst和一个popst,一个用来插入数据,一个用来删除数据,我们可以想一下将数据插入到pushst,栈是后进先出,那么我们将pushst的数据倒到popst,他们的顺序就发生了改变,这个时候popst栈顶的数据也就是队列第一个要出的数据,我们可以画图来看一下。这里还有一个小技巧,当popst不为空的时候,取栈顶就是队列要出的数据,这里不用来回倒,插入数据直接插入pustst即可,当popst为空时把pushst的数据都倒入到popst栈中

3.2代码分析 

3.3整体代码 



typedef int STDatatype;

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

//函数声明
//函数的初始化和销毁
void STInit(ST* pst);
void STDestroy(ST* pst);

//进栈和出栈
void STPush(ST* pst, STDatatype x);
void STPop(ST* pst);

//栈顶
STDatatype STTop(ST* pst);

//栈是否为空
bool STEmpty(ST* pst);

//个数
int STSize(ST* pst);


//函数的初始化和销毁
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}


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

//进栈和出栈
void STPush(ST* pst, STDatatype x)
{
	assert(pst);
	if (pst->capacity == pst->top)
	{
		//增加容量
		int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
		STDatatype* tmp = (STDatatype*)realloc(pst->a,sizeof(STDatatype)*newcapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			exit(1);
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

//栈顶
STDatatype STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	return pst->a[pst->top - 1];
}

//栈是否为空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
	//等于就是空,不等于0就是非空
}

//个数
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

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


MyQueue* myQueueCreate() {
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    STInit(&obj->pushst);
    STInit(&obj->popst);
    return obj;
}

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


int myQueuePeek(MyQueue* obj) {
    if(STEmpty(&obj->popst))
    {
        while(!STEmpty(&obj->pushst))
        {
            STPush(&obj->popst,STTop(&obj->pushst));
            STPop(&obj->pushst);
        }
    }
    return STTop(&obj->popst);
}

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

bool myQueueEmpty(MyQueue* obj) {
    return STEmpty(&obj->pushst) && STEmpty(&obj->popst);
}

void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->popst);
    STDestroy(&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);
*/

4.​​​​​​​设计循环队列

这道题是让我们设计一个循环队列,队列是先进先出的,是一个循环,可以把他比较一个图书馆的桌子,有四个座位,也就是说最多可以坐四个人,如果超过四个人后面的人就要等待在坐的四个人有人坐,再坐进去,拿这个例子我们就可以很好的理解这个题目了!

4.1整体思路

这道题有多种解法,可以用链表来做,也可以用数组来做,在这里我还是用数组来做,链表比较麻烦,不论是在申请空间,还是找尾节点的前驱节点,大家感兴趣可以拿链表做一下,这里就拿数组做了,拿数组做也有出现一些小问题,如果我们只申请k个空间,那么数组是空还是满我们就分不清了,这里可以画图来看一下!

这里有两种解法,一个是定义一个计数器,还有一个是多开一个空间,就是开k+1个空间,这样我们就可以分清是空还是非空了,还有一个点就是如何让他循环起来,就是使用%,也就是每次到尾%回到下标为0的元素,这里我就采用多开一个空间的方法了,也就是%(k+1)这里画图可以帮助大家理解一下 。

这里将把每一个函数都拿出来分析!

4.2代码分析

4.2.1MyCircularQueue(k)

4.2.2isEmpty()

4.2.3isfull 

 4.2.4enQueue(value)和deQueue()

4.2.5Front和Rear

4.3整体代码




typedef struct {
    int* a;
    int head;
    int tail;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    obj->a = (int*)malloc(sizeof(int)*(k+1));
    obj->head = 0;
    obj->tail = 0;
    obj->k = k;
    return obj;  
}


bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->head == obj->tail;
}

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

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if(myCircularQueueIsFull(obj))
    {
        return false;
    }
    obj->a[obj->tail] = value;
    obj->tail++;
    obj->tail %= (obj->k+1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    obj->head++;
    obj->head %= (obj->k+1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    return obj->a[obj->head];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
   return obj->a[(obj->tail -1 + obj->k +1) % (obj->k+1)];
   //return obj->tail == 0 ? obj->a[obj->k]:obj->a[obj->tail-1];
}



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

  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值