You are on page 1of 5

Lab Exercises

Exercise 1

Write a program that implements the stack with Max_size=7.Program must have following functions

PUSH()

POP()

Display()

Test the program using the following procedure

a. Call PUSH (5)


b. Call PUSH (2)
c. Call PUSH (3)
d. Call PUSH (6)
e. Call PUSH (9)
f. Call PUSH (3)
g. Call PUSH(7)
h. Call PUSH(9)
i. Display all elements.
j. POP an element
k. Display all elements
Code:
#include<iostream>
using namespace std;
const int size = 7;
int stack[size];
int top = -1;
void PUSH(int item){
if (top == size-1){
cout << "overflow\n";
}
else
{ top += 1;
stack[top] = item;
}
}
void pop(){
if (top == -1){
cout << "under Flow \n";
}
else{
top =top - 1;
cout << "\nElement removed\n";
}
}
void display(){
for (int i = top; i >= 0; i--){
cout << " " << stack[i];
}
}
void main(){
PUSH(5);
PUSH(2);
PUSH(3);
PUSH(6);
PUSH(9);
PUSH(3);
PUSH(7);
PUSH(9);
display();
pop();
display();
cout << endl;
system("pause");

Output:

Exercise 2

There are five balls of different colours (Red, Green, Blue, Orange, and Yellow) in a jar. Players take the
ball from jar for playing. Write a program that maintains the stack of balls and prints the following

a. How many balls are left in a jar?


b. Colour of ball taken by each player.
c. “No ball available” if palyer6 arrives.

Exercise 3

Write a program that evaluates the postfix expression using stack.

#include <iostream>
#include <string>
using namespace std;

// Function to evaluate a postfix expression


int evaluatePostfix(string exp) {
int stack[1000];
int top = -1;
for (int i = 0; i < exp.length(); i++) {
char c = exp[i];
if (isdigit(c)) {
stack[++top] = c - '0';
}
else {
int op2 = stack[top--];
int op1 = stack[top--];
switch (c) {
case '+':
stack[++top] = op1 + op2;
break;
case '-':
stack[++top] = op1 - op2;
break;
case '*':
stack[++top] = op1 * op2;
break;
case '/':
stack[++top] = op1 / op2;
break;
}
}
}
return stack[top--];
}

int main() {
string exp;
cout << "Enter a postfix expression: ";
cin >> exp;
int result = evaluatePostfix(exp);
cout << "Result: " << result << endl;
system("Pause");
}

#include <iostream>
using namespace std;
void Push(string colors[], int& top, string item) {

if (top != 4) {
top = top + 1;
colors[top] = item;

}
else {
cout << " NO Ball Available! Player6 occured. " << endl;

}
}
void Pop(string colors[], int & top, int & player) {
if (top == -1) {
cout << " Stack Underflow!" << endl;
}
else {
cout << " Player " << player << " Took " << colors[top] << " Color Ball"
<< endl;
player++;
top = top - 1;
}
}
void display(string colors[], int & top) {
for (int i = top; i >= 0; i--) {
cout << colors[i] << endl;
}
}

void main() {
string colors[5];
int player = 1;
int top = -1;
string item;
Push(colors, top, "RED");
Push(colors, top, "GREEN");
Push(colors, top, "BLUE");
Push(colors, top, "ORANGE");
Push(colors, top, "YELLOW");
Push(colors, top, "YELLOW");

display(colors, top);
Pop(colors, top, player);
Pop(colors, top, player);
Pop(colors, top, player);
Pop(colors, top, player);
Pop(colors, top, player);

You might also like