AIM:-Perform Stack Operations Using Array Implementation. Use Templates. CODE
AIM:-Perform Stack Operations Using Array Implementation. Use Templates. CODE
public:
stack(int size = SIZE); // constructor
void push(X);
X pop();
X peek();
int size();
bool isEmpty();
bool isFull();
// destructor
~stack(){
delete[] arr;
}
};
template <class X>
stack<X>::stack(int size)
{
arr = new X[size];
capacity = size;
top = -1;
}
pt.push("A");
pt.push("B");
pt.pop();
pt.pop();
pt.push("C");
cout << "Top element is: " << pt.peek() << endl;
cout << "Stack size is " << pt.size() << endl;
pt.pop();
if (pt.isEmpty())
cout << "Stack Is Empty\n";
else
cout << "Stack Is Not Empty\n";
return 0;
}
OUTPUT:-
int main()
{
int n1, n2;
cout << "H.C.F of " << n1 << " & " << n2 << " is:
" << hcf(n1, n2);
return 0;
}
OUTPUT:-
(ii) Without recursion
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
OUTPUT:-