Vous êtes sur la page 1sur 8

Atif Jalal

02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020

Exercise 1:
SOURCE CODE:
#include <iostream>
using namespace std;

void push(int);
bool isfull();
void pop();
bool isempty();
void display();

int stack[10], num = 10 , top = -1;


Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020

void main()
{
int a = 0, b;
cout << "\n ------------------ ARRAY BASED STACK ------------------" <<endl;
cout << " 1. Push an element on stack \n 2. Pop an element from stack " <<
endl;
cout << " 3. Display \n 4. Top element \n 5. Exit" << endl;
cout << " -------------------------------------------------------" <<endl;
while (a != 5)
{
cout << "\n Enter your choice : ";
cin >> a;
switch (a)
{
case 1:
{
cout << " Enter number : ";
cin >> b;
push(b);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout << " Top element : " << stack[top] << endl;
break;
}
case 5:
break;
default:
cout << "\n Select from the given (1-5) options." << endl;
}
}
system("pause");
}

void push(int value)


{
if (isfull())
cout << "\n Stack overflow." << endl;
else
{
top++;
stack[top] = value;
}
}

bool isfull()
{
if (top < num)
return false;
else
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020
return true;
}

void pop()
{
stack[top] = 0;
top--;
}

bool isempty()
{
if (top == -1)
return true;
else
return false;
}
void display()
{
cout << " Stack : {";
for (int i = 0; i <= top; i++)
{
cout << stack[i] <<",";
}
cout << "\b}" <<endl;

OUTPUT:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020

Exercise 2:
SOURCE CODE:
#include "stdafx.h"

#include <iostream>

using namespace std;

int top=1;

int arr[5];

void push(int arr[],int item)

if(top>5)
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020
{

cout<<"Stack is Over Flow";

else

top=top+1;

arr[top]=item;

return;

void pop(int arr[])

for(int a=0;a<1;a++)

if(top==0)

cout<<"Stack Is Uder Flow";

break;

}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020
else

arr[top]=arr[top+1];

top=top-1;

return;

void peak(int arr[],const int size)

cout<<endl<<"Your Stack Value IS"<<endl;

for(int o=size;o>0;o--)

if(arr[o]!=0)

cout<<arr[o]<<endl;

}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020

int _tmain(int argc, _TCHAR* argv[])

system("color f0");

push(arr,5);

push(arr,2);

push(arr,3);

int a=arr[4];

int b=arr[3];

pop(arr);

pop(arr);

push(arr,b+2);

push(arr,8);

push(arr,a+b);

cout<<" Yor Stack Value IS"<<endl;

while (top!=0)

if(arr[top]!=0)

cout<<" "<<arr[top]<<endl;

pop(arr);

return 0;

}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020

Output:

Vous aimerez peut-être aussi