逆波兰表达式-单链表形式栈
具体代码
#include<stdio.h>
#include<stdlib.h>
typedef struct Stack{
int data;
struct Stack *next;
}Stack;
void Init(Stack* *S){
(*S)=(Stack*)malloc(sizeof(Stack));
(*S)->next=NULL;
}
void Push(Stack *S,int e){
Stack *p=(Stack*)malloc(sizeof(Stack));
p->data=e;
p->next=NULL;
p->next=S->next;
S->next=p;
}
char Pop(Stack *S){
if(S->next==NULL){
return NULL;
}else{
Stack *p=S->next;
char e=p->data;
S->next=S->next->next;
free(p);
return e;
}
}
int main(){
char c;
Stack S;
char arr[]={'5','1','+','3','/'};
int i=0;
while(i<5){
c=arr[i++];
if(c>='0'&&c<='9'){
Push(&S,c-48);
}else{
int b=Pop(&S);
int a=Pop(&S);
int temp;
if(c=='+'){
temp=a+b;
}else if(c=='-'){
temp=a-b;
}else if(c=='*'){
temp=a*b;
}else if(c=='/'){
temp=a/b;
}
Push(&S,temp);
}
}
int sum=Pop(&S);
printf("结果是:%d",sum);
}