Explorer les Livres électroniques
Catégories
Explorer les Livres audio
Catégories
Explorer les Magazines
Catégories
Explorer les Documents
Catégories
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();
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");
}
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>
int top=1;
int arr[5];
if(top>5)
Atif Jalal
02-235191-027
BS (IT)-3A Lab 06 Date: 14 July, 2020
{
else
top=top+1;
arr[top]=item;
return;
for(int a=0;a<1;a++)
if(top==0)
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;
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
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);
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: