博客
关于我
栈和队列的经典例题(括号匹配,用栈实现队列,用队列实现栈,设计循环队列)
阅读量:631 次
发布时间:2019-03-14

本文共 4436 字,大约阅读时间需要 14 分钟。

栈和队列的基础接口实现

一、括号匹配

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

#include 
#include
using namespace std;class Solution {public: bool isValid(string s) { stack
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; } } } return stack_ch.empty(); }};

二、用队列实现栈

#include 
class MyStack {public: queue
q; MyStack() { } void push(int x) { q.push(x); } int pop() { if (q.empty()) { return -1; // 可按实际需求处理空栈情况 } 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; } int top() { if (q.empty()) { return -1; } 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; } bool empty() { return q.empty(); }};

三、用栈实现队列

#include 
class MyQueue {public: stack
left; stack
right; MyQueue() { } void push(int x) { right.push(x); } 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); } } if (left.empty()) { return -1; // 可按实际需求处理空队列情况 } int v = left.top(); left.pop(); return v; } 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); } } if (left.empty()) { return -1; // 可按实际需求处理空队列情况 } return left.top(); } bool empty() { return left.empty() && right.empty(); }};

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

#include 
class MinStack {public: stack
normal; stack
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(); }};

五、设计一个循环队列

#include 
class MyCircularQueue {public: int* array; int size; int capacity; int front; int rear; MyCircularQueue(int k) { array = new int[k]; capacity = k; front = 0; rear = 0; size = 0; } bool enQueue(int value) { if (size == capacity) { return false; } array[rear] = value; rear = (rear + 1) % capacity; size++; return true; } bool deQueue() { if (size == 0) { return false; } front = (front + 1) % capacity; size--; return true; } int Front() { if (size == 0) { return -1; } return array[front]; } int Rear() { if (size == 0) { return -1; } int index = (rear + capacity - 1) % capacity; return array[index]; } bool isEmpty() { return size == 0; } bool isFull() { return size == capacity; }};

转载地址:http://cchoz.baihongyu.com/

你可能感兴趣的文章
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>