You are on page 1of 10

Lab#2

Name:Hamza khalid
Roll No. 20011598-074
Section :B(Software Engineering)

Question 1:
Made two stacks one name even and one named odd.
Push their sum in the third stack named sum.

→Source code:
#include<iostream>
using namespace std;
int size=10;

class _stack
{
private:

int *node;
int top;

public:

_stack()
{

//dynamic allocation of array


node=new int[size];
top=-1;
}

void push(int value)


{

if(isfull())
{
cout<<"The stack is full you can't push more elements"<<endl;
}
else
{

top++;

node[top]=value;
}

int pop()
{ //initialize it with zero
int value=0;
if(isempty())
cout<<"There is nothing to pop stack is empty"<<endl;
else
{
value=node[top];
node[top]=0;
top--;
}
return value;

bool isfull()
{
if(top==(size-1))
return true;
else
return false;
}
bool isempty()
{

if(top==-1)
return true;
else
return false;

void display()
{
for(int i=top;i>=0;i--)
cout<<node[i]<<endl;
}

};

int main()
{

_stack even;
_stack odd;
_stack add;

//Now pushing in even stack

for(int i=1;i<=20;i++)
{ if(i%2==0)
{ even.push(i);}
}

// now pushing in odd stack

for(int i=1;i<20;i++)
{ if(i%2!=0)
{
odd.push(i);
}
}
//Now showing the initial values

cout<<"Now showing even stack"<<endl;


even.display();

cout<<"\nNow showing odd stack "<<endl;


odd.display();

//Now pushing their sum in third stack named add

for(int i=0;i<10;i++)
add.push((even.pop()+odd.pop()));

cout<<"\nNow showing the add stack"<<endl;


for(int i=0;i<10;i++)
cout<<add.pop()<<endl;

Output:
Question #2:
→Source code:
#include<iostream>
using namespace std;
int size=15;
class _stack
{
private:

int *node;
int top;

public:

_stack()
{

//dynamic allocation of array


node=new int[size];
top=-1;
}

void push(int value)


{

if(isfull())
{
cout<<"The stack is full you can't push more elements"<<endl;
}
else
{

top++;

node[top]=value;
}

void pop()
{ //initialize it with zero
int value=0;
if(isempty())
cout<<"There is nothing to pop stack is empty"<<endl;
else
{
value=node[top];
node[top]=0;
top--;
}
cout<<value<<endl;

bool isfull()
{
if(top==(size-1))
return true;
else
return false;
}

bool isempty()
{

if(top==-1)
return true;
else
return false;

void display()
{
for(int i=0;i<=top;i++)
cout<<node[i]<<endl;
}

};

int main()
{

_stack less_than_10;
_stack less_than_20;
_stack less_than_30;

//Now taking values


int value;
int ten=0;
int twenty=0;
int thirty=0;
for(int i=1;i<=15;i++)
{
cout<<"Enter the value # "<<i<<" : ";
cin>>value;

if(value<=10)
{
less_than_10.push(value);
ten++;

}
else if(value<=20)
{
less_than_20.push(value);
twenty++;
}
else if(value<=30)
{
less_than_30.push(value);
thirty++;
}

// now displaying the values


cout<<"The total values below ten are # "<<ten<<" and they are following :
\n";
for(int i=0;i<ten;i++)
{

less_than_10.pop();
}

cout<<"The total values below ten are # "<<twenty<<" and they are following :
\n";

for(int i=0;i<twenty;i++)
{
less_than_20.pop();
}
cout<<"The total values below ten are # "<<thirty<<" and they are following :
\n";
for(int i=0;i<thirty;i++)
{

less_than_30.pop();
}

Output:

You might also like