0% found this document useful (0 votes)
58 views2 pages

DS Micro

This document contains code for several algorithms and data structures: 1) Infix to postfix conversion using a stack. 2) Evaluating a postfix expression using another stack. 3) Queue implementation using an array. 4) Linear search algorithm. 5) GCD calculation. 6) Factorial calculation. 7) Stack implementation with push and pop functions.

Uploaded by

Aman Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

DS Micro

This document contains code for several algorithms and data structures: 1) Infix to postfix conversion using a stack. 2) Evaluating a postfix expression using another stack. 3) Queue implementation using an array. 4) Linear search algorithm. 5) GCD calculation. 6) Factorial calculation. 7) Stack implementation with push and pop functions.

Uploaded by

Aman Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

 INFIX TO POSTFIX case'^':return (3); printf("\n Queue is Full");

# include<stdio.h> case'*': else


#include<conio.h> case'/': return(2); {
#include<string.h> case '+': printf("\n Enter no %d:",j++);
char infix[50],postfix[50],stack[25]; case'-':return(1); scanf("%d",&queue[rear++]);
int top=-1; case'(': }
break;
void convert_fun(); case')':return(0);
case 2:
int precedence(char); case'#':return(-1);
if(front==rear)
void push(char); }
{
char pop(); }
printf("\n Queue is empty");
void main() }
{  Evaluating an Postfix else
printf("Enter the infix expression\n"); #include<stdio.h> {
scanf("%s",infix); #include<conio.h> printf("\n Deleted Element is %d",queue[front++]);
convert_fun(); #include<string.h> x++;
printf("\n THE INFIX EXPRESSION :%s",infix); #include<math.h> }
printf("\n THE POSTFIX EXPRESSION:%s",postfix); #include<ctype.h> break;
getch(); int top,s[100]; case 3:
} int cal(char sym,int op1,int op2) printf("\nQueue Elements are:\n ");
void convert_fun() { if(front==rear)
{ printf("\n Queue is Empty");
switch(sym)
int i=0,j=0; else
{
{
char c,tmp; case'+' : return(op1+op2);
for(i=front; i<rear; i++)
push('#'); case'-' : return(op1-op2);
{
while(i<strlen(infix)) case'*' : return(op1*op2); printf("%d",queue[i]);
{ case'/' : return(op1/op2); printf("\n");
c=infix[i++]; case'^' : return(pow(op1,op2)); }
switch(c) } break;
{ return 0; case 4:
case '(' :push(c); } exit(0);
break; void main() default:
case ')' :tmp=pop(); { printf("Wrong Choice: please see the options");
while(tmp!='(') int i,op1,op2; }
{ char pf[100],sym; }
postfix[j++]=tmp; top=-1; }
tmp=pop(); printf("enter the postfix expression:"); return 0;
} }
gets(pf);
break; for(i=0;i<strlen(pf);i++) GCD
case'+': { #include <stdio.h>
case'-': sym=pf[i]; int main()
case'*': if(isdigit (sym)) {
case'/': { int n1, n2, i, gcd;
case'^': s[++top]=sym-'0'; printf("Enter two integers: ");
while(precedence(stack[top])>=precedence(c)) } scanf("%d %d", &n1, &n2);
{ else for(i=1; i <= n1 && i <= n2; ++i)
tmp=pop(); { {
postfix[j++]=tmp; op2=s[top--]; if(n1%i==0 && n2%i==0)
} op1=s[top--]; gcd = i;
push(c); s[++top]=cal(sym,op1,op2); }
break; } printf("G.C.D of %d and %d is %d", n1, n2, gcd);
default: postfix[j++]=c; } return 0;
break; printf("\n value of %s is %d",pf,s[top--]); }
} getch();
} }
Factorial
while(top>0)
#include <stdio.h>
{ Queue implementation int main() {
tmp=pop();
int n, i;
postfix[j++]=tmp; #include<stdio.h> unsigned long long fact = 1;
} #define n 5
printf("Enter an integer: ");
} int main()
scanf("%d", &n);
void push(char c) {
if (n < 0)
{ int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Error! Factorial of a negative number
stack[++top]=c; printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display doesn't exist.");
}
\n4.Exit"); else {
char pop()
while(ch) for (i = 1; i <= n; ++i) {
{
{ fact *= i;
return(stack[top--]);
printf("\nEnter the Choice:"); }
}
scanf("%d",&ch); printf("Factorial of %d = %llu", n, fact);
int precedence(char c)
switch(ch) }
{
{ return 0;
switch(c)
case 1: }
{
if(rear==x)
Linear Search Push an Element on to Stack else
#include<stdio.h> {
int main () #include <stdio.h> printf("\nElements present in the stack: \n");
{ #include <stdlib.h> for (int i = top; i >= 0; --i)
int a[20],i,x,n; clrscr(); #define SIZE 4 printf("%d\n", inp_array[i]);
printf("How many elements?\n");
scanf("%d",&n); int top = -1, inp_array[SIZE]; }
printf("Enter %d elements:\n"); void push(); }
for(i=0;i<n;++i) void pop();
scanf("%d",&a[i]); void show();
printf("Enter element to search:\n"); int main()
scanf("%d",&x);
for(i=0;i<n;++i) {
if(a[i]==x) int choice;
break; while (1)
if(i<n) {
printf("Element found at index %d",i); else printf("\nPerform operations on the stack:");
printf("Element not found"); getch();
return 0; printf("\n1.Push the element\n2.Pop the
} element\n3.Show\n4.End");
Binary Search printf("\n\nEnter the choice: ");
#include<stdio.h> scanf("%d", &choice);
#include<conio.h> switch (choice)
int main() {
{ case 1:
int i, low, high, mid, n, key, array[100]; push();
clrscr(); break;
printf("Enter number of elements \n"); case 2:
scanf("%d",&n); pop();
printf("Enter %d integers in ascending break;
order\n", n); for(i = 0; i < n; i++) case 3:
{
show();
scanf("%d",&array[i]);
break;
}
case 4:
printf("Enter value to find\n");
exit(0);
scanf("%d", &key); low = 0; high =n - 1;
default:
mid = (low+high)/2;
printf("\nInvalid choice!!");
while (low <= high)
{ }
if(array[mid] < key) low = mid + 1; }
else if (array[mid] == key) }
{ void push()
printf("%d found at location %d\n", key, {
mid); int x;
break; if (top == SIZE - 1)
} {
else printf("\nOverflow!!");
high = mid - 1; }
mid = (low + high)/2; else
} {
if(low > high) printf("\nEnter the element to be added onto
printf("Not found! %d isn't present in the
the stack: ");
list\n", key);
scanf("%d", &x);
getch();
top = top + 1;
return 0;
} inp_array[top] = x;
}
}
void pop()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d",
inp_array[top]);
top = top - 1;
}
}
void show()
{
if (top == -1)
{
printf("\nUnderflow!!");
}

You might also like