PROGRAM TO STORE char IN STACK USING TEMPLATES.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
template <class Type>
class Stack
{
private:
Type *Arr;
int Top, Size;
public:
Stack(int Siz)
{
Size = Siz;
Arr = new Type[Size];
for (int I = 0; I <Size; I++)
{
Arr[I] = 0;
}
Top = -1;
}
void Push(Type Num)
{
if (Top == Size - 1)
{
cout << "\nStack Overflowed..........." << endl;
exit(1);
}
Arr[++Top] = Num;
}
Type Pop()
{
if (IsEmpty())
{
cout << "\nStack Underflowed..........." << endl;
return 0;
}
return Arr[Top--];
}
inline bool IsEmpty()
{
return (Top == -1);
}
~Stack()
{
cout << "\n\n\n\n\n\n\n..........Program Ended...........\n\n\n\n\n\n\n\n\n";
}
};
int main()
{
Stack<char> Obj1(5);
Obj1.Push('M');
Obj1.Push('A');
Obj1.Push('D');
Obj1.Push('A');
Obj1.Push('M');
cout << "String is: " << endl;
while (!Obj1.IsEmpty())
cout << Obj1.Pop();
getch();
return 0;
}
0 comments:
Post a Comment