Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct stack{
- char data;
- struct stack *left, *right, *bottom;
- } *top = NULL;
- void push(struct stack *newNode){
- if(top) newNode->bottom = top;
- top = newNode;
- }
- struct stack* pop(){
- struct stack *target = top;
- top = top->bottom;
- target->bottom = NULL;
- return target;
- }
- void display(struct stack *help){
- if(help){
- display(help->left);
- display(help->right);
- printf("%c", help->data);
- }
- }
- int main(){
- char postfix[20];
- printf("Enter the expression(oprands can be A-Z or 0-9): ");
- gets(postfix);
- for (int i = 0; postfix[i] != '\0'; i++){
- struct stack *newNode = (struct stack*) malloc(sizeof(struct stack));
- newNode->data = postfix[i]; newNode->bottom = NULL;
- if((newNode->data >= 'A' && newNode->data <= 'Z')||(newNode->data >= '0' && newNode->data <= '9')){
- newNode->left = newNode->right = NULL;
- push(newNode);
- }
- else{
- if(!top->bottom) goto end;
- newNode->right = pop(); newNode->left = pop();
- push(newNode);
- }
- }
- if(!top->bottom) display(top);
- else end: printf("\nNot a valid postfix expression.");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement