Program Name : 12.1.cpp
#include<iostream>
#include<string.h>
using namespace std;
template<class t>
class stack
{
public:
t data;
stack *prev;
};
template<class t>
class stacks
{
//int stack[size];
stack<t> *top;
public:
stacks()
{
top=NULL;
}
void push(t n)
{
stack<t> *temp;
/*if(top==(size-1))
{
cout<<"overflow element not entered\n";
return 1;
}*/
//else
//{
temp=new stack<t>;
temp->data=n;
temp->prev=top;
top=temp;
//return 0;
//}
}
t pop()
{
t a;
if(top==NULL)
{
cout<<"underflow:\n";
return -1;
}
else
{
a=top->data;
top=top->prev;
return a;
}
}
void display()
{
cout<<"The data in stack is:\n";
while(top!=NULL)
{
cout<<(char)top->data<<"\n^\n"<<endl;
top=top->prev;
}
}
int empty()
{
if(top==NULL)
return -1;
else
return 1;
}
};
----------------------------------------------Program Name : 12.2.cpp
#include<iostream>
using namespace std;
const int size=40;
int flag=0;
/*void attain(int n)
{
size=n;
}*/
class queues
{
int front,rear;
int queue[size];
public:
queues()
{
front=rear=-1;
}
void insert(int n)
{
flag=0;
if(/*(rear-front)>(size-1))&&*/(rear==(size-1)))
{
cout<<"overflow element can not be entered\n";
flag=1;
//return 1;
}
else if(rear<(size-1))
{
queue[++rear]=n;
front=0;
//return 0;
}
else if((rear+front)>=size)
{
for(int i=front-1;i<rear;i++)
queue[i]=queue[i+1];
queue[rear]=n;
}
}
int dlt()
{
int a;
if((rear==-1)||(empty()==-1))
{
cout<<"underflow:\n";
return 0;
}
else
{
a=queue[front++];
cout<<"In function delete:\nfront= "<<front<<endl;
return a;
}
}
void display()
{
cout<<"In function display:\nfront= "<<front<<endl<<"rear= "<<rear<<endl;
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
cout<<endl;
}
int empty()
{
if(front>rear)
return -1;
else
return 1;
}
};
----------------------------------------------
Program Name : 12main.cpp
#include<iostream>
#include"12.1.cpp"
#include"12.2.cpp"
using namespace std;
int main()
{
stacks<int> s1;
int s[100],n;
queues q1;
char ch;
cout<<"Start entering numbers in stack:\n";
do
{
cin>>n;
s1.push(n);
//temp++;
cout<<"Want to enter more:\n";
cin>>ch;
}while(ch=='y');
//attain(temp);
while(s1.empty()!=-1)
q1.insert(s1.pop());
while(q1.empty()!=-1) s1.push(q1.dlt());
cout<<"The resulting reversed stack is:\n";
s1.display();
}
------------------------------------------------------------------------------------------------------------------