0% found this document useful (0 votes)
94 views8 pages

Ques and Stack

The document discusses different data structures and algorithms related to stacks, queues, and their operations. It includes code implementations for: 1. Converting infix to postfix notation using a stack. 2. Evaluating a postfix expression using a stack. 3. Performing operations like insertion and deletion on a queue. 4. Implementing a circular queue with operations like insertion and deletion.

Uploaded by

rajeshkripal3945
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views8 pages

Ques and Stack

The document discusses different data structures and algorithms related to stacks, queues, and their operations. It includes code implementations for: 1. Converting infix to postfix notation using a stack. 2. Evaluating a postfix expression using a stack. 3. Performing operations like insertion and deletion on a queue. 4. Implementing a circular queue with operations like insertion and deletion.

Uploaded by

rajeshkripal3945
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Infix to postfix using stacks

#include<stdio.h>
#include<conio.h>
#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(i<length)
{
s=infix[i];
switch(s)
{
case '(': push(s); break;
case ')': t=pop();
while(t!='(')
{
postfix[j++]=t;
t=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(test(stack[top])>test(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<conio.h>
#include<ctype.h>
#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;r++)
printf("%d",queue[r]);
}

void insert()
{
while(1)
{
if(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<n;r++)
printf("%d",queue[r]);
}

void insert()
{
while(1)
{
if((r==n-1))
{
printf("overflow");
break;
}
printf("enter a no to insert");
scanf("%d",&element);
queue[++r]=element;

if(f==r)
f=0;

}
}
int del()
{
if(f==n)
f=0;
if(f==r)
{
printf("the queue is empty");
return 0;
}
f++;
element=queue[f];
return element;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy