0% found this document useful (0 votes)
4 views10 pages

AIM:-Perform Stack Operations Using Array Implementation. Use Templates. CODE

The document contains code for implementing stack operations using an array with templates in C++. It also includes two methods for calculating the GCD of two numbers: one using recursion and the other without recursion. The code snippets demonstrate the functionality of both the stack operations and the GCD calculations.

Uploaded by

CS 73 Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

AIM:-Perform Stack Operations Using Array Implementation. Use Templates. CODE

The document contains code for implementing stack operations using an array with templates in C++. It also includes two methods for calculating the GCD of two numbers: one using recursion and the other without recursion. The code snippets demonstrate the functionality of both the stack operations and the GCD calculations.

Uploaded by

CS 73 Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

AIM:-Perform Stack operations using Array

implementation. Use Templates.


CODE:-
#include <iostream>
#include <cstdlib>
using namespace std;
#define SIZE 10
template <class X>
class stack
{
X *arr;
int top;
int capacity;

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;
}

template <class X>


void stack<X>::push(X x)
{
if (isFull())
{
cout << "OverFlow\nProgram Terminated\n";
exit(EXIT_FAILURE);
}

cout << "Inserting " << x << endl;


arr[++top] = x;
}
template <class X>
X stack<X>::pop()
{
if (isEmpty())
{
cout << "UnderFlow\nProgram Terminated\
n";
exit(EXIT_FAILURE);
}

cout << "Removing " << peek() << endl;


popped element
return arr[top--];
}
template <class X>
X stack<X>::peek()
{
if (!isEmpty())
return arr[top];
else
exit(EXIT_FAILURE);
}
template <class X>
int stack<X>::size()
{
return top + 1;
}
template <class X>
bool stack<X>::isEmpty()
{
return top == -1; // or return size() == 0;
}
template <class X>
bool stack<X>::isFull()
{
return top == capacity - 1; // or return size()
== capacity;
}
int main()
{
stack<string> pt(2);

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:-

AIM:- WAP TO CALCULATE GCD OF TWO


NUMBERS (i) with recursion (ii) without
recursion
CODE:-
(i) With recursion
#include <iostream>
using namespace std;

int hcf(int n1, int n2);

int main()
{
int n1, n2;

cout << "Enter two positive integers: ";


cin >> n1 >> n2;

cout << "H.C.F of " << n1 << " & " << n2 << " is:
" << hcf(n1, n2);

return 0;
}

int hcf(int n1, int n2)


{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

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;
}

cout << "HCF = " << n1;


return 0;
}

OUTPUT:-

You might also like