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