Showing posts with label DataStructure. Show all posts
Showing posts with label DataStructure. Show all posts

Friday, 29 June 2012

Postfix To Infix Using c++


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
char stack[60],exp[60],p[60];
int tos=-1,m,n,t=1;
void main()
{
void push(char),display(int);
char ch, pop() ;
clrscr();
cout<<"Enter the Expression : ";
gets(exp);
exp[strlen(exp)]=')';
exp[strlen(exp)+1]=NULL;
// puts(exp);
getch();
push('(');
while(tos>-1)
{
switch(exp[m])
{
case'+':
case'-':
while(stack[tos]=='+'||stack[tos]=='-'||stack[tos]=='*'||stack[tos]=='/'||stack[tos]=='^'||stack[tos]=='%')
{
ch=pop();
p[n]=ch;
n++;
//push(exp[m]);
}
//else
push(exp[m]);
break;


case'*':
case'/':
case'%':
while(stack[tos]=='*'||stack[tos]=='/'||stack[tos]=='^'||stack[tos]=='%')
{
ch=pop();
p[n]=ch;
n++;
}
//else
push(exp[m]);
break;


case'^':
while(stack[tos]=='^')
{
ch=pop();
p[n]=ch;
n++;
}
//else
push(exp[m]);
break;
case ')':
while(stack[tos]!='(')
{
ch=pop();
p[n]=ch;
n++;
}
ch=pop();
break;
case '(':
push('(');
break;
case ' ':
default:
//ch=pop();
p[n]=exp[m];
n++;
}
m++;
//cout<<p[n]<<endl;


display(m-1);


}


}


void push(char c)
{
// cout<<"push";
tos++;
stack[tos]=c;
}






char pop()
{
char ch;
if(tos<0)
return(0);
else
{
ch=stack[tos];
stack[tos]=' ';
tos--;
return(ch);
}
}


void display(int aa)
{
//cout<<"\ncheck" ;
if(t==1)
{
cout<<"\n\t Symbol \t\t Stack \t\t\t Postfix Expression ";
cout<<"\n\t ------ \t\t ------\t\t\t -------------------\n";
t++;
}
if(tos==-1)
cout<<"\n\t "<<exp[aa]<<"\t\t\t Empty"<<"\t\t\t"<<p;
else
cout<<"\n\t "<<exp[aa]<<"\t\t\t "<<stack<<"\t\t\t\t"<<p;






cout<<endl;
getch();
}



Stack Implemenation Using C++


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
#include<graphics.h>
 int stack[50],n,tos=0;


 void main()
 {
   int i,o;
   void menu();
   void push();
   void pop();
   void display();
   clrscr();
    cout<<endl<<"ENTER THE NO. OF ELMENTS :";
    cin>>n;
    while(1)
    {
       menu();
       cout<<endl<<endl<<"ENTER YOUR CHOICE :";
       cin>>o;
       switch(o)
       {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<endl<<endl<<"EXITING. . . .";
delay(1000);
exit(0);
default:
cout<<endl<<endl<<"WRONG CHOICE. . .TRY AGAIN. . ";
getch();
       }
    }
//   getch();
 }
  void menu()
  {
    clrscr();
    cout<<endl<<endl<<"\t\t\t\t=========STACK OPERATIONS==========";
    cout<<endl<<"\t\t\t\t1.PUSH                               SIZE = "<<n;
    cout<<endl<<"\t\t\t\t2.POP";
    cout<<endl<<"\t\t\t\t3.DISPLAY";
    cout<<endl<<"\t\t\t\t4.EXIT";
  }


   void push()
   {
     if(tos>=n)
     {
       cout<<endl<<endl<<"SORRY STACK IS FULL. . OVERFLOW. . .";
       getch();
     }
     else
     {
       ++tos;
       int v;
       cout<<endl<<endl<<"ENTER THE VALUE TO BE INSERTED IN STACK : ";
       cin>>v;
       stack[tos]=v;
     }
   }


    void pop()
    {
      if(tos==0)
      {
cout<<endl<<endl<<"SORRY STACK IS EMPTY. . . UNDERFLOW. . .";
getch();
      }
      else
      {
tos--;
      }
    }
     void display()
     {
int i;
 if(tos==0)
 {
 cout<<endl<<endl<<"NO ELEMENTS TO DISPLAY. . . USE PUSH TO INSERT";
 }
 else
 {
  cout<<endl<<endl<<"\t\tELEMENTS IN STACK ARE . . . .                 TOS = "<<tos;
//   cout<<tos;
   for(i=tos;i>=1;i--)
   {
     cout<<endl<<endl<<"\t\t\tSTACK["<<i<<"] = "<<stack[i];
   }
 }
 getch();
     }

Insert Element At First Position In A Link List


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct ll
{
     int info;
     struct ll *p;
   };
struct ll *start,*ptr;
void main()
{       clrscr();
     void infirst(ll*);
     void create (ll*);
     void print(ll*);
     start=(ll*) malloc(sizeof(ll));
     ptr=start;
     create(ptr);
       cout<<"START---->";
       print(ptr);
     infirst(ptr);
     cout<<"LINK List BECOMES:";
     print(ptr);
     getch();
}
void create (struct ll *m)
{
   char ch;
   cout<<"\n ENTER THE INFO PART:";
   cin>>m->info;
   cout<<"\n DO YOU WANT ANOTHER NODE(yes-'y' or no-'n')";
   cin>>ch;
   if(ch=='n'||ch=='N')
   {
m->p=NULL;
return;
   }
   else
   {
      m->p=(ll*)malloc(sizeof(ll));
      create(m->p);
   }
}
void print(struct ll *m)
{


    if(m->p==NULL)
    {
cout<<m->info<<"--->X";
return;
     }
     else
     {
cout<<m->info<<"---->";
print(m->p);
       }
  }
  void infirst(ll *m)
  {
struct ll *new1;
new1=(ll *)malloc(sizeof(ll));
cout<<"\n ENTER THE ITEM IN THE NODE";
cin>>new1->info;
if(m->p==NULL)
{
    new1->p=NULL;
    ptr=new1;
 }
 else
 {
      new1->p=m;
      ptr=new1;
   }


       }

Radix Sort


#include<iostream.h>
#include<process.h>
#include<conio.h>
int a[50];
void radix(int *a,int);
main()
{
int i,n;
clrscr();
cout<<"\n Enter The Size of given List : ";
cin>>n;
cout<<"\n Enter the elements of List :";
for(i=0;i<n;i++)
{
cout<<"\n A["<<i<<"]= ";
cin>>*(a+i);
}
radix(a,n);
for(i=0;i<n;i++)
{
cout<<"\n A["<<i<<"]= "<<a[i];
}
getch();
}




void radix(int *a,int n)
{
int rad[10][20],c[10],i,j,k,r,dc=0,div=1,lar,pno;
lar=a[0];
for(i=1;i<n;i++)
{
if(a[i]>lar)
{
lar=a[i];
}
}
while(lar>0)
{
dc++;
lar=lar/10;
}
for(pno=0;pno<dc;pno++)
{
for(k=0;k<10;k++)


c[k]=0;
for(i=0;i<n;i++)
{
r=(a[i]/div)%10;
rad[r][c[r]++]=a[i];
}


i=0;
for(k=0;k<10;k++)
{
for(j=0;j<c[k];j++)
{
a[i++]=rad[k][j];
}
}
div=div*10;
}
}

QuickSort


#include<iostream.h>
#include<conio.h>
int a[50],upper[50],lower[50],utos,ltos,n,beg,end;
int upop(int *);
void upush(int *,int);
int lpop(int *);
void lpush(int *,int);
int quick(int *,int,int);
void main()
{
clrscr();
int loc1;
cout<<"\n Enter number of elements:";
cin>>n;
cout<<"\n Enter array:\n";
for(int i=1;i<=n;i++)
{
cout<<" a["<<i<<"]: ";
cin>>a[i];
}
beg=1;
end=n;
lpush(lower,beg);
upush(upper,end);
while(utos>0)
{
beg=lpop(lower);
end=upop(upper);
loc1=quick(a,beg,end);
if(beg<(loc1-1))
{
lpush(lower,beg);
upush(upper,(loc1-1));


}
if(end>(loc1+1))
{
lpush(lower,(loc1+1));
upush(upper,end);
}
}
cout<<"\n Sorted List:\n";
for(i=1;i<=n;i++)
{
cout<<"\n a["<<i<<"]: "<<a[i];
}


getch();
}
void upush(int *a,int item)
{
utos=utos+1;
a[utos]=item;
}
void lpush(int *a,int item)
{
ltos=ltos+1;
a[ltos]=item;
}
int upop(int *a)
{    int item;
item=a[utos];
utos=utos-1;
return(item);
}
int lpop(int *a)
{
int item;
item=a[ltos];
ltos=ltos-1;
return(item);
}


int quick(int *a,int beg,int end)
{
int left,right,temp,loc;
left=beg;
right=end;
loc=beg;
rtol:while((a[loc]<=a[right])||(loc!=right))
{
if(loc==right)
{
return(loc);
}
else
{
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
goto ltor;
}
}
right =right-1;
}
ltor: while(a[left]<=a[loc]||left!=loc)
{
if(loc==left)
{
return(loc);
}
else
{
if(a[left]>a[loc])
{
temp=a[loc];
a[loc]=a[left];
a[left]=temp;
loc=left;
goto rtol;
}
}
left=left+1;
}
}

Bubble Sort Using Pointer's


#include<iostream.h>
#include<conio.h>
#include<process.h>
 void main()
{
clrscr();
cout<<"ENTER NUMBER OF ELEMENTS IN ARRAY ";
cin>>n;
cout<<"\n\nENTER ELEMENTS OF ARRAY ";
input(a,n);
cout<<"\n\nELEMENTS OF ARRAY ARE\n";
output(a,n);


cout<<"**********BUBBLE SORT**********\n\n";
int i,temp,j;
for(i=1;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if((*(a+j))>(*(a+j+1)))
{
temp=(*(a+j));
(*(a+j))=(*(a+j+1));
(*(a+j+1))=temp;
}
}
cout<<"\n";
output(a,n);
getch();
}
cout<<"\n\nARRAY SORTED.....";
getch();
}


void input(int *a,int n)
{
int i;
for(i=0;i<n;i++)
cin>>(*(a+i));
}
void output(int *a,int n)
{
int i;
for(i=0;i<n;i++)
PAGE :16
cout<<(*(a+i))<<" ";
}