栈、队列&leetcode225&155&232
来源:互联网 发布:微云直链解析php源码 编辑:程序博客网 时间:2024/06/06 15:36
- c++中栈的简单应用
- 定义
#include < stack >
template < class T, class Container = deque< T > > class stack;
stack< int > sta; //栈中均为int;
用数组实现一个栈~~
class stack { private: int arr[MAX]; int top; public: stack() { inItStack(); } //初始化栈 void inItStack() { top=-1; } //入栈 void push(int a) { top++; if(top < MAX) { arr[top]=a; } else { cout<<"STACK FULL!!"<<top; } } //出栈 void pop() { if(empty()) { cout<<"STACK IS EMPTY "; } else { int data=arr[top]; arr[top]=NULL; top--; } } int top(){ if(empty()){ cout<<"stack is empty"; } else return arr[top]; } //是否为空 bool empty() { if(top == -1) return true; else return false; } };
- 应用
sta.empty() 如果栈为空返回true,否则返回false;
sta.size() 返回int,栈内元素的大小;
sta.pop() 返回void;删除栈顶元素;
sta.push() 返回void;向栈内压入一个成员;
sta.top() 返回int;返回栈顶元素; - c++中队列的简单应用
- 定义: #include< queue >
queue< int > que;
用链表实现队列~~
#include<iostream>using namespace std;template <typename T>struct Node{ Node(T &d){ data=d; next=NULL; } T data; Node *next;};template <typename T>class LinkQueue{ private: int length; Node<T> *front; Node<T> *rear; public: LinkQueue(T &n){ Node <T> *p=new Node<T>(n); length=0; front=rear=p; } bool Queuelength() { cout<<"当前队列长度:"<<length<<endl; return true; } bool IsEmpty(){ return length==0; } void EnQueue(T n) { Node<T> *p=new Node<T>(n); rear->next=p; rear=p; length++; } bool DelQueue(){ if(front==rear) return false; Node<T> *p=front->next; front->next=p->next; if(front->next==NULL) rear=front; delete p; length--; return true; } void Tranverse() { Node<T> *p=front->next; cout<<"遍历队列:"<<endl; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; }};
- 应用
back()返回最后一个元素,顶部
empty()如果队列空则返回真
front()返回第一个元素,底部
pop()删除第一个元素,底部
push()在末尾加入一个元素,顶部
size()返回队列中元素的个数
1、Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); –> Returns -3.
minStack.pop();
minStack.top(); –> Returns 0.
minStack.getMin(); –> Returns -2.
class MinStack {private: stack<int> a; stack<int> a_min;public: void push(int x) { a.push(x); if(a_min.empty()||((!a_min.empty())&&x<=a_min.top())){ a_min.push(x); } } void pop() { if(!a.empty()){ if(a.top()==a_min.top()){ a_min.pop(); } a.pop(); } } int top() { if(!a.empty()) return a.top(); } int getMin() { if(!a_min.empty()) return a_min.top(); }};/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
2、Implement Stack using Queues
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
class Stack {private: queue<int> que;public: // Push element x onto stack. void push(int x) { que.push(x); for(int i=0;i<que.size()-1;i++){ que.push(que.front()); que.pop(); } } // Removes the element on top of the stack. void pop() { que.pop(); } // Get the top element. int top() { return que.front(); } // Return whether the stack is empty. bool empty() { return que.empty(); }};
3、Implement Queue using Stacks
Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class Queue { stack<int> in,out;public: // Push element x to the back of queue. void push(int x) { in.push(x); } // Removes the element from in front of queue. void pop(void) { peek(); out.pop(); } // Get the front element. int peek(void) { if(out.empty()){ while(!in.empty()){ out.push(in.top()); in.pop(); } } return out.top(); } // Return whether the queue is empty. bool empty(void) { return in.empty()&&out.empty(); }};
- 栈、队列&leetcode225&155&232
- Leetcode225: Candy
- LeetCode225:Implement Stack using Queues
- leetcode225 Implement Stack using Queues
- LeetCode225. Implement Stack using Queues
- Leetcode225. Implement Stack using Queues
- leetcode225.Implement Stack using Queues
- LeetCode225 Implemet Stack using Queues Java题解
- LeetCode225——Implement Stack using Queues
- LeetCode225—Implement Stack using Queues
- 栈和队列--队列
- 栈、队列、优先级队列
- 栈与队列->队列
- 【栈和队列】队列
- 栈、队列、优先队列
- 栈,队列,优先队列
- 栈 队列
- 栈、队列
- c++中的指针和引用
- 九度 oj 题目1080:进制转换
- 错误提示:Syntax error on token "function", delete this token
- msys2, vim, ctags,codeblocks, cmake
- 问题五十一:怎么用ray tracing画tear drop
- 栈、队列&leetcode225&155&232
- 学习笔记-对抗生成网络
- 利用python进行性能测试(下)
- Swift3.0-继承、构造、类扩展
- Json数据填充视图数据的一些想法
- Ubuntu16.04LTS安装及submile text 3基本配置
- listview滑动源码分析(二)
- 大数据架构详解从数据获取到深度学习读书笔记
- easyui笔记