Multi Stack using single Array
#include<iostream>
using namespace std;
class Stack_ADT
{
public:
int *a,*t,*s,*c;
Stack_ADT(int n,int m)
{
int i;
a=new int[n];
t=new int[m];
s=new int[m+1];
for(i=0;i<m;i++)
{
t[i]=(n/m)*i-1;
s[i]=(n/m)*i-1;
}
s[i]=n-1;
}
void m_push(int i,int x);
void m_pop(int i);
void m_display(int);
};
void Stack_ADT::m_push(int i,int x)
{
if(t[i]==s[i+1])
cout<<"\nStack "<<i+1<<" is FULL\n";
else
{
t[i]=t[i]+1;
a[t[i]]=x;
c[i]=c[i]+1;
cout<<"\n Element "<<a[t[i]]<<" Pushed into the Stack ";
}
}
void Stack_ADT::m_pop(int i)
{
if(t[i]==s[i])
cout<<"\nStack "<<i+1<<" is EMPTY\n";
else
{
t[i]=t[i]-1;
c[i]=c[i]-1;
cout<<"\nPopped Element of the Stack : "<<a[t[i]+1];
}
}
void Stack_ADT::m_display(int i)
{
int j=s[i];
if(t[i]==s[i])
cout<<"\nStack "<<i+1<<" is EMPTY\n";
else
{
cout<<"\n Elements of the Stack "<<i+1<<" : \n";
while(t[i]>j)
{
cout<<a[j+1]<<"\n";
j++;
}
}
}
int main()
{
int i,ch,x,n,m;
cout<<"\n Enter number of Stacks ";
cin>>m;
cout<<"\n Enter size of array ";
cin>>n;
Stack_ADT ob(n,m);
while(1)
{
cout<<"\n Enter Your choice";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter element to push ";
cin>>x;
cout<<"\n enter stack number 1 to "<<m<" ";
cin>>i;
if(i<=m)
ob.m_push(i-1,x);
else
{
cout<<"Entered wrong stack";
}
break;
case 2:
cout<<"\n enter stack number 1 to "<<m<<" to Pop the element ";
cin>>i;
if(i<=m)
ob.m_pop(i-1);
else
{
cout<<"Entered wrong stack";
}
break;
case 3:
cout<<"\n enter stack number 1 to "<<m<<" to display the elements ";
cin>>i;
if(i<=m)
ob.m_display(i-1);
else
{
cout<<"Entered wrong stack";
}
break;
case 4:
exit(0);
default:
cout<<"\n Entered wrong choice";
}
}
return 0;
}
using namespace std;
class Stack_ADT
{
public:
int *a,*t,*s,*c;
Stack_ADT(int n,int m)
{
int i;
a=new int[n];
t=new int[m];
s=new int[m+1];
for(i=0;i<m;i++)
{
t[i]=(n/m)*i-1;
s[i]=(n/m)*i-1;
}
s[i]=n-1;
}
void m_push(int i,int x);
void m_pop(int i);
void m_display(int);
};
void Stack_ADT::m_push(int i,int x)
{
if(t[i]==s[i+1])
cout<<"\nStack "<<i+1<<" is FULL\n";
else
{
t[i]=t[i]+1;
a[t[i]]=x;
c[i]=c[i]+1;
cout<<"\n Element "<<a[t[i]]<<" Pushed into the Stack ";
}
}
void Stack_ADT::m_pop(int i)
{
if(t[i]==s[i])
cout<<"\nStack "<<i+1<<" is EMPTY\n";
else
{
t[i]=t[i]-1;
c[i]=c[i]-1;
cout<<"\nPopped Element of the Stack : "<<a[t[i]+1];
}
}
void Stack_ADT::m_display(int i)
{
int j=s[i];
if(t[i]==s[i])
cout<<"\nStack "<<i+1<<" is EMPTY\n";
else
{
cout<<"\n Elements of the Stack "<<i+1<<" : \n";
while(t[i]>j)
{
cout<<a[j+1]<<"\n";
j++;
}
}
}
int main()
{
int i,ch,x,n,m;
cout<<"\n Enter number of Stacks ";
cin>>m;
cout<<"\n Enter size of array ";
cin>>n;
Stack_ADT ob(n,m);
while(1)
{
cout<<"\n Enter Your choice";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter element to push ";
cin>>x;
cout<<"\n enter stack number 1 to "<<m<" ";
cin>>i;
if(i<=m)
ob.m_push(i-1,x);
else
{
cout<<"Entered wrong stack";
}
break;
case 2:
cout<<"\n enter stack number 1 to "<<m<<" to Pop the element ";
cin>>i;
if(i<=m)
ob.m_pop(i-1);
else
{
cout<<"Entered wrong stack";
}
break;
case 3:
cout<<"\n enter stack number 1 to "<<m<<" to display the elements ";
cin>>i;
if(i<=m)
ob.m_display(i-1);
else
{
cout<<"Entered wrong stack";
}
break;
case 4:
exit(0);
default:
cout<<"\n Entered wrong choice";
}
}
return 0;
}
Labels: DS through C++ lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home