栈和队列的应用

栈 队列作为特殊的线性表,应用相当广泛。一般有递归、括号匹配、中缀表达式转后缀表达式、后缀表达式求值、以及树的层次遍历等几个应用方面。以下给出了几个例子供读者自行研读。

1、递归

以下给出斐波那切数列计算的例子供参考。

/** \brief 栈在递归中的调用
 *
 * \param
 * \param
 * \return
 *
 */
#include <iostream>
#include <stack>
using namespace std;
int Fib(int n)
{
    /** \brief output shows the sequence that is preorder process.
     *
     * \param
     * \param
     * \return
     *
     */

    cout<<"Fib"<<n<<" is called!"<<endl;
    if(n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return Fib(n-1)+Fib(n-2);
}

int main()
{
    cout << "------------Hello world!-------------" << endl;
    cout<<Fib(5);
    return 0;
}

2、括号匹配

典型的栈的应用,可以判断表达式是否合法。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stack>
using namespace std;
bool khpp(char str[],int n)
{
    int i=0;
    stack<char> s;
    while(i<n)
    {
        switch(str[i])
        {
        case '(':
            s.push(str[i]);
            break;
        case '{':
            s.push(str[i]);
            break;
        case '[':
            s.push(str[i]);
            break;
        case ')':
            if(!s.empty() && s.top()=='(')
            {
                s.pop();
            }
            else
            {
                i=100000;
            }
            break;
        case ']':
            if(!s.empty() && s.top()=='[')
            {
                s.pop();
            }
            else
            {
                i=100000;
            }
            break;
        case '}':
            if(!s.empty() && s.top()=='{')
            {
                s.pop();
            }
            else
            {
                i=100000;
            }
            break;
        }
        i++;
    }
    //cout<<"i="<<i<<endl;
    if(s.empty() && i==n)
    {
        return true;
    }
    else return false;
}
int main()
{
    char str[20];
    cout << "------------Hello world!-------------" << endl;
    cin>>str;
    if(khpp(str,strlen(str)))
        cout<<"OK!"<<endl;
    else cout<<"NG!"<<endl;
    return 0;
}

3、中缀表达式转后缀表达式

实际运算符太多,这里只是以+-*/%()作为例子。后缀表达式求值也是如此。

#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
void printStack(stack<char> s)
{
    while(!s.empty())
    {
        cout<<s.top()<<" ";
        s.pop();
    }
    cout<<endl;
}
void strzh(char str[],int n,char zhstr[])
{
    int i=0,j=0;
    stack<char> s;
    for(i=0;i<n;i++)
    {
        if(str[i]>=48 && str[i]<=57)
        {
            zhstr[j]=str[i];
            j++;
        }
        else
        {
            switch(str[i])
            {
            case '(':
                {
                    s.push(str[i]);
                    break;
                }
            case ')':
                {
                    while(s.top()!='(')
                    {
                        zhstr[j]=s.top();
                        s.pop();
                        j++;
                    }
                    s.pop();
                    break;
                }
            case '+':
                {
                    while(!s.empty() && (s.top()=='+' || s.top()=='-' || s.top()=='*' || s.top()=='/'))
                    {
                        zhstr[j]=s.top();
                        s.pop();
                        j++;
                    }
                    s.push(str[i]);
                    break;
                }
            case '-':
                {
                    while(!s.empty() && (s.top()=='+' || s.top()=='+' || s.top()=='-' || s.top()=='*' || s.top()=='/'))
                    {
                        zhstr[j]=s.top();
                        s.pop();
                        j++;
                    }
                    s.push(str[i]);
                    break;
                }
            case '*':
                {
                    while(!s.empty() && (s.top()=='*' || s.top()=='/'))
                    {
                        zhstr[j]=s.top();
                        s.pop();
                        j++;
                    }
                    s.push(str[i]);
                    break;
                }
            case '/':
                {
                    while(!s.empty() && (s.top()=='*' || s.top()=='/'))
                    {
                        zhstr[j]=s.top();
                        s.pop();
                        j++;
                    }
                    s.push(str[i]);
                    break;
                }
            }
        }
        //printStack(s);
    }
    while(!s.empty())
    {
        zhstr[j]=s.top();
        s.pop();
        j++;
    }
}
int main()
{
    char str[20];
    char zhstr[20]={0};
    cin>>str;
    strzh(str,strlen(str),zhstr);
    cout<<zhstr<<endl;
    return 0;
}

4、后缀表达式求值

#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int solve(char str[],int n)
{
    stack<int> s;
    int i=0;
    while(i<n)
    {
        if(48<=str[i] && str[i]<=57)
        {
            s.push(str[i]-48);
        }
        else
        {
            int top1=s.top();
            s.pop();
            int top2=s.top();
            s.pop();
            switch(str[i])
            {
            case '+':
                s.push(top2+top1);
                break;
            case '-':
                s.push(top2-top1);
                break;
            case '*':
                s.push(top2*top1);
                break;
            case '/':
                s.push(top2/top1);
                break;
            case '%':
                s.push(top2%top1);
                break;
            }
        }
        cout<<"i="<<i<<" "<<"top="<<s.top()<<endl;
        i++;
    }
    return s.top();
}

int main()
{
    char str[30];
    cout << "--------------------Hello world!---------------" << endl;
    cin>>str;
    cout<<solve(str,strlen(str));
    return 0;
}

5、层次遍历

层次遍历可以结合本人前边二叉树的存储以及遍历方法的几篇文章仔细研读。

/** \brief 队列在二叉树层次遍历中的应用
 *
 * \param
 * \param
 * \return
 *
 */
#include <iostream>
#include <queue>
#include "ThreadTree.h"
using namespace std;
void visit1(ThreadTree p)
{
    cout<<p->data<<" ";
}
void LayotOrder(ThreadTree T)
{
    queue<ThreadTree> que;
    ThreadTree p;
    que.push(T);
    while(!que.empty())
    {
        p=que.front();
        que.pop();
        visit1(p);
        if(p->lchild!=NULL)
        {
            que.push(p->lchild);
        }
        if(p->rchild!=NULL)
        {
            que.push(p->rchild);
        }
    }
}
int main()
{
    cout << "---------------Hello world!--------------" << endl;
    ThreadTree T;
    T=Init_ThreadTree();
    LayotOrder(T);
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值