Saturday, February 7, 2015

Deleting a node in btree in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct bstnode
{
bstnode *lchild;
int data;
bstnode *rchild;
};
bstnode *insert(bstnode * &t,int k)
{
if(t==NULL)
{
t=new(bstnode);
t->lchild=NULL;
t->rchild=NULL;
t->data=k;
}
else if(t->data>k)
insert(t->lchild,k);
else
insert(t->rchild,k);
}
int search(bstnode *t,int k)
{
if(t==NULL)
return 0;
else if(t->data>k)
return(search(t->lchild,k));
else if(t->data<k)
return(search(t->rchild,k));
else
return 1;
}
void printsort(bstnode * t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int max2(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return max2(t->lchild);
}
void del(bstnode * &t,int k)
{
if(t!=NULL)
{
if(k==t->data && (t->rchild!=NULL || t->lchild!=NULL))
{
int a=max(t->lchild);
if(a){
del(t,a);
t->data=a;
}
else{
int b=max2(t->rchild);
if(b)
del(t,b);
t->data=b;
}
}
else if(k==t->data && (t->rchild==NULL && t->lchild==NULL))
{
t=NULL;
}
else if(k>t->data)
del(t->rchild,k);
else
del(t->lchild,k);
}
}
void begin()
{
bstnode *s,*t;
int k,i,choice;
t=NULL;
while(1)
{
  cout<<"Select any of the options provided below....\n1:Enter the bstree\n2:Search for a no\n3:Max no\n4:Min no\n5:Printing terminal node\n6:Printing Sorted no's\n";
  cin>>choice;
  switch(choice)
  {
  case 1 :cout<<"Press -1 for aborting....\n";
         cin>>k;
         while(k!=-1)
         {
          insert(t,k);
          cin>>k;
         }
         break;
case 2 :cout<<"Enter the no:";
cin>>k;
del(t,k);
break;
case 3 :cout<<"The sorted nos's are\n";
printsort(t);
break;
case 10:cout<<"Thanks\n";
       exit(1);
default:cout<<"Wrong Choice Entered....Please enter again\n";
break;
  }
    }
}
int main()
{
begin();

}

B tree creation basic in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct bstnode
{
bstnode *lchild;
int data;
bstnode *rchild;
};
bstnode *insert(bstnode * &t,int k)
{
if(t==NULL)
{
t=new(bstnode);
t->lchild=NULL;
t->rchild=NULL;
t->data=k;
}
else if(t->data>k)
insert(t->lchild,k);
else
insert(t->rchild,k);
}
int search(bstnode *t,int k)
{
if(t==NULL)
return 0;
else if(t->data>k)
return(search(t->lchild,k));
else if(t->data<k)
return(search(t->rchild,k));
else
return 1;
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int min(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return min(t->lchild);
}
void ter_print(bstnode *t)
{
if(t!=NULL)
{
if(t->lchild==NULL && t->rchild==NULL)
cout<<t->data<<endl;
ter_print(t->lchild);
ter_print(t->rchild);
}
}
void printsort(bstnode *t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
void begin()
{
bstnode *s,*t;
int k,i,choice;
t=NULL;
while(1)
{
  cout<<"Select any of the options provided below....\n1:Enter the bstree\n2:Search for a no\n3:Max no\n4:Min no\n5:Printing terminal node\n6:Printing Sorted no's\n";
  cin>>choice;
  switch(choice)
  {
  case 1 :cout<<"Press -1 for aborting....\n";
         cin>>k;
         while(k!=-1)
         {
          insert(t,k);
          cin>>k;
         }
         break;
  case 2 :cout<<"Enter the element you are looking for:\n";
     cin>>k;
     s=t;
   i=search(s,k);
   if(i==1)
cout<<"Found\n";
   else
    cout<<"Not found\n";
   break;
case 3 :cout<<"Thus the maximum no is:";
s=t;
i=max(s);
if(i==0)
cout<<"No maximum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 4 :cout<<"Thus the minimum no is:";
s=t;
i=min(s);
if(i==0)
cout<<"No minimum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 5 :cout<<"Thus the Terminal Nodes are\n";
ter_print(t);
break;
case 6 :cout<<"The sorted nos's are\n";
printsort(t);
break;
case 10:cout<<"Thanks\n";
       exit(1);
default:cout<<"Wrong Choice Entered....Please enter again\n";
break;
  }
    }
}
int main()
{
begin();

}

Doubly linked list all operations

#include<iostream>
#include<cstdlib>
using namespace std;
struct dnode
{
int data;
dnode *right;
dnode *left;
};
void display(dnode *s)
{
dnode *p;
p=s;
cout<<"\tThus the no's are as:\n";
while(p!=NULL)
{cout<<"\t"<<p->data<<endl;
p=p->right;
}
}
dnode *add_end(dnode *s,int d)
{
dnode *p,*l;
l=s;
while(l->right!=NULL)
l=l->right;
p=new(dnode);
p->data=d;
p->right=NULL;
p->left=NULL;
l->right=p;
p->left=l;
return s;
}
dnode *add_begin(dnode *s,int d)
{
dnode *p,*l;
p=s;
l=new(dnode);
l->data=d;
l->right=p;
l->left=NULL;
p->left=l;
s=l;
return s;
}
dnode *add_after(dnode *s,int d,int k)
{
dnode *p,*l;
p=s;
while(p->data!=d)
p=p->right;
if(p->right==NULL)
{
l=new(dnode);
l->data=k;
l->right=NULL;
l->left=p;
p->right=l;
return s;
}
else
{
l=new(dnode);
l->data=k;
l->right=NULL;
l->left=NULL;
l->right=p->right;
l->left=p;
p->right=l;
l->right->left=l;
return s;
}
}
dnode *add_before(dnode *s,int d,int k)
{
dnode *p,*l;
p=s;
if(p->data==d)
{
l=new(dnode);
l->data=k;
    l->right=p;
   l->left=NULL;
p->left=l;
s=l;
return s;
}
else
{
while(p->data!=d)
p=p->right;
l=new(dnode);
l->data=k;
l->left=p->left;
l->right=p;
p->left=l;
l->left->right=l;
return s;
}
}
dnode *del_end(dnode *s)
{
dnode *p;
p=s;
while(p->right->right!=NULL)
p=p->right;
p->right=NULL;
return s;
}
dnode *del_begin(dnode *s)
{
dnode *p;
p=s;
p=p->right;
p->left=NULL;
s=p;
return s;
}
dnode *del_mid(dnode *s,int d)
{
dnode *p;
p=s;
while(p->right->data!=d)
p=p->right;
p->right=p->right->right;
p->right->left=p;
return s;
}
int main()
{
dnode *s,*t,*l;
int d,k,n;
l=new(dnode);
s=l;
    cout<<"Start entering the no's.....Enter -1 for aborting...\n";
    cin>>d;
    l->data=d;
    l->left=NULL;
    l->right=NULL;
    cin>>d;
    while(d!=-1)
     {t=new(dnode);
      t->data=d;
      t->right=NULL;
      t->left=NULL;
      l->right=t;
      t->left=l;
      l=t;
      cin>>d;
     }
    display(s);
    while(1)
{
  cout<<"Select any of the options provided below....\n1:Adding at the end\n2:Adding at the beginning\n3:Adding after a specified no\n4:Adding before a specified no\n5:Deleting a no at the end\n6:Deleting a no at the beginning\n7:Deleting a no in between the no's\n8:For quitting\n";
  cin>>n;
       switch(n)
       {case 1 :cout<<"Enter the no:";
                cin>>d;
                s=add_end(s,d);
                display(s);
                break;
        case 2 :cout<<"Enter the no:";
                cin>>d;
                s=add_begin(s,d);
                display(s);
                break;
        case 3 :cout<<"Enter the no after which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_after(s,d,k);
       display(s);
break;
case 4 :cout<<"Enter the no before which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_before(s,d,k);
       display(s);
       break;
case 5 :s=del_end(s);
       display(s);
       break;
case 6 :s=del_begin(s);
       display(s);
       break;
case 7 :cout<<"Enter the no which you want to delete:";
       cin>>d;
       s=del_mid(s,d);
       display(s);
       break;
case 8 :cout<<"Thanks...";
       exit(1);
        default:cout<<"Wrong choice....Enter again\n";
                break;
       }
    }
}

Adding large numbers using linked list in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
void display(node *l)
{
node *s;
s=l;
while(s!=NULL)
{
cout<<s->data;
s=s->next;
}
}
node *add(node *p,node *q)
{
node *t,*l;
int d;
t=NULL;
while(p!=NULL && q!=NULL)
{
d=p->data+q->data;
p=p->next;
q=q->next;
if(d>9)
{
d-=10;
if(p==NULL)
{
p=new(node);
p->data=0;
p->next=NULL;
}
if(q==NULL)
{
q=new(node);
q->data=0;
q->next=NULL;
}
p->data+=1;
}
if(t==NULL)
{
t=new(node);
t->data=d;
t->next=NULL;
}
else
{
l=new(node);
l->data=d;
l->next=t;
t=l;
}
if(p==NULL && q==NULL)
break;
if(p==NULL)
{
p=new(node);
p->data=0;
p->next=NULL;
}
if(q==NULL)
{
q=new(node);
q->data=0;
q->next=NULL;
}
}
return l;
}
int main()
{
node *s1,*s2,*l,*t;
int d;
cout<<"\nEnter the no's....Press -1 for aboting....:\n";
cin>>d;
t=new(node);
t->data=d;
t->next=NULL;
cin>>d;
while(d!=-1)
{
l=new(node);
l->data=d;
l->next=t;
t=l;
cin>>d;
}
s1=l;
cout<<"\nEnter the no's....Press -1 for aboting....:\n";
cin>>d;
t=new(node);
t->data=d;
t->next=NULL;
cin>>d;
while(d!=-1)
{
l=new(node);
l->data=d;
l->next=t;
t=l;
cin>>d;
}
s2=l;
l=add(s1,s2);
cout<<"Thus the addn is:\n";
display(l);
}

Adding two mathematical function using linked list in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int co;
int po;
node *next;
};
void display(node *s)
{
node *p;
p=s;
while(p!=NULL)
{
cout<<p->co<<"*("<<p->po<<")+";
p=p->next;
}

}
node *add(node *s)
{
node *t,*p,*q,*r,*e,*z,*f;int ctr=0;
r=NULL;
e=r;
p=s;
while(p!=NULL)
{
t=new(node);
t->po=p->po;
t->co=p->co;
t->next=NULL;
q=p;
if(q!=NULL)
q=q->next;
if(q==NULL);
else
{

while(q!=NULL)
{
if(p->po==q->po)
{
t->co+=q->co;
if(p->next->po==q->po)
p->next=p->next->next;
else if(p->next==NULL)
break;
else
{
f=p;
while(f->next->po!=q->po)
f=f->next;
f->next=f->next->next;
break;
}
}
if(q->next==NULL)
break;
else
q=q->next;
}
}
if(r==NULL)
{
r=t;
e=r;
}
else
{
r->next=t;
r=t;
}
p=p->next;
}
return e;
}
node *sub(node *s1,node *s2)
{
node *t,*p,*q,*r,*e,*s,*z,*f,*w;int ctr=0;
r=NULL;
e=r;
s=s1;
w=s2;
while(s2!=NULL)
{
s2->co*=-1;
s2=s2->next;
}
while(s1->next!=NULL)
s1=s1->next;
s1->next=w;
p=s;
while(p!=NULL)
{
t=new(node);
t->po=p->po;
t->co=p->co;
t->next=NULL;
q=p;
if(q!=NULL)
q=q->next;
if(q==NULL);
else
{

while(q!=NULL)
{
if(p->po==q->po)
{
t->co+=q->co;
if(p->next->po==q->po)
p->next=p->next->next;
else if(p->next==NULL)
break;
else
{
f=p;
while(f->next->po!=q->po)
f=f->next;
f->next=f->next->next;
break;
}
}
if(q->next==NULL)
break;
else
q=q->next;
}
}
if(r==NULL)
{
r=t;
e=r;
}
else
{
r->next=t;
r=t;
}
p=p->next;
}
return e;
}
node *mul(node *s1,node *s2)
{
node *l,*t,*s;
t=NULL;
s=s2;
while(s1!=NULL)
{
s2=s;
while(s2!=NULL)
{
if(t==NULL)
{
t=new(node);
t->po=(s1->po)+(s2->po);
t->co=(s1->co)*(s2->co);
t->next=NULL;
}
else
{
l=new(node);
l->po=(s1->po)+(s2->po);
l->co=(s1->co)*(s2->co);
l->next=t;
t=l;
}
s2=s2->next;
}
s1=s1->next;
}
t=add(l);
return t;
}
int main()
{
node *s,*l,*p1,*p2,*t,*s1,*s2;
int i,d,k;
l=new(node);
p1=l;
    cout<<"Start entering the Coeff. & Power.....Enter -1 for aborting...\n";
    cin>>d;
    cin>>k;
    l->co=d;
    l->po=k;
    l->next=NULL;
    cin>>d;
    cin>>k;
    while(d!=-1)
     {t=new(node);
      t->co=d;
      t->po=k;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
      cin>>k;
     }
    display(p1);
    l=new(node);
    p2=l;
    cout<<"\nStart entering the Coeff. & Power.....Enter -1 for aborting...\n";
    cin>>d;
    cin>>k;
    l->co=d;
    l->po=k;
    l->next=NULL;
    cin>>d;
    cin>>k;
    while(d!=-1)
     {t=new(node);
      t->co=d;
      t->po=k;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
      cin>>k;
     }
    display(p2);
    s2=p2;
    s1=p1;
    l=p1;
    t=p1;
    while(l->next!=NULL)
    l=l->next;
    k=l->co;
    d=l->po;
    while(s1->next!=NULL)
    s1=s1->next;
    s1->next=s2;
    cout<<"\nPress...\n1 for Addition\n2 for Subtraction\n3 for Multiplication\n";
    cin>>k;
    switch(k)
    {
    case 1:s=add(p1);
      display(s);
      break;
    case 2:while(t->co!=k && t->po!=d)
          t=t->next;
          t->next=NULL;
  s=sub(p1,p2);
      display(s);
      break;
    case 3:while(t->co!=k && t->po!=d)
          t=t->next;
          t->next=NULL;
  s=mul(p1,p2);
      display(s);
      break;
    }
}

Radix sort in C++

#include<iostream>
#include<math.h>
using namespace std;
struct node
{
int data;
node *next;
};
void display(node *s)
{
node *p;
p=s;
cout<<"\tThus the no's are as:\n";
while(p!=NULL)
{cout<<"\t"<<p->data<<endl;
p=p->next;
}
}
node *add_end(node *q,int n)
{
node *p,*l;
l=q;
if(l==NULL)
{
l=new(node);
    l->data=n;
    l->next=NULL;
    return l;
}
else
{
while(q->next!=NULL)
    q=q->next;
      p=new(node);
    p->data=n;
        p->next=NULL;
        q->next=p;
        return l;
}

}
int main()
{
node *r[10],*l,*s,*t;
int i,n,d,k,z,j,max=-1,ctr=0;
l=new(node);
s=l;
    cout<<"Start entering the no's.....Enter -1 for aborting...\n";
    cin>>d;
    l->data=d;
    l->next=NULL;
    cin>>d;
    while(d!=-1)
     {t=new(node);
      t->data=d;
      if(max<d)
      max=d;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
     }
    while(max!=0)
    {
    max=max/10;
    ctr++;
    }
    for(j=1;j<ctr+1;j++)
    {
    for(i=0;i<10;i++)
          r[i]=NULL;
    while(s!=NULL)
        {
       n=s->data;
       k=pow(10,j);
       z=n%k;
       d=pow(10,j-1);
       i=z/d;
        r[i]=add_end(r[i],n);
          s=s->next;
        }
        s=NULL;
        l=s;
        for(i=0;i<10;i++)
        {
        if(r[i]!=NULL)
        {
        t=r[i];
        if(s==NULL)
        {
        s=t;
        l=s;
        }
        else
        {
        while(s->next!=NULL)
        s=s->next;
        s->next=t;
        }
        }
        }
        s=l;
    }
    display(s);
}

Bin sort in C++

#include<iostream>
using namespace std;
struct nodep
{
int data;
nodep *next1;
};
struct node
{
char ch;
nodep *p;
nodep *q;
node *next;
};
int search(char c,node *K)
{
while(K->next!=NULL)
{
if(K->ch==c)
{
cout<<"welcome:\n";
return 1;
}
else
if(K->next!=NULL)
K=K->next;
}
return 0;
}
void print(node *KK)
{
while(KK!=NULL)
{
cout<<KK->ch<<"    ";
while(KK->q!=NULL)
{
cout<<KK->q->data<<"      ";
KK->q=KK->q->next1;
}
cout<<"\n";
KK=KK->next;
}
}
int main()
{
node *L,*S;char ch1,ch2,choice;int n,y;
L=new(node);S=L;
cout<<"enter the character:\n";
cin>>ch1;
ch2=ch1;
L->ch=ch1;
cout<<"enter the data value:\n";
cin>>n;
L->p=new(nodep);
L->q=L->p;
L->p->data=n;
L->p->next1=NULL;
L->next=NULL;
cout<<"enter y to continue:\n";
cin>>choice;
while(choice=='y'||choice=='Y')
{
cout<<"enter the character:\n";
cin>>ch1;
if(ch1==ch2)
y=1;
else
y=search(ch1,S);
ch2=ch1;
if(y==1)
{
L=S;
while(L->ch!=ch1)
L=L->next;
nodep *temp;
temp=new(nodep);
cout<<"enter the data value:\n";
cin>>n;
temp->data=n;
temp->next1=NULL;
while(L->p->next1!=NULL)
L->p=L->p->next1;
L->p->next1=temp;
L->p=temp;
}
else
{
L=S;
while(L->next!=NULL)
  L=L->next;
  node *temp;
  temp=new(node);
  temp->ch=ch1;
  temp->p=new(nodep);
  temp->q=temp->p;
  cout<<"enter the data value:\n";
  cin>>n;
  temp->p->data=n;
  temp->p->next1=NULL;
  temp->next=NULL;
  L->next=temp;
  L=temp;
}
cout<<"enter y to continue:\n";
cin>>choice;
}
print(S);
return 0;
}

implementation of stack using linked list in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
struct lns
{
node *top;
};
node *push(node *l,int k)
{
node *t,*q;
t=l;
t=new(node);
t->data=k;
t->next=NULL;
return l;
}
int pop(node *l)
{
node *q;int k;
q=l;
k=q->data;
q=q->next;
l=q;
return k;
}
int main()
{
lns *s;
s=NULL;
node *t,*l;
int k,i;
cout<<"Enter no's...Press -1 for aborting...\n";
cin>>k;
while(k!=-1)
{
s=new(lns);
s->top=NULL;
l=push(s->top,k);
cin>>k;
}
for(i=1;i<count;i++)
{
k=pop(l);
cout<<k<<endl;
}
}

Implementation of linked list and all operations in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
void display(node *s)
{
node *p;
p=s;
cout<<"\tThus the no's are as:\n";
while(p!=NULL)
{cout<<"\t"<<p->data<<endl;
p=p->next;
}
}
node *add_end(node *s,int d)
{
node *p,*l;
l=s;
while(l->next!=NULL)
l=l->next;
p=new(node);
p->data=d;
p->next=NULL;
l->next=p;
return s;
}
node *add_begin(node *s,int d)
{
node *p,*l;
p=s;
l=new(node);
l->data=d;
l->next=p;
s=l;
return s;
}
node *add_after(node *s,int d,int k)
{
node *p,*l;
p=s;
while(s->data!=d)
s=s->next;
l=new(node);
l->data=k;
l->next=s->next;
s->next=l;
return p;
}
node *add_before(node *s,int d,int k)
{
node *p,*l;
p=s;
if(p->data==d)
{
l=new(node);
    l->data=k;
    l->next=p;
   s=l;
    return s;
}
else
{
while(p->next->data!=d)
p=p->next;
l=new(node);
l->data=k;
l->next=p->next;
p->next=l;
return s;
}
}
node *del_end(node *s)
{
node *p;
p=s;
while(p->next->next!=NULL)
p=p->next;
p->next=NULL;
return s;
}
node *del_begin(node *s)
{
node *p;
p=s;
p=p->next;
s=p;
return s;
}
node *del_mid(node *s,int d)
{
node *p;
p=s;
while(p->next->data!=d)
p=p->next;
p->next=p->next->next;
return s;
}
int main()
{
node *s,*t,*l;
int d,k,n;
l=new(node);
s=l;
    cout<<"Start entering the no's.....Enter -1 for aborting...\n";
    cin>>d;
    l->data=d;
    l->next=NULL;
    cin>>d;
    while(d!=-1)
     {t=new(node);
      t->data=d;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
     }
    display(s);
    while(1)
{
  cout<<"Select any of the options provided below....\n1:Adding at the end\n2:Adding at the beginning\n3:Adding after a specified no\n4:Adding before a specified no\n5:Deleting a no at the end\n6:Deleting a no at the beginning\n7:Deleting a no in between the no's\n8:For quitting\n";
  cin>>n;
       switch(n)
       {case 1 :cout<<"Enter the no:";
                cin>>d;
                s=add_end(s,d);
                display(s);
                break;
        case 2 :cout<<"Enter the no:";
                cin>>d;
                s=add_begin(s,d);
                display(s);
                break;
        case 3 :cout<<"Enter the no after which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_after(s,d,k);
       display(s);
break;
case 4 :cout<<"Enter the no before which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_before(s,d,k);
       display(s);
       break;
case 5 :s=del_end(s);
       display(s);
       break;
case 6 :s=del_begin(s);
       display(s);
       break;
case 7 :cout<<"Enter the no which you want to delete:";
       cin>>d;
       s=del_mid(s,d);
       display(s);
       break;
case 8 :cout<<"Thanks...";
       exit(1);
        default:cout<<"Wrong choice....Enter again\n";
                break;
       }
    }
}

Implementation of stack containing queues as elements data structure in C++

#include<iostream>
using namespace std;
class queue
{public:
int elmnt[6];
int front;
int rear;
int isempty();
void enqueue(int e);
    int dequeue();
    queue()
    {rear=-1;
     front=-1;
    }
};
int queue::isempty()
{if(front==-1)
  {
   return 1;
  }
  else
  return 0;
}
void queue::enqueue(int e)
{ rear=(rear+1)%3;
  elmnt[rear]=e;
  if(front==-1)
  front++;
}
int queue::dequeue()
{int c;
  if(isempty());
  else
   {c=elmnt[front];
    if(front==rear)
    {front=-1;
     rear=-1;
     return c;
    }
    else
     front=(front+1)%3;
    return(c);
   }
}
class stack
{public:
  queue q[10],r;
  int top,t,arr[50];
  void push(queue f);
  queue pop();
};
void stack::push(queue f)
{q[++top]=f;
}
queue stack::pop()
{
return q[top--];
}

int main()
{stack s;
 int no,i,j,n;
 s.top=-1;
 cout<<"Enter the no of stacks:";
 cin>>n;
 for(i=0;i<n;i++)
 {  cout<<"Enter in the "<<i+1<<" stack:\n";
    for(j=0;j<3;j++)
     {cin>>no;
 s.r.enqueue(no);
     }
    s.push(s.r);
 }
 for(i=n;i>0;i--)
  {cout<<"Thus the "<<i<<" stack contains;\n";
     s.r=s.pop();
      for(j=0;j<3;j++)
       {cout<<s.r.dequeue()<<" ";
       }
       cout<<endl;
  }
}

Implementation of Bubble sort using Queue in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class queue
{ public:
    int size,i,j,n;
    int elmnt[50];
    int front;
    int rear;
    void enqueue(int j);
    void dequeue();
    void sort();
    queue()
    {size=50;
     front=-1;
     rear=-1;
    }
};
void queue::enqueue(int e)
{ rear=(rear+1)%size;
     elmnt[rear]=e;
     if(front==-1)
     front++;
}
void queue::dequeue()
{
     cout<<elmnt[front]<<endl;
front=(front+1)%size;
   
}
void queue::sort()
{for(i=0;i<n-1;i++)
 {for(j=0;j<n-1-i;j++)
  {if(elmnt[j]>elmnt[j+1])
    {elmnt[j]=elmnt[j]+elmnt[j+1];
     elmnt[j+1]=elmnt[j]-elmnt[j+1];
     elmnt[j]=elmnt[j]-elmnt[j+1];
    }
  }
 }
}
int main()
{queue s;
 cout<<"Enter the no of terms you want to enter:";
 cin>>s.n;
 cout<<"Enter the no's:\n";
 for(s.i=0;s.i<s.n;s.i++)
 {cin>>s.j;
  s.enqueue(s.j);
 }
 cout<<"Thus the no's after sorting are:\n";
 s.sort();
 for(s.i=0;s.i<s.n;s.i++)
  s.dequeue();
}

Evaluating postfix expression using queue in C++

#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class queue
{ public:
    int size;
    char elmnt[50];
    int front;
    int rear;
int isempty();
    void enqueue(int e);
    int dequeue();
    queue()
    {size=20;
     front=-1;
     rear=-1;
    }
};
int queue::isempty()
{if(front==-1)
  {cout<<"Empty";
   return 1;
  }
  else
  return 0;
}
void queue::enqueue(int e)
{  rear=(rear+1)%size;
     elmnt[rear]=e;
     if(front==-1)
     front=0;
}
int queue::dequeue()
{ int w=0;
  if(isempty());
  else
   {w=elmnt[front];
    if(front==rear)
    {front=-1;
     rear=-1;
    }
    else
     front=(front+1)%size;
   }
   return(w);
}
int main()
{queue s;int j,i,c[2],ctr=-1,z,x,t,k;char arr[50];
  cin.getline(arr,50);
  k=strlen(arr);
  for(i=0;i<k;i++)
   {if(arr[i]=='+'||arr[i]=='-'||arr[i]=='*'||arr[i]=='/')
     {for(j=0;j<i-2;j++)
       {x=s.dequeue();s.enqueue(x);
       }
      for(j=i-2;j<i;j++)
      {c[++ctr]=s.dequeue();
      }
      ctr=-1;
      if(arr[i]=='+')
       z=c[0]+c[1];
      else if(arr[i]=='-')
       z=c[0]-c[1];
      else if(arr[i]=='*')
       z=c[0]*c[1];
      else
       z=c[0]/c[1];
      s.enqueue(z);
     }
    else
    {t=arr[i]-48;
s.enqueue(t);}
   }
   x=s.dequeue();
   cout<<x;
}

Sorting using two stacks in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class stack
{public:
int elmnt[50],top,i,j,n;
void push(int e);
void pop();
void sort();
};
 void stack::push(int j)
 { elmnt[++top]=j;
 }
 void stack::pop()
 {cout<<elmnt[top--]<<endl;
 }
 void stack::sort()
 { for(i=0;i<n-1;i++)
     {for(j=0;j<n-1-i;j++)
       {if(elmnt[j]<elmnt[j+1])
         {elmnt[j]=elmnt[j]+elmnt[j+1];
          elmnt[j+1]=elmnt[j]-elmnt[j+1];
          elmnt[j]=elmnt[j]-elmnt[j+1];
         }
       }
     }
 }
 int main()
 {stack s1,s2;s1.top=-1;
  cin>>s1.n;
  for(s1.i=0;s1.i<s1.n;s1.i++)
  {cin>>s1.j;
   s1.push(s1.j);
  }
  s1.sort();
  s2.top=-1;
  for(s2.i=0;s2.i<s1.n;s2.i++)
  s2.push(s1.elmnt[s2.i]);
  for(s2.i=0;s2.i<s1.n;s2.i++)
  s2.pop();
 }

Queue implementation and operation in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class queue
{ public:
    int size;
    int elmnt[50];
    int front;
    int rear;
    int isfull();
int isempty();
    void enqueue(int e);
    int dequeue();
    queue()
    {size=5;
     front=-1;
     rear=-1;
    }
};
int queue::isfull()
{if((rear+1)%size==front)
  {cout<<"Full";
   return 1;
  }
  else
   return 0;
}
int queue::isempty()
{if(front==-1)
  {cout<<"Empty";
   return 1;
  }
  else
  return 0;
}
void queue::enqueue(int e)
{ if(isfull())
   exit(1);
  else
   { rear=(rear+1)%size;
     elmnt[rear]=e;
     if(front==-1)
     front++;
   }
}
int queue::dequeue()
{ int c;
  if(isempty())
   exit(1);
  else
   {c=elmnt[front];
    if(front==rear)
    {front=-1;
     rear=-1;
    }
    else
     front=(front+1)%size;
    return(c);
   }
}
int main()
{queue s;int d,i;
  s.enqueue(4);
  s.enqueue(5);
  for(i=0;i<3;i++)
  {d=s.dequeue();
   cout<<d<<endl;
  }
}

Stack of stack in C++

#include<iostream>
using namespace std;
class stack
{public:
  int top,t,arr[50];
  void push(int c);
  int pop();
};
void stack::push(int c)
{arr[++top]=c;
}
int stack::pop()
{t=arr[top--];
}
int main()
{stack s;int n,no,i,j,z,ctr=-1;
 s.top=-1;
 cout<<"no:";
 cin>>n;
 cout<<"-1\n";
 for(i=0;i<n;i++)
 {do
  {cin>>no;
   if(no!=-1)
   s.push(no);
   ++ctr;
  }
  while(no!=-1);
  s.push(ctr);
  ctr=-1;
 }
 for(i=n-1;i>=0;i--)
 {cout<<i+1<<" Stack\n";
    s.pop();
    z=s.t;
    for(j=0;j<z;j++)
    {s.pop();
    cout<<s.t<<endl;
    }
 }
}

Infix to Postfix using stack in C++

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class stack
{ public:
  int top,t,i;char e[10],li[20];
  void push(char c);
  char pop();
  char pop2();
  char arr[10],h;
  int random();
};
void stack::push(char c)
{arr[++top]=c;
}
char stack::pop()
{ while(arr[top]=='-'  ||  arr[top]=='+'  ||  arr[top]=='*' || arr[top]=='/')
  e[++t]=arr[top--];
  push(li[i]);
}
char stack::pop2()
{ while(arr[top]=='*' || arr[top]=='/')
  e[++t]=arr[top--];
  push(li[i]);
}
int stack::random()
{ if(arr[0]=='+' && arr[1]=='-')
   {pop();}
   else if((arr[top]=='*') && (arr[(top+1)]=='-' || arr[top+1]=='+'))
   pop();
}
int main()
{stack s;int j,k,z;
 s.top=-1;s.t=-1;
 cout<<"Enter the infix expression:\n";
 cin.getline(s.li,20);
 j=strlen(s.li);
 cout<<"Hence the postfix of this expression is:\n";
 for(s.i=0;s.i<j;s.i++)
  {if(s.li[s.i]=='+')
     {if(s.top==-1)
 {s.push('+');}
 else
 {s.pop();
  for(k=0;k<=s.t;k++)
  {cout<<s.e[k];}}
  s.t=-1;}
   else if(s.li[s.i]=='-')
     {if(s.top==-1)
 {s.push('-');}
 else
 {s.pop();
  for(k=0;k<=s.t;k++)
  {cout<<s.e[k];}}
  s.t=-1;}
   else if(s.li[s.i]=='*')
    {if(s.top=='-' || s.top=='+' || s.top==-1)
 {s.push('*');}
 else
 {s.pop2();
  for(k=0;k<=s.t;k++)
  {cout<<s.e[k];}}
  s.t=-1;}
   else if(s.li[s.i]=='/')
    {if(s.top=='-' || s.top=='+'  ||  s.top==-1)
 {s.push('/');}
 else
 {s.pop2();
  for(k=0;k<=s.t;k++)
  {cout<<s.e[k];}}
  s.t=-1;}
   else
    cout<<s.li[s.i];
  }
  for(k=s.top;k>=0;k--)
  cout<<s.arr[k];
}

Contributors

Translate