栈、队列&leetcode225&155&232

  • 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();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值