栈的应用实例

栈应用


逆序输出:进制转换

//LIFO 
#include <cstdio>
#include <cstdlib>
const char dight[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
struct stack{
	char data;
	stack* next;
}; 
typedef struct stack stack;
stack* build(){
	stack* list = (stack*)malloc(sizeof(stack));
	list->next=NULL;
	return list;
}

stack* push(stack* list ,char a){
	stack* p = (stack*)malloc(sizeof(stack));
	p->data=a;
	p->next=list;
	list=p;
	return list;
}

bool empty(stack* list){
	if(list->next==NULL){
		return false;
	}
	return true;
}

stack* pop(stack* list){
	if(empty(list)){
		stack* p=list;
		list=list->next;
		free(p);
	}
	return list;
}

int top(stack* list){
	if(empty(list)){
		return list->data;
	}	
} 
int size(stack* list){
	int cnt=0;
	stack* p = list;
	while(p->next){
		cnt++;
		p=p->next;
	}
	return cnt;
}

//进制转换 
stack* convert(long long n,int base){
	stack* list=build();
	while(n>0){
		list=push(list,dight[n%base]);
		n/=base;
	}
	return list;
}
int main(){
	int n,a;
	scanf("%d %d",&n,&a);//将一个数转换成a进制
	stack* list;
	list=build();
	list=convert(n,a);
	stack* p=list;
	while(p->next){
		printf("%c",p->data);
		p=p->next;
	}
	printf("\n栈的长度为:%d\n",size(list));
	printf("栈顶元素为:%c\n",top(list));
	return 0;
}

栈应用:括号匹配

在这里插入图片描述

#include<algorithm>
#include<stack>
#include<cstring>
#include<map>
#include<cstdio>
using namespace std;
stack<char> s;
map<char,char>mp;
bool paren(char exp[]){
	printf("%d\n",strlen(exp));
	for(int i=0;i<strlen(exp);i++){
		if(exp[i]=='('||exp[i]=='['||exp[i]=='{'){
			s.push(exp[i]);
		} else if((!s.empty())&&s.top()==mp[exp[i]]){
			s.pop();
		}else{
			return false;
		}
	}
	return s.empty();
}
int main(){
	char ch[105];
	mp[']']='[';
	mp[')']='(';
	mp['}']='{';
	printf("%c",mp['[']);
	scanf("%s",ch);
	if(paren(ch)==true){
		printf("匹配\n");
	}else{
		printf("不匹配"); 
	}
	return 0;
}

栈应用:中缀表达式求值A版本

#include<cstdio>
#include<stack>
#include<iostream>
#include<map>
using namespace std;
stack<int>opnd;
stack<char>optr;
map<char,int>mp;
int evaluate(string s) {
	while(!opnd.empty()) {
		opnd.pop();
	}
	while(!optr.empty()) {
		optr.pop();
	}
	int len=optr.size();
	opnd.push(0);
	mp['+']=1;
	mp['-']=1;
	mp['*']=2;
	mp['/']=2;
	for(int i=0; i<s.length(); i++) {
		if(s[i]>='0'&&s[i]<='9') {
			if(s[i-1]>='0'&&s[i-1]<='9') {
				int a=opnd.top();
				opnd.pop();
				opnd.push(a*10+(s[i]-'0'));
			} else {
				opnd.push(s[i]-'0');
			}
		} else {
			if(optr.empty()) {
				optr.push(s[i]);
			} else {
				if(mp[s[i]]<=mp[optr.top()]) {
					while(!optr.empty()) {
						int a=opnd.top();
						opnd.pop();
						int b=opnd.top();
						opnd.pop();
						char ch=optr.top();
						if(ch=='+') {
							b+=a;
						} else if(ch=='-') {
							b-=a;
						} else if(ch=='*') {
							b*=a;
						} else {
							b/=a;
						}
						opnd.push(b);
						optr.pop();
					}
					optr.push(s[i]);
				} else {
					optr.push(s[i]);
				}
			}
		}
	}
	while(!optr.empty()) {
		int a=opnd.top();
		opnd.pop();
		int b=opnd.top();
		opnd.pop();
		char ch=optr.top();
		if(ch=='+') {
			b+=a;
		} else if(ch=='-') {
			b-=a;
		} else if(ch=='*') {
			b*=a;
		} else {
			b/=a;
		}
		opnd.push(b);
		optr.pop();
	}
	return opnd.top();
}
int main() {
	string s;
	cin>>s;
	int a=evaluate(s);
	cout<<a<<endl;
	return 0;
}
// 1+2*4-1+6/2

栈应用:中缀表达式求值B版本

#include<cstdio>
#include<stack>
#include<string>
#include<map>
#include<iostream>
using namespace std;
map<char,int>mp;
void init(){
	mp['#']=0;
	mp['(']=1;
	mp[')']=1;
	mp['+']=2;
	mp['-']=2;
	mp['*']=3;
	mp['/']=3;
	
}
//中缀表达式求结果 
int sum(string s){
	stack<int>  d;
	stack<char> c;
	c.push('#');
	int i=0;
	while(!c.empty()){
		if(s[i]>='0'&&s[i]<='9'){
			if(i!=0&&(s[i-1]>='0'&&s[i-1]<='9')){
				int a=d.top();
				d.pop();
				d.push(a*10+(s[i]-'0'));
			}else{
				d.push(s[i]-'0');
			}
			i++;
		}else{
			if(mp[s[i]]==mp[c.top()]&&((s[i]=='#'&&c.top()=='#')||(s[i]==')'&&c.top()=='('))){
				c.pop();
				i++;
			}else if(s[i]=='('||(c.top()=='(')||(mp[s[i]]>mp[c.top()])){
				c.push(s[i]);
				i++;
			}else{
				int a=d.top();
				d.pop();
				int b=d.top();
				d.pop();
				if(c.top()=='+'){
					d.push(b+a);
				}else if(c.top()=='-'){
					d.push(b-a);
				}else if(c.top()=='*'){
					d.push(b*a);
				}else{
					d.push(b/a);
				}
				c.pop();
			}
		}	
	}
	return d.top();
}
//1+(20-8*2)+2
int main(){
	init();
	string s;
	cin>>s;
	s+='#';
	int a=sum(s);
	cout<<a<<endl;
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值