You are on page 1of 16

CSL-221: Data Structures & Algorithms

Semester: BS IT – 03(A)

Lab 06: An array-based implementation of STACK


Exercises

Exercise 1

Write a program as follows for STACK with10 integers.

SOLUTION:

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

int main() {
int stack[10], n=10, top=-1;
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
break;
}
case 2: {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
break;
}}
case 3: {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}

}while(ch!=4);
return 0;
}

OUTPUT:
Exercise 2

Test the program using the following procedure: STACK of size N=6

1. Call PUSH (5)


2. Call PUSH (2)
3. Call PUSH (3)
4. Call POP ()
5. Call PUSH (6)
6. Call PUSH (9)
7. Call PUSH (3)
8. Call DISPLAY ()
9. Call TOP ()
SOLUTION:

SOURCE CODE:

#include <iostream>
using namespace std;
int stack[6], n=6, top=-1;
void Top(){
cout<<"top of array is:"<<top;
}
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
cout<<"the pushed value is "<<stack[top]<<endl;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
push(5);
push(2);
push(3);
pop();
pop();
push(9);
pop();
push(7);
display();
Top();
}

OUTPUT:
Exercise 3

There are 20 books placed on a table in a pile. Sara wants to arrange these books on shelf. She placed the
books on shelf one by one.
Write a program that implements the above scenario and print number of books left on table and number
of books placed on shelf.
SOLUTION:

SOURCE CODE:

#include <cstdlib>
#include <iostream>
using namespace std;
int table[20], shelf[20], n = 20, top1 = -1, top2 = -1;
void push(int val) {
if (top1 >= n - 1)
cout << "Stack Overflow" << endl;
else {
top1++;
table[top1] = val;
}
}
void pop() {
if (top1 <= -1)
cout << "Stack Underflow" << endl;
else {
cout << "The popped element is " << table[top1] << endl;
top1--;
}
}
int main(){

int val, ch, val2;

do{
cout << "Choose 1 to keep the books on the table\nChoose 2 to take the book from table and enter it in the shelf\
n"<<endl;
cin >> ch;
if (ch == 1){
for (int i = 0; i < 20; i++){
cout << "Enter value to be pushed:" << endl;
cin >> val;
push(val);
}
if (top1 >= 0) {
cout << "-----------------------" << endl;
cout << "Table elements are:";
for (int i = top1; i >= 0; i--){
cout << table[i] << " ";
}
cout << endl;
cout << "-----------------------" << endl;
}
}
if (ch == 2){
val2 = table[top1];
pop();
cout << "\n\n-----------------------" << endl;
cout << "Enter " << val2 << " in the shelf.\n";
top2++;
shelf[top2] = val2;
cout << "Books left on the table\n";
for (int i = top1; i >= 0; i--){
cout << table[i] << " ";
}
cout << endl;
cout << "\n\n-----------------------" << endl;
cout << "Books in the shelf\n";
for (int i = top2; i >= 0; i--){
cout << shelf[i] << " ";

}
cout << endl;
}
} while (ch != 3);

OUTPUT:
Exercise 4

There are five stacks of Chips packs with different flavours in a carton. Each Stack contains five packs of
Chips. Packet weights may vary in each stack.
Write a program that implements the above scenario. Print all flavours of chips with each packet weight.
Also print the weight of the carton.
SOLUTION:
SOURCE CODE:

#include <cstdlib>
#include <iostream>
using namespace std;
float chp1[5], chp2[5], chp3[5], chp4[5], chp5[5], count;
int n = 5, top = -1;
void push(float val) {
cout << "Stack Overflow" << endl;
else {
top++;
chp1[top] = val;
count = count + chp1[top];
}
}
void push(float val) {
cout << "Stack Overflow" << endl;
else {
top++;
chp2[top] = val;
count = count + chp2[top];
}

}
void push(float val) {
cout << "Stack Overflow" << endl;
else {
top++;
chp3[top] = val;
count = count + chp3[top];
}

}
void push(float val) {
cout << "Stack Overflow" << endl;
else {
top++;
chp4[top] = val;
count = count + chp4[top];
}

}
void push(float val) {
cout << "Stack Overflow" << endl;
else {
top++;
chp5[top] = val;
count = count + chp5[top];
}

}
int main(){
int ch;
float val;
do{
cout << "Choose 1 to keep the books on the table\nChoose 2 to take the book from table and enter
it in the shelf\n" << endl;
cin >> ch;
if (ch == 1){
cout << "\n\n-----------------------" << endl;
cout << "CHEESE: " << endl;
for (int i = 0; i < 5; i++){
cout << "Enter value to be pushed:" << endl;
cin >> val;
push(val);
}
if (top >= 0) {
cout << "-----------------------" << endl;
cout << "Packet weights: ";
for (int i = top; i >= 0; i--){
cout << chp1[i] << " gm " << " ";
}
cout << endl;
cout << "-----------------------" << endl;
}

cout << "\n\n-----------------------" << endl;


cout << "MASALA: " << endl;
top = -1;
for (int i = 0; i < 5; i++){
cout << "Enter value to be pushed:" << endl;
cin >> val;
push2(val);
}
if (top >= 0) {
cout << "-----------------------" << endl;
cout << "Packet weights: ";
for (int i = top; i >= 0; i--){
cout << chp2[i] << " gm " << " ";
}
cout << endl;
cout << "-----------------------" << endl;
}

cout << "\n\n-----------------------" << endl;


cout << "SALTY: " << endl;
top = -1;
for (int i = 0; i < 5; i++){
cout << "Enter value to be pushed:" << endl;
cin >> val;
push3(val);
}
if (top >= 0) {
cout << "-----------------------" << endl;
cout << "Packet weights: ";
for (int i = top; i >= 0; i--){
cout << chp3[i] << " gm " << " ";
}
cout << endl;
cout << "-----------------------" << endl;
}
cout << "\n\n-----------------------" << endl;
cout << "TARAGON SAUCE: " << endl;
top = -1;
for (int i = 0; i < 5; i++){
cout << "Enter value to be pushed:" << endl;
cin >> val;
push4(val);
}
if (top >= 0) {
cout << "-----------------------" << endl;
cout << "Packet weights: ";
for (int i = top; i >= 0; i--){
cout << chp4[i] << " gm " << " ";
}
cout << endl;
cout << "-----------------------" << endl;
}

cout << "\n\n-----------------------" << endl;


cout << "CHATNI: " << endl;
top = -1;
for (int i = 0; i < 5; i++){
cout << "Enter value to be pushed:" << endl;
cin >> val;
push5(val);
}
if (top >= 0) {
cout << "-----------------------" << endl;
cout << "Packet weights: ";
for (int i = top; i >= 0; i--){
cout << chp5[i] << " gm " << " ";
}
cout << endl;
cout << "-----------------------" << endl;
}
cout << "-----------------------" << endl;
cout << "Total weight of carton: " << count << endl;
cout << "-----------------------" << endl;
}
if (ch == 2){
cout << "Total weight of carton: " << count << endl;
}
} while (ch != 3);

OUTPUT:
Department of Computer Sciences 3 Semester BSIT 3
CSL-221: Data Structure & Algorithm Lab 06: STACK

Exercise 5

Ali and Saad are playing a game. Both have list A and B respectively of same numbers (1-20) in
different order. Both must guess the number placed at the last position of the list. If Ali’s last number is
greater than Saad’s last number, then number will be removed from Ali’ list and point will be awarded
to Ali and same case goes with Saad. Assume that both have 10 turns, and those who have more points
will be the winner. Write a program that implements the above scenario

SOLUTION:
SOURCE CODE:

#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int top = 19, top1 = 19;
int A_saad[20] = { 1, 6, 4, 8, 7, 2, 9, 5, 10, 15, 12, 17, 13, 16, 14, 11, 3, 18, 20, 17 },
B_ali[20] = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 12, 14, 16, 18, 20, 11, 13, 15, 17, 19 },
SaadPoints = 0, AliPoints = 0;
cout << "------------Game starts--------------" << endl;
for (int i = 1; i <= 10; i++) {
cout << "Turn " << i << endl;
if (A_saad[top] > B_ali[top1]) {
if (top <= -1)
cout << "Stack Underflow" << endl;
else {
cout << "-------------------------" << endl;
cout << "Saad got a point!!" << endl;
cout << "The popped element is " << A_saad[top] << endl;
cout << "-------------------------" << endl;
top--;
SaadPoints = SaadPoints + 1;

}
}
else if (A_saad[top] < B_ali[top1]) {
if (top1 <= -1)
cout << "Stack Underflow" << endl;
else {
cout << "-------------------------" << endl;
cout << "Ali got a point!!" << endl;
cout << "The popped element is " << B_ali[top1] << endl;
cout << "-------------------------" << endl;
top1--;
AliPoints = AliPoints + 1;
}
}
else if (A_saad[top] == B_ali[top1]){
cout << "\n-------------------------" << endl;
cout << "Numbers are same: " << endl;
cout << "\n-------------------------" << endl;
top--;
top1--;
}
}
if (SaadPoints > AliPoints){
cout << "\n-------------------------" << endl;
cout << "Saad is the winner!!\n" << "Points: " << SaadPoints << endl;
}
else{
cout << "\n-------------------------" << endl;
cout << "Ali is the winner!!\n" << "Points: " << AliPoints << endl;

}
return 0;
}

OUTPUT:
Exercise 6
Write a program that evaluates the postfix expression using stack
SOLUTION:
SOURCE CODE:

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

// Function to evaluate a given postfix expression


int evalPostfix(string exp)
{
// create an empty stack
stack<int> stack;

// traverse the given expression


for (int i = 0; i < 5; i++)
{
// if the current character is an operand, push it into the stack
if (exp[i] >= '0' && exp[i] <= '9') {
stack.push(exp[i] - '0');
}
// if the current character is an operator
else {
// remove the top two elements from the stack
int x = stack.top();
stack.pop();

int y = stack.top();
stack.pop();
// evaluate the expression `x op y`, and push the // result back to the stack
if (exp[i] == '+') {
stack.push(y + x);
}
else if (exp[i] == '-') {
stack.push(y - x);
}
else if (exp[i] == '/') {
stack.push(y / x);
}
}
}
// At this point, the stack is left with only one element, i.e.,
// expression result
return stack.top();
}
int main()
{
string exp = "138x+";

cout << evalPostfix(exp) << endl;


return 0;
}
OUTPUT:

You might also like