Ds - All Codes
Ds - All Codes
Infix to postfix
/***********************
C Program for, Here we covert the infix expression to postfix expression by using stack, for example
a*b-c/d is the infix expression, and equivalent postfix expression is: ab*cd/-.
**********************/
#include<stdio.h>
#include<conio.h>
#include <ctype.h>
char s[50];
s[++top] = elem;
char pop()
return (s[top--]);
switch (elem)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
int main()
/* Main Program */
int i = 0, k = 0;
scanf("%s",infx);
push('#');
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
pofx[k++] = pop();
else
/* Operator */
pofx[k++] = pop();
push(ch);
pofx[k++] = pop();
getch();
return(0);
algorithm-
4) If it is an operator, then
iii) If it has higher priority than the top of stack, push operator on stack.
iv) Else pop the operator from the stack and output it, repeat step 4
5) If it is a closing parenthesis, pop operators from stack and output them until an opening
parenthesis is encountered. pop and discard the opening parenthesis.
#include <stdio.h>
#include <stdlib.h>
struct Node
int Data;
}*top;
void popStack()
if (top==NULL)
printf("\nStack Empty");
}
else
top = top->next;
free(var);
temp->Data=value;
if (top == NULL)
top=temp;
top->next=NULL;
else
temp->next=top;
top=temp;
void display()
while(var!=NULL)
printf("\t%d\n",var->Data);
var=var->next;
printf("\n");
else
printf("\nStack is Empty");
int main()
int i=0;
top=NULL;
while(1)
scanf("%d",&i);
switch(i)
{
case 1:
int value;
scanf("%d",&value);
push(value);
display();
break;
case 2:
popStack();
display();
break;
case 3:
display();
break;
case 4:
while(top!=NULL)
temp = top->next;
free(top);
top=temp;
exit(0);
default:
3. Implementation of a stack
#include<stdio.h>
#include<stdlib.h>
#define MAX 12
int s[MAX];
/* function declaration*/
int isFull();
int isEmpty();
void pop();
void peek();
int main()
do
printf("\n1. PUSH\n");
printf("\n2.POP\n");
printf("\n3. PEEK\n");
printf("\n4.Exit\n");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
exit(0);
break;
}
}while(choice!=5);
return 0;
int isFull()
if(top==MAX-1)
return 1;
else
return 0;
int isEmpty()
if (top==-1)
return 1;
else
{
return 0;
void push()
if (isFull())
return;
else
int x;
printf("\nEnter a value\n");
scanf("%d",&x);
top++; // top=top+1
s[top]=x;
void pop()
if ( isEmpty())
printf("\nUnderflow\n");
return;
else
{
printf("\n%d popped from stack\n",s[top]);
top--; // top=top-1;
void peek()
if ( isEmpty())
printf("\nUnderflow\n");
return;
else
// top--; // top=top-1;
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
void insert();
void delete();
void frontt();
int queue[MAX],rear=-1,front=-1,item;
int main()
{
int ch;
do
scanf("%d",&ch);
switch(ch)
case 1:
insert(); break;
case 2:
delete(); break;
case 3:
frontt();
break;
case 4:
exit(0); break;
default:
} // end of switch
}while(1);
getch();
void insert()
front=rear=0;
return;
else
scanf("%d",&item);
queue[rear]=item;
} // end insert
void delete()
return;
{ item = queue[front];
front = rear = -1 ;
return;
item = queue[front];
void frontt()
printf("%d\t",queue[front]);
getch();
5. Decimal to binary
#include <stdio.h>
#include <conio.h>
int data[MAX];
int top;
}stack;
return(1);
return(0);
return(0);
int x;
return(x);
int main()
int num;
scanf("%d", &num);
while((num != 0))
push(&s, num % 2); //pushing the remainder into the stack using 'push' function
num = num / 2;
}
else
exit(0);
while(!empty(&s))
num = pop(&s); //poping the remainder out from the stack using 'pop' function
printf("%d", num);
*one by one pop all characters from stack and put them back to string. #include <stdio.h>
#include <string.h>
printf("stack overflow");
} else {
stack[++top]=x;
void pop(){
int main()
int i; for(i=0;i<len;i++)