博客
关于我
栈和队列的经典例题(括号匹配,用栈实现队列,用队列实现栈,设计循环队列)
阅读量: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/

你可能感兴趣的文章
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS UC 2013-0-Prepare Tool
查看>>
msbuild发布web应用程序
查看>>