Stack and Queues Using Class Templates
Stack and Queues Using Class Templates
Stack and Queues Using Class Templates
#include<iostream.h>
#include<conio.h>
class stack
public:
T st[100];
int top,size;
stack()
cin>>size;
top=-1;
void push(T x)
st[++top]=x;
T pop()
return(st[top--]);
void display()
{
int i;
for(i=top;i>=0;i--)
cout<<endl<<" "<<st[i];
};
void main()
int n,f=0;
clrscr();
stack <float>s1;//change float to any data type for Eg,<char> for queue which stores character
type of data
while(f!=1)
cin>>n;
switch(n)
case 1:
cin>>x;
s1.push(x);
}else
cout<<"\n --OVERFLOW--\n";
break;
case 2:
if(s1.top!=-1)
}else
cout<<"\n --UNDERFLOW--\n";
break;
case 3:
if(s1.top!=-1)
s1.display();
}else
break;
case 4:
f=1;
break;
getch() ;
#include<iostream.h>
#include<conio.h>
class queue
public:
int rear,front;
int size;
T Q[100];
queue()
rear=front=-1;
cin>>size;
}
void enqueue()
if(rear==-1)
front=0;
T x;
cin>>x;
Q[++rear]=x;
}else
cout<<"\n --OVERFLOW--\n";
void dequeue()
if(rear == -1)
cout<<"\n --UNDERFLOW--\n";
}else
rear=front=-1;
}else
for(int i=front;i<rear;i++)
Q[i]=Q[i+1];
rear--;
void display()
if(rear!=-1)
int i;
cout.precision(2); //just in case of float
for(i=front;i<=rear;i++)
}else
};
void main()
int n,f=0;
clrscr();
queue <float>s1;
while(f!=1)
cin>>n;
switch(n)
{
case 1:
s1.enqueue();
break;
case 2:
s1.dequeue();
break;
case 3:
s1.display();
break;
case 4:
f=1;
break;
getch();