Ques And Stack

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ques And Stack as PDF for free.

More details

  • Words: 518
  • Pages: 8
Infix to postfix using stacks #include<stdio.h> #include #include<process.h> #include<string.h> char stack[20]; int top=-1; void postfixof(char infix[]); void push(char); char pop(); int main() { char infix[20]; clrscr(); puts("enter a infix expression"); gets(infix); puts("\npostfix expression"); postfixof(infix); getch(); return 0; } void push(char s) { if(top>=19) { puts("stack overflow"); exit(0); } else { stack[++top]=s; } } char pop() { char num; if(top==-1) { puts("stack is empty"); getch(); return 0; } else { num=stack[top--];

} return num; } int test(char ch) { if(ch=='/') return 5; else if(ch=='*') return 4; else if(ch=='+') return 3; else return 2; } void postfixof(char infix[]) { int length; static int i=0,j=0; char s,t; char postfix[40]; length=strlen(infix); push('#'); while(itest(s)) { t=pop(); postfix[j++]=t; } push(s); break; default : postfix[j++]=s; break; } i++; }

while(top>0) { t=pop(); postfix[j++]=t; } postfix[j++]='\0'; puts(postfix); return; }

Postfix evaluation #include<stdio.h> #include #include #include<math.h> float stack[10]; int top=-1; void push(char); float pop(); float postfixeval(char postfix[],float number[]); int main() { int i=0; char postfix[20]; float number[20],res; clrscr(); printf("\n enter a postfix expression"); gets(postfix); while(postfix[i]!='\0') { if(isalpha(postfix[i])) { fflush(stdin); printf("\n enter number for %c",postfix[i]); scanf("%f",&number[i]); } i++; } res=postfixeval(postfix,number); printf("the res of %s=%f",postfix,res); getch(); return 0; } float postfixeval(char postfix[],float number[]) {

int i=0; float opa,opb,res; char ch; while(postfix[i]!='\0') { ch=postfix[i]; if(isalpha(postfix[i])) { push(number[i]); } else { opb=pop(); opa=pop(); switch(ch) { case '+': push(opa+opb); break; case '-': push(opa+opb); break; case '*': push(opa+opb); break; case '/': push(opa+opb); break; case '^': push(pow(opa,opb)); break; } } i++; } res=pop(); return res; } void push(char c) { stack[++top]=c; } float pop() { float n; n=stack[top--]; return n; }

Queue operations #include<stdio.h> #include<stdlib.h> #include<string.h>

int r=-1,f=-1,j,n,*queue,element,ch; void display(); void insert(); int del(); int main() { clrscr(); printf("\n enter the size of the queue"); scanf("%d",&n); queue=(int*)malloc(n*2); while(1) { printf("\n enter ur choice\n 1.insert \n 2.display \n 3.delete \n 4.exit"); scanf("%d",&ch); switch(ch) { case 1:insert();break; case 2: printf("\n Elements of queue are:"); display(); break; case 3:element=del(); printf("the element deleted is %d",element); break; default: exit(0); } } getch(); return 0; } void display() { for(r=0;r=n) { printf("overflow"); break; }

else { printf("enter a no to insert"); scanf("%d",&element); queue[++r]=element; } } } int del() { if(f==r) { printf("the queue is empty"); return 0; } f++; element=queue[f]; if(f==n) { f=1; r=1; } return element; }

Circular queue operations #include<stdio.h> #include<stdlib.h> #include<string.h> int r=-1,f=-1,j,n,*queue,element,ch; void display(); void insert(); int del(); int main() { clrscr(); printf("\n enter the size of the queue"); scanf("%d",&n); queue=(int*)malloc(n*2); while(1) { printf("\n enter ur choice\n 1.insert \n 2.display \n 3.delete \n 4.exit"); scanf("%d",&ch);

switch(ch) { case 1:insert();break; case 2: printf("\n Elements of queue are:"); display(); break; case 3:element=del(); printf("the element deleted is %d",element); break; default: exit(0); } } getch(); return 0; } void display() { for(r=0;r
printf("the queue is empty"); return 0; } f++; element=queue[f]; return element; }

Related Documents

Ques And Stack
June 2020 2
Stack
May 2020 16
Stack
April 2020 21
Stack
June 2020 14
Stack
November 2019 19
Stack
November 2019 19