#include #include #include #define null 0 typedef struct post { int data; struct post *next; }node; node *newnode, *temp,*head; void alloc(int); void main() { char a[20]; int i,v1,v2,res,k; head=null; clrscr(); printf("\nEnter the postfix expression using linked list:"); //gets(a); while((a[i]=getchar())!='\n') { if(isdigit(a[i])) { k=a[i]-48; if(k>0&&k<=9) { alloc(k); }} else { temp=head; v2=temp->data; head=head->next; free(temp); temp=head; v1=temp->data; head=head->next; free(temp); switch(a[i]) { case '+': res=v1+v2; break; case '-': res=v1-v2; break; case '*': res=v1*v2; break; case '/': res=v1/v2; break; default:printf("\nEnter an valid postfix expression"); } alloc(res); } i++; } printf("Result is:%d",head->data); getch(); } void alloc(int k) { newnode=(node *)malloc(sizeof(node)); newnode->next=null; newnode->data=k; newnode->next=head; head=newnode; }
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|