栈和队列的经典例题(括号匹配,用栈实现队列,用队列实现栈,设计循环队列)

这篇博客探讨了栈和队列在数据结构中的经典应用,包括括号的有效匹配、用队列实现栈、用栈实现队列以及设计特殊功能的栈(如常数时间内获取最小元素)和循环队列。详细介绍了每个问题的思路和实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈和队列的基础接口实现

链接:https://blog.youkuaiyun.com/qq_43763344/article/details/89386829

.============================================================
头文件:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
using namespace std;
#include <stack>
#include <queue>

一、括号匹配

给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。

class Solution
{
public:
	bool isValid(string s)
	{
		stack<char> stack_ch;//定义一个栈
		int size = s.size();//返回字符串的长度
		for (int i = 0; i < size; ++i)
		{
			char ch = s[i];
			switch (ch)
			{
			case'(':
			case'[':
			case'{':
				stack_ch.push(ch);//压入
				break;
			case')':
			case']':
			case'}':
			{
				if (stack_ch.empty())
				{
					return false;
				}
				char left = stack_ch.top();
				if (!((left == '('&& ch == ')') ||
					(left == '['&& ch == ']') ||
					(left == '{'&& ch == '}')))
				{
					return false;
				}
				stack_ch.pop();
				break;
			}
			default:
				break;
			}
		}
		if (!stack_ch.empty())
		{
			return false;
		}
		else
		{
			return true;
		}
	}
};

二、用队列实现栈

class MyStack {
public:
	queue<int>	q;//成员变量
	
	/** Initialize your data structure here. */
	MyStack() {
	}

	/** Push element x onto stack. */
	void push(int x) {
		q.push(x);
	}

	/** Removes the element on top of the stack and returns that element. */
	int pop() {
		int size = q.size() - 1;
		for (int i = 0; i < size; ++i)
		{
			int data = q.front();
			q.pop();
			q.push(data);
		}
		int d = q.front();
		q.pop();
		return d;
	}

	/** Get the top element. */
	int top() {
		int size = q.size() - 1;
		for (int i = 0; i < size; ++i)
		{
			int data = q.front();
			q.pop();
			q.push(data);
		}
		int d = q.front();
		q.pop();
		q.push(d);
		return d;
	}

	/** Returns whether the stack is empty. */
	bool empty() {
		return q.empty();
	}
};

三、用栈实现队列

class MyQueue {
public:
	stack<int> left;
	stack<int> right;
	/** Initialize your data structure here. */
	MyQueue() {

	}

	/** Push element x to the back of queue. */
	void push(int x) {
		right.push(x);
	}

	/** Removes the element from in front of queue and returns that element. */
	int pop() {
		if (left.empty())
		{
			int size = right.size();
			for (int i = 0; i < size; ++i)
			{
				int d = right.top();
				right.pop();
				left.push(d);
			}
		}
		int v = left.top();
		left.pop();
		return v;
	}

	/** Get the front element. */
	int peek() {
		if (left.empty())
		{
			int size = right.size();
			for (int i = 0; i < size; ++i)
			{
				int d = right.top();
				right.pop();
				left.push(d);
			}
		}
		int v = left.top();
		return v;
	}

	/** Returns whether the queue is empty. */
	bool empty() {
		return left.empty() && right.empty();
	}
};

四、设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

push(x) – 将元素 x 推入栈中。
pop() – 删除栈顶的元素。
top() – 获取栈顶元素。
getMin() – 检索栈中的最小元素

class MinStack {
public:
	/** initialize your data structure here. */
	stack<int>	normal;
	stack<int>  min;
	MinStack() {
	}

	void push(int x) {
		normal.push(x);
		if (min.empty() || x <= min.top())
		{
			min.push(x);
		}
		else
		{
			min.push(min.top());
		}
	}

	void pop() {
		normal.pop();
		min.pop();
	}

	int top() {
		return normal.top();
	}

	int getMin() {
		return min.top();
	}
};

五、设计一个循环队列

class MyCircularQueue {
public:
	/** Initialize your data structure here. Set the size of the queue to be k. */
	int* array;
	int size;
	int capacity;
	int front;//队首下标
	int rear;//队尾下标
	MyCircularQueue(int k) {
		array = new int[k];
		capacity = k;
		front = 0;
		rear = 0;
	}

	/** Insert an element into the circular queue. Return true if the operation is successful. */
	bool enQueue(int value) {
		if (size == capacity)
		{
			return false;
		}
		array[rear] == value;
		rear = (rear + 1) % capacity;
		size++;
		return true;
	}

	/** Delete an element from the circular queue. Return true if the operation is successful. */
	bool deQueue() {
		if (size == 0)
		{
			return false;
		}
		front = (front + 1) % capacity;
		size--;
		return true;
	}

	/** Get the front item from the queue. */
	int Front() {
		if (size == 0)
		{
			return -1;
		}
		return array[front];
	}

	/** Get the last item from the queue. */
	int Rear() {
		if (size == 0)
		{
			return -1;
		}
		int index = (rear + capacity - 1) % capacity;
		return array[index];
	}

	/** Checks whether the circular queue is empty or not. */
	bool isEmpty() {
		return size == 0;
	}

	/** Checks whether the circular queue is full or not. */
	bool isFull() {
		return size == capacity;
	}
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿的温柔香

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

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

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

打赏作者

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

抵扣说明:

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

余额充值