Professional Documents
Culture Documents
Hamza Khalid 074#lab2
Hamza Khalid 074#lab2
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()
{
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;
for(int i=1;i<=20;i++)
{ if(i%2==0)
{ even.push(i);}
}
for(int i=1;i<20;i++)
{ if(i%2!=0)
{
odd.push(i);
}
}
//Now showing the initial values
for(int i=0;i<10;i++)
add.push((even.pop()+odd.pop()));
Output:
Question #2:
→Source code:
#include<iostream>
using namespace std;
int size=15;
class _stack
{
private:
int *node;
int top;
public:
_stack()
{
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;
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++;
}
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: