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

你可能感兴趣的文章
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>